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 ci
oryarn install
- Replace
{DISCORD_TOKEN}
and{DISCORD_APPLICATION_ID}
in the followingDISCORD_TOKEN={DISCORD_TOKEN} DISCORD_APPLICATION_ID={DISCORD_APPLICATION_ID} npx node src/register.js
and run it to register the bot commands - Run
npx wrangler secret put DISCORD_APPLICATION_ID
and set the Discord app's ID - Run
npx wrangler secret put DISCORD_PUBLIC_KEY
and set the Discord app's public key - Run
npx wrangler secret put DISCORD_TOKEN
and set the Discord bot's token - Run
npx wrangler secret put OPENAI_API_KEY
and set the OpenAI API key - Add space-delimited user IDs to whitelist in
DISCORD_USERID_WHITELIST
in wrangler.toml - (Optional) To allow extra lines of context, run
npx wrangler kv:namespace create context
and replace the ID ofCHATGPT_DISCORD_BOT_KV
and increaseCONTEXT
to more than 0 in wrangler.toml (will consume a lot more tokens), else removekv_namespaces
block entirely from wrangler.toml - (Optional) To change the model, update
CHATGPT_MODEL
in 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
system
message to set the AI's behavior, updateCHATGPT_BEHAVIOR
in wrangler.toml - Run
npx wrangler publish
to deploy to Cloudflare Workers - Set the
Interactions Endpoint URL
of your Discord application to the URL of the deployed worker