Example Discord bot written in Python that uses the chat completions API to have conversations with the gpt-3.5-turbo
model, and the moderations API to filter the messages.
This uses the same model that powers ChatGPT, but it does not necessarily have the same configurations as ChatGPT, so it won't necessarily give the same kind of responses like you would expect from ChatGPT.
This bot uses the OpenAI Python Library and discord.py.
/chat
starts a public thread, with amessage
argument which is the first user message passed to the bot- The model will generate a reply for every user message in any threads started with
/chat
- The entire thread will be passed to the model for each request, so the model will remember previous messages in the thread
- when the context limit is reached, or a max message count is reached in the thread, bot will close the thread
- you can customize the bot instructions by modifying
config.yaml
- you can change the model to other chat models (e.g. GPT-4 models), the hardcoded value is
gpt-3.5-turbo
- It is more cheaper than the
text-davinci-003
- Unlike
text-davinci-003
, it cannot differentiate between users, if more than 1 users are talking to it in the same thread, it sees them all as the same user - It is generally more capable and optimised for chat than
text-davinci-003
- Copy
.env.example
to.env
and start filling in the values as detailed below - Go to https://platform.openai.com/account/api-keys, create a new API key, and fill in
OPENAI_API_KEY
- Create your own Discord application at https://discord.com/developers/applications
- Go to the Bot tab and click "Add Bot"
- Click "Reset Token" and fill in
DISCORD_BOT_TOKEN
- Disable "Public Bot" unless you want your bot to be visible to everyone
- Enable "Message Content Intent" under "Privileged Gateway Intents"
- Click "Reset Token" and fill in
- Go to the OAuth2 tab, copy your "Client ID", and fill in
DISCORD_CLIENT_ID
- Copy the ID the server you want to allow your bot to be used in by right clicking the server icon and clicking "Copy ID". Fill in
ALLOWED_SERVER_IDS
. If you want to allow multiple servers, separate the IDs by "," likeserver_id_1,server_id_2
- Install dependencies and run the bot
You should see an invite URL in the console. Copy and paste it into your browser to add the bot to your server. Note: make sure you are using Python 3.9+ (check with python --version)
pip install -r requirements.txt python -m src.main
- If you want moderation messages, create and copy the channel id for each server that you want the moderation messages to send to in
SERVER_TO_MODERATION_CHANNEL
. This should be of the format:server_id:channel_id,server_id_2:channel_id_2
- If you want to change the personality of the bot, go to
src/config.yaml
and edit the instructions - If you want to change the moderation settings for which messages get flagged or blocked, edit the values in
src/constants.py
. A lower value means less chance of it triggering.
Why isn't my bot responding to commands?
Ensure that the channels your bots have access to allow the bot to have these permissions.
- Send Messages
- Send Messages in Threads
- Create Public Threads
- Manage Messages (only for moderation to delete blocked messages)
- Manage Threads
- Read Message History
- Use Application Commands