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.txt
pip install discord.py~=2.3.2
pip install openai~=1.30.1
pip install python-dotenv~=1.0.1
- Create a
.env
file in the bot's root folder. (Same folder as main.py) - Format the
.env
file as follows, replacingTOKEN_HERE
,OWNER_UID_HERE
,CHAT_GPT_API_KEY_HERE
,DISCORD_SERVER_1
andDISCORD_SERVER_2
with 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
.gitignore
file 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 Application
button 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
Bot
page on the left navigation pannel as shown below:
![drawing](https://private-user-images.githubusercontent.com/99786431/331863273-1d95f080-b1a7-4be2-acce-c789237f5622.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1MjczNTQsIm5iZiI6MTczOTUyNzA1NCwicGF0aCI6Ii85OTc4NjQzMS8zMzE4NjMyNzMtMWQ5NWYwODAtYjFhNy00YmUyLWFjY2UtYzc4OTIzN2Y1NjIyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDA5NTczNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTlkNjhkZGJkZDgxNTFiY2EwNWJjYTQzZWJjMzg3YjE0NTkwMDMzYjdhMmZiMzZjNzZiMGZjMDVlMjE0YTgxNDQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.OQC_ZdovkX7lKbrrZw1iWhzXKEB3LAgOtFmocbjSZY0)
- Change your bot's username under
USERNAME
if desired. - Click the
Reset Token
button and copy your bots token for use in the.env
file. You will have to reset the bot token to view and copy it again. - Navigate to the
OAuth2
page on the left navigation pannel as shown below:
![drawing](https://private-user-images.githubusercontent.com/99786431/331863361-18dc9f85-380e-4dc3-b6e8-ba29f31ed5a8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1MjczNTQsIm5iZiI6MTczOTUyNzA1NCwicGF0aCI6Ii85OTc4NjQzMS8zMzE4NjMzNjEtMThkYzlmODUtMzgwZS00ZGMzLWI2ZTgtYmEyOWYzMWVkNWE4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDA5NTczNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTU5NTBhODQ0NDIxNmM2ZGIyOWYxYjA0NjZiNjc1YTJjMDBhNjI0ZmE5MjIwYTE5ODA4ZDVlZWFhYTE4OWY0NTcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.zfkG09SzRiDfnOBG6lCgwt1MTt9Zw28p5q0QUPPdHYU)
- Select
bot
underOAuth2 URL Generator
SCOPES
and selectAdministrator
underBOT PERMISSIONS
GENERAL PERMISSIONS
. - Copy the generated discord link under
GENERATED URL
at 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 Server
orAdministrator
permission.
- 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 ID
as 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 ID
as shown below:
- Visit the openai playground website settings page Here.
- Click the
Create Project
button at the bottom of the settings list as shown below:
![drawing](https://private-user-images.githubusercontent.com/99786431/331863639-867e01a4-e628-4a36-9436-a56f5c30c12d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1MjczNTQsIm5iZiI6MTczOTUyNzA1NCwicGF0aCI6Ii85OTc4NjQzMS8zMzE4NjM2MzktODY3ZTAxYTQtZTYyOC00YTM2LTk0MzYtYTU2ZjVjMzBjMTJkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDA5NTczNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTAyMDdkOGJkM2I3MmJmY2MxZDRjNzYxODRhMzYyMDg1NjYyZDI1YjFjZWRjM2EyNzcxNzVmMjRlMTU4MGJjMDAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.1QRv6KNf0HI4-9svSQMALiy31gK5eevybDr96oWyU0c)
- Name your project and click the
Create
button. - Navigate to the
API keys
page above the settings page on the left side navigation pannel. Alternatively you can click Here. - Click the
Create new secret key
button. - Choose
You
underOwned by
. Name your API key something descriptive and select the new project you just created in theProject
dropdown. SetPermissions
asAll
and click theCreate secret key
button 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.