A simple Discord bot powered with ChatGPT.
This repository contains a Python-based Discord bot that integrates with OpenAI's ChatGPT API to provide AI-driven responses to user prompts. The bot listens for messages in a Discord channel that start with the > character and returns a response generated by the ChatGPT model.
- Responds to user prompts in Discord channels using OpenAI's GPT-4 model.
- Handles errors gracefully, including OpenAI API errors and Discord message length limits.
- Provides detailed error messages for common issues like exceeding token limits or rate limits.
- Easy integration with environmental variables and Docker container for secure API key management.
- Python 3.13 or higher
- Discord bot token
- OpenAI API key
-
⚠️ Check your Python version: Run python --version to check the version of Python you're using. -
⚠️ If using Python 3.13 or above: You'll need to find an alternative library for audio manipulation. -
⚠️ If using Python 3.11 or 3.12: You can still use audioop, but consider migrating to an alternative library. -
⚠️ Install audioop-lts: For projects like discord.py that require audioop on newer Python versions, you can install the audioop-lts package:
pip install audioop-lts
- Go to the Discord Developer Portal. https://discord.com/developers/applications?form=MG0AV3
- or follow this instructions https://discordpy.readthedocs.io/en/stable/discord.html
- Click on the "New Application" button.
- Give your application a name and click "Create".
- In the "Bot" tab, assign administrator permissions to the bot and obtain its token key.
- Enable all the Privileged Gateway Intents under the "Bot" tab.
⚠️ When a bot is given Administrator permissions in Discord, it gains access to almost all actions within the server.
- In the OAuth tab, enable the "bot" checkbox and select the "administrator" checkbox under bot permissions.
- In the OAuth tab, copy the generated url.
You will use the URL to invite the bot to a Discord server.
- Copy the Generated URL:
- From the OAuth2 section in the Discord Developer Portal, copy the URL that you generated by selecting the required permissions.
- Open the URL in a Browser:
- Paste the URL into your browser's address bar and press Enter.
- Select the Server:
- You will be redirected to a Discord page asking you to select a server to add the bot to.
- From the dropdown menu, choose the server where you want to invite the bot.
- Note: You must have Manage Server permissions in the server to invite a bot.
- Authorize the Bot:
- Click the Authorize button to grant the bot the permissions specified in the URL.
- If required, complete the CAPTCHA to verify.
- Check the Bot's Status:
- Open the Discord server you added the bot to.
- Check the member list to ensure the bot is now a member of the server.
- The bot should appear offline until its code is running and it is connected to Discord.
1 Clone or download the repository
git clone https://github.com/StanlyRod/ChatGPT-discord-bot.git
cd ChatGPT-discord-bot
2 Set Up a Virtual Environment (Optional but Recommended)
python -m venv venv
3 Copy all the required files into the new virtual environment venv folder (bot.py, gptobject.py and requirements.txt).
On windows Command Prompt
copy bot.py venv\
copy gptobject.py venv\
copy requirements.txt venv\
4 Activate the virtual environment
On Windows
venv\Scripts\activate
cd venv
On Linux
source venv/bin/activate
On macOS
source venv/bin/activate
5 Navigate to the venv directory and install the required dependencies
pip install -r requirements.txt
6 Set Environment Variables
- Set the OPENAIKEY and DISCORDBOTKEY environmental variables, with their api key and token value in your system and restart your computer.
⚠️ Ensure that the environment variable names match their corresponding names in the code (OPENAIKEY and DISCORDBOTKEY).
Linux/Mac:
export OPENAIKEY="your-openai-api-key"
export DISCORDBOTKEY="your-discord-bot-key"
Windows (Command Prompt):
set OPENAIKEY=your-openai-api-key
set DISCORDBOTKEY=your-discord-bot-key
Windows (PowerShell):
$env:OPENAIKEY="your-openai-api-key"
$env:DISCORDBOTKEY="your-discord-bot-key"
- After restarting your computer, activate the virtual environment, navigate to the venv directory, and run the bot.py file.
python bot.py
⚠️ Use greater than symbol '>'
- Make sure to use the greater than symbol '>' before any prompt, if the bot sees a prompt without the > prefix, it responds with a reminder: "Every prompt needs to start with a greater than sign '>'. Try again." This helps users quickly understand how to interact with the bot effectively.
Run the bot in a docker container
- Installation of docker desktop
- Discord bot token
- OpenAI API key
⚠️ ** To create a Docker image, ensure you use the Docker file located in the docker folder along with the requirements.txt file, which lists all the dependencies required to run the Python code. All the dependencies in the requirements.txt file in the docker folder are compatible with the python:3.11-bullseye Docker image.**
⚠️ ** Before creating a Docker image, make sure to isolate the Python files (bot.py, gptobject.py) along with all the files in the docker folder (dockerfile, requirements.txt) into a separate directory.**
-
Launch the docker desktop app
-
open the terminal navigate to the new isolated directory and run the bash code to create a docker image.
docker build -t discordbotimage .
- Run the docker image and provide the environment variables names with the openai api key and the discord bot token.
- Docker container running successfully
- Tokens Response: To control the ChatGPT response, adjust the token limit specified in the code.
- ChatGPT Model: By default, the code uses the gpt-4o model, which can be changed to your preferred model.