Serverless Discord bot in webhook mode to quickly interface with OpenAI's Chat Completion API
Note: This is mainly for personal use, if you would like to add features, do fork the repository. Do perform PRs back if you would be so kind!
- A Cloudflare account with Workers (at least free-tier) enabled
- Completed the Discord Developers - Getting Started - Creating an App with the same scopes and have added the bot to your target server
- An OpenAI API key that has the ability to use the Chat Completion API
- Clone this repository
- Run
npm cioryarn install - Replace
{DISCORD_TOKEN}and{DISCORD_APPLICATION_ID}in the followingDISCORD_TOKEN={DISCORD_TOKEN} DISCORD_APPLICATION_ID={DISCORD_APPLICATION_ID} npx node src/register.jsand run it to register the bot commands - Run
npx wrangler secret put DISCORD_APPLICATION_IDand set the Discord app's ID - Run
npx wrangler secret put DISCORD_PUBLIC_KEYand set the Discord app's public key - Run
npx wrangler secret put DISCORD_TOKENand set the Discord bot's token - Run
npx wrangler secret put OPENAI_API_KEYand set the OpenAI API key - Add space-delimited user IDs to whitelist in
DISCORD_USERID_WHITELISTin wrangler.toml - (Optional) To allow extra lines of context, run
npx wrangler kv:namespace create contextand replace the ID ofCHATGPT_DISCORD_BOT_KVand increaseCONTEXTto more than 0 in wrangler.toml (will consume a lot more tokens), else removekv_namespacesblock entirely from wrangler.toml - (Optional) To change the model, update
CHATGPT_MODELin wrangler.toml to whatever you want as documented at https://platform.openai.com/docs/api-reference/chat/create#chat/create-model - (Optional) To include a
systemmessage to set the AI's behavior, updateCHATGPT_BEHAVIORin wrangler.toml - Run
npx wrangler publishto deploy to Cloudflare Workers - Set the
Interactions Endpoint URLof your Discord application to the URL of the deployed worker

