This project requires python version 3.12.3 discord.py version 2.3.2, OpenAI version 1.30.1 and python-dotenv version 1.0.1 which can be installed via requirements.txt:
pip install -r requirements.txtpip install discord.py~=2.3.2pip install openai~=1.30.1pip install python-dotenv~=1.0.1- Create a
.envfile in the bot's root folder. (Same folder as main.py) - Format the
.envfile as follows, replacingTOKEN_HERE,OWNER_UID_HERE,CHAT_GPT_API_KEY_HERE,DISCORD_SERVER_1andDISCORD_SERVER_2with your bot's token, your Discord UID, ChatGPT API key and discord server ID's respectively. Keeping the double quotation marks.
BOT_TOKEN = "TOKEN_HERE"
OWNER_ID = "OWNER_UID_HERE"
GPT_API_KEY = "CHAT_GPT_API_KEY_HERE"
DISCORD_SERVER_1 = "FIRST_DISCORD_SERVER_ID_HERE"
DISCORD_SERVER_2 = "SECOND_DISCORD_SERVER_ID_HERE"
For example:
BOT_TOKEN = "12ab56c89"
OWNER_ID = "123456789"
GPT_API_KEY = "12ab56c89"
DISCORD_SERVER_1 = "123456789"
DISCORD_SERVER_2 = "123456789"
OWNER_ID, DISCORD_SERVER_1 and DISCORD_SERVER_2 must be a numeric whole value
- The
.gitignorefile will ignore the.env.
If you want to change the location of the .env file, you will need to make a reference for it by adding:
dotenv_path = os.path.join("path/to/env", ".env")above load_dotenv(override=True) and update load_dotenv(override=True) to:
load_dotenv(dotenv_path, override=True)If you want to change your .env file name as well add this reference to the .env:
dotenv_path = os.path.join("path/to/env", "Env_Name_Here.env")Open a new command line in the same folder as the main.py script (Make sure python is installed and/or your python venv is active) and type:
python main.py- Visit to the discord developer portal applications page Here.
- Click the
New Applicationbutton at the top right of the page next to your discord profile picture. - Name your application and click create. This will be used as the default username for your bot.
- Navigate to the
Botpage on the left navigation pannel as shown below:
- Change your bot's username under
USERNAMEif desired. - Click the
Reset Tokenbutton and copy your bots token for use in the.envfile. You will have to reset the bot token to view and copy it again. - Navigate to the
OAuth2page on the left navigation pannel as shown below:
- Select
botunderOAuth2 URL GeneratorSCOPESand selectAdministratorunderBOT PERMISSIONSGENERAL PERMISSIONS. - Copy the generated discord link under
GENERATED URLat the bottom of the page and paste this link into your web browsers address bar. - Follow the prompts to add your bot to any discord server where you have the
Manage ServerorAdministratorpermission.
- On Discord, go to Settings > Advanced
- Scroll down and make sure that Developer Mode is on
- Exit settings and left click on your profile picture at the bottom left of discord (same place as the settings button) and click
Copy User IDas shown below:
- On Discord, go to Settings > Advanced
- Scroll down and make sure that Developer Mode is on
- Exit settings and right click on the server(s) your bot is in
and click
Copy Server IDas shown below:
- Visit the openai playground website settings page Here.
- Click the
Create Projectbutton at the bottom of the settings list as shown below:
- Name your project and click the
Createbutton. - Navigate to the
API keyspage above the settings page on the left side navigation pannel. Alternatively you can click Here. - Click the
Create new secret keybutton. - Choose
YouunderOwned by. Name your API key something descriptive and select the new project you just created in theProjectdropdown. SetPermissionsasAlland click theCreate secret keybutton as shown below:
Ensure you have an available Credit balance. You can check on the Billing page in Settings or by clicking Here. If you do not have a Credit balance you will need to add money (credit) to your account otherwise this discord bot's chat GPT functionality will not work.


