A discord bot that uses slash commands to generate chat and images using official ChatGPT models. Popular models include ChatGPT-4 and Dalle-3, which can generate text or images, respectively.
To use this script, you must modify THREE lines of code for:
- OpenAI API token
- Discord Bot token
Instructions for creating the OpenAI token, creating the discord bot, downloading and running the python script, and final pointers will be discussed in further detail.
Note: Usage of the OpenAI's API may incur costs. Please refer to OpenAI's pricing policy before continuing any further.
- Head over to OpenAI and create an account. Then, login using the credentials you have just created.
- Once signed in, hover your mouse over the lefthand dropdown and click on
API keys
. - Press
Create new secret key
and type any name of your choice. I named mine "Discord Bot".
- Important: Be sure to copy down the secret key for later use. Do NOT share it with others.
The discord bot must be created in the developer portal.
- Head over to Discord Developers and create an account.
- Sign into the developer portal.
- Create a
New Application
- Name the application and click
Create
- Click on the newly created bot and copy down 3 pieces of information
Token
Application ID
Public Key
The only file required for running the bot is main.py
. Simply download the raw file from GitHub and store it on your local machine.
As mentioned in the beginning, the user must change THREE lines of code to get their own discord bot up and running.
Delete this line of code from main.py
.
load_dotenv()
Next, find these two lines of code. We will modify them, soon.
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
DISCORD_BOT_KEY = os.getenv("DISCORD_BOT_KEY")
Replace os.getenv("OPENAI_API_KEY")
and os.getenv("DISCORD_BOT_KEY")
with their respective keys.
The final code should look something like this.
OPENAI_API_KEY = "YOUR_OPENAI_API_KEY_GOES_HERE"
DISCORD_BOT_KEY = "YOUR_DISCORD_BOT_KEY_GOES_HERE"
To run the script, the user must have Python
installed.
Windows users can install Python from Microsoft Store
or Python Org.
OpenAI offers many models for generating text or images. Check out OpenAI Models. Remember, the pricing varies between models. Be sure to check out OpenAI Pricing.
To change your model, find and modify these two lines of code from main.py
.
CHAT_MODEL = "gpt-3.5-turbo-1106"
IMAGE_MODEL = "dall-e-3"
Now, replace gpt-3.5-turbo-1106
and/or dall-e-3
with the model name.
For example, if I wanted to use GPT-4 Turbo, I would modify one line so it looks like this.
CHAT_MODEL = "gpt-4-0125-preview"
And happy coding!