Make a ChatGPT Discord Bot Using OPENAI API Keys (Full Code Here)
In this article you will learn how to make your very own personal ChatGPT bot that you can run in up to 100 different discord servers.
I did this project in a weekend and have a working ChatGPT bot in my Discord server that behaves exactly like chatGPT.com. Before this project I didn't know anything about OPENAI or any of their LLM models, I've also never made a bot before. So you can definitely do the same.
These are the main parts of this project:
STEP 1: Get API key from OPENAI
STEP 2: Set up base bot in Discord
You need to use their UI to start the bot building process. Get the Bot TOKEN key and copy and paste it into your .env file as a variable. If you want to share the code later you don't want any API keys floating around out there on the internet. Then just put the .env file in your .gitignore file so people can't rack up your API key calls and you receive a bill for $6,847 dollars next month in your email.
STEP 3: Add your bot to your Discord server
Once you get your bot built on the Discord developer platform you need to send an invite to the bot so that it will show up on your server as a member. You'' need to follow this example below and then go to that url and you will add the bot to your selected server. You can find the required keys on your developer portal.
This is the final objective of this part. To get the bot infrastructure onto your server so that we can start writing the Node.js server to host the bot.
STEP 4: Let's build the code to run the bot's Node.js server
Before we write the code we need these dependencies:
go do your working folder and initialize npm_modules so we can get these dependencies with npm init.
npm init -y
npm install dotenv discord.js openai
Now we can start building the code.
This is the code below. To get the bot online we need to run
node index3.js
Once the node server is running if we go back to discord we should see the bot online!
Don't forget to use the Discord permissions table to get a permission integer that represents "read messages" and "write messages" for our bot permissions.
领英推荐
permissions: 67584,
This is the code. I'll provide pseudocode below this image for what is happening.
This was giving me problems because in ChatGPT 3 to 4 updates some things changed with the library functions that we use to get the completion.
We need to AWAIT a response inside the async function. Below is the correct way to get the response into a variable.
// Get the ChatGPT response
const result = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: conversationLog,
max_tokens: 100,
});
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-3.5-turbo-0613",
"system_fingerprint": "fp_44709d6fcb",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?",
},
"logprobs": null,
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
If you need help you can refer to the docs here https://platform.openai.com/docs/guides/text-generation?lang=node.js
This is how I put the response into result.
message.reply(result.choices[0].message.content);
Conclusion
So that's basically it.
Now you can have your very own personal discord ChatGPT bot.
Send me a message if you need help. I will update this article later with a Github link with the code.
I graduate from Nova Scotia Community College - NSCC in 4 months with IT - Programming and want to connect with developers here in Nova Scotia so send me a message if you found this useful!
Follow me here on LinkedIn for software development articles www.dhirubhai.net/in/justin-bishop-32276075
Or YouTube for software videos www.youtube.com/justinbishop
Cheers,
Justin
#halifax #software #blockchain #web3 #ethereum #crypto #ai #softwaredevelopment #softwareengineer #oilandgas #careerchange
Senior Data Scientist | IBM Certified Data Scientist | AI Researcher | Chief Technology Officer | Deep Learning & Machine Learning Expert | Public Speaker | Help businesses cut off costs up to 50%
1 年Amazing project! Can't wait to check out the code! ??
That's impressive! Can't wait to check out the code.
Arabic Localization QA (LocQA | QA tester) | ex-Apple | Multilingual Expert in Localization Quality Assurance | Polyglot: Arabic, French, Italian, English
1 年Impressive work! Can't wait to check out the code and give it a whirl! ??
????Vom Arbeitswissenschaftler zum Wissenschaftskommunikator: Gemeinsam für eine sichtbarere Forschungswelt
1 年That sounds like an impressive project! Can't wait to check out the code. ??
AI Speaker & Consultant | Helping Organizations Navigate the AI Revolution | Generated $50M+ Revenue | Talks about #AI #ChatGPT #B2B #Marketing #Outbound
1 年That's impressive! Can't wait to check out the code!