How to Integrate ChatGPT to Your Own Discord Bot
February 24, 2023
ChatGPT is a powerful language model developed by OpenAI. This article will teach you how to integrate it with a Discord bot to help teams work more efficiently and foster communication and collaboration. We will show you how to do the integration without writing a single line of code. If you want to have customization, it is very easy to customize the integration by changing a few simple configuration settings
Introduction
First, we open source the code for the integration. Check it out at:ChatGPT-Discord-Bot. To use a ChatGPT bot in Discord. You only need to type /chat
in the input box, and a keyword /chat message
will be automatically inserted. You can directly enter text to interact with ChatGPT. In addition, we also integrated the DALL·E 2 model, so you can use /imagine prompt
to generate images through text, as shown below:
Integration Steps
Get Token
OpenAI Token
Register/login to your account on the OpenAI platform. You will need to provide basic information for your first-time registration
Click the profile icon in the upper right corner and select
View API keys
Click
Create new secret key
in the middle.
⚠️ Each API has a free quota and its limitations. For details, please refer to OpenAI Pricing
Discord Token
Log in to Discord Developer
Create a bot:
Enter
Applications
on the left sideClick
New Application
in the upper right corner and enter the bot's name. After it is confirmed, enter a new page.Click
Bot
on the left side.Click
Add Bot
on the right side.Click
View Token
. If you have already applied a token before, clickReset Token
Turn on
MESSAGE CONTENT INTENT
Click
Save Change
Set up OAuth 2
- Click
OAuth2
on the left - Click
URL Generator
on the left - Choose
bot
underSCOPES
on the right, and chooseAdministrator
underBOT PERMISSIONS
at the bottom right {' '} - Copy the URL at the bottom to your browser
- Select the server you want to join
- Click
Continue
and thenAuthorize
- Click
Project Setup
Fork the Github project
Register/login on GitHub
Enter ChatGPT-Discord-Bot
Click
Star
to support this repositoryClick
Fork
to copy all the code to your own repository
Deployment for Free
Go to replit
Click
Sign Up
and use yourGithub
account to sign up, and then clickSkip
to skip the initializationAfter you enter, click
Create
. A pop-up will show up. ClickImport from Github
on the top rightIf you have not yet added your GitHub repository, click
Connect GitHub to import your private repos.
, and then selectOnly select repositories
andChatGPT-Discord-Bot
Go back to step 4, now in
Github URL
, you can selectChatGPT-Discord-Bot
project. Now clickImport from Github
。
Start the Project
Environment variables setting
After you
Import
, go to theReplit
project management page. SelectTools
and then clickSecrets
。Click
Got it
in the bottom right, and add the environment variable- OpenAI API Token:
- key:
OPENAI_API
- value:
sk-FoXXXX (this is from the step above)
- key:
- Select the model:
- key:
OPENAI_MODEL_ENGINE
- value:
gpt-3.5-turbo
- key:
- ChatGPT The role words that the assistant wants to play (currently the official has not released more usage methods, and the players are tested by themselves)
- key:
SYSTEM_MESSAGE
- value:
You are a helpful assistant.
- key:
- Discord Token:
- key:
DISCORD_TOKEN
- value:
MTA3NXXX (this is from the step above)
- key:
- OpenAI API Token:
Execute the Project
Click
Run
on the topYou will see
Hello. I am alive!
if you successfully run the project. Now Copy URL on the top right⚠️ If you do not make any request within an hour, the program will stop running. That's why we need the following steps
CronJob
Sign up or log in cron-job.org
Click
CREATE CRONJOB
on the top rightEnter
ChatGPT-Discord-Bot
in theTitle
field using the URL copied from the last stepSelect
5 minutes
so that it will call the api every 5 minutesClick
CREATE
Commands
commands | what it does |
---|---|
/chat | Type /chat in the input box to and enter texts to interact with ChatGPT. |
/reset | ChatGPT will remember the first 10 interactions. Resets ChatGPT's memory with this command |
/imagine | Type /imagine in the input box, then enter the text to generate an image using the DALL·E 2 model. |
Q&A
How do I view log files? In the Replit project, there is a
logs
section on the left. The logger in the project will record logs there, and you can add your own logger to it.How do I adjust the image size of DALL·E 2 output? Modify the
image_size
parameter in/src/models/OpenAIModel
.
Conclusion
This article has taught you how to integrate ChatGPT with a Discord bot to create a powerful collaboration assistant. By following the integration and setup steps, you can easily start using ChatGPT in your team's Discord channels!