Make a ChatGPT Discord Bot Using OPENAI API Keys (Full Code Here)
Make a discord bot that runs ChatGPT

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:

  • You need an OPENAI api key. Then fund $10 to the account and you will get access to the API's. https://openai.com/. Make an account.
  • Set up the bot scaffolding on Discord, then get the bot TOKEN key.
  • The backend that runs the bot is using Node.js with these dependencies. dotenv, discord.js and openai
  • AI is moving fast so by the time you read this, the OPENAI chat completion JSON objects or the library function names might change. So you need to refer to the OPEN docs. https://platform.openai.com/docs/api-reference

STEP 1: Get API key from OPENAI

This key is only visible upon creation and then hidden.

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.

https://discord.com/developers/applications

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.

Once you get your bot build on Discord Developer Portal, this is how you add the bot to a server.

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.

This is our bot. It's sleeping right now.

STEP 4: Let's build the code to run the bot's Node.js server

Before we write the code we need these dependencies:

  1. dotenv (lets us hide our API keys in a .env file)
  2. discord.js (we need the connection library to Discord)
  3. openai (we need the connection library to OPENAI)

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.

our node.js bot code

  • So up top we declare dependencies and make the client connection with discord.
  • Next we make the connection with openai with our api key hidden in the .env
  • Next we make a async request to the Openai API with our discord message query that is initialized with a '/'.
  • This is the chat completion from openAI response that we get back in JSON. Notice how choices[0] is our response...

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

our bot works now


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


Mohammed Lubbad, PhD ??

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.

回复
Mohsene Chelirem

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! ??

回复
Udo Kiel

????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. ??

回复
Alex Carey

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!

回复

要查看或添加评论,请登录

Justin Bishop的更多文章

社区洞察

其他会员也浏览了