Create a ChatGPT AI Discord Bot 🤖🔥 (READ PINNED COMMENT)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going everyone so open AI recently released the model they used for chat GPT as an API service it's called GPT 3.5 turbo and the best part about this new model is that it's 10 times cheaper than the DaVinci model which we have previously used to create a similar chatbot don't worry though you don't have to pay anything in order to start using this API in this video I'm going to show you how to make use of this new model in node.js to create a Discord chatbot like this if you guys want to see a live demo of how this chatbot works I have it set up and open for everyone in the chat GPT channel of my Discord server before we start make sure you have node.js installed because we're writing server-side JavaScript and I'm going to be using visual studio code as my code editor of course because we're writing JavaScript code make sure you know at least the basics just so you're not left scratching your head anyway let's get started by first creating our Discord bot account so head over to the Discord developer portal which you can find at Discord dot com slash developers slash applications from here go ahead and create a new application I'm going to call my application GPT 3.5 and make sure to accept the terms of service and click create once you have your application created go to the bot section on the left hand side of your screen and click add bot then click on yes do it this should create your Bots account from here you can go ahead and give your Bot in Avatar and change its username however I'm just going to leave this the way it is next scroll down and I'm going to disable public Bots turning off this option means only I can invite this bot this is your personal preference however next scroll down further and enable all three of these intense options these options will make sure your Bot is receiving all the required information from the Discord API now we're going to be inviting this bot to our server so make sure you have a server ready for testing your new Discord Bots to invite your Bot head over to the oauth 2 section on the left 10 side of your screen and click on URL generator under Scopes go ahead and choose bot and if you're going to be adding slash commands to your Bot make sure you also enable application.commands now scroll down and select the permissions you want your Bot to have of course I only recommend giving it the permission to send messages because if it does have enough permissions then people can potentially abuse your Bot and make it ping everyone this has happened in my server once but luckily it was my friend who caught on to it and notified me anyway copy the link that you get down here and what I like to do is head over to Discord and paste the link then click on this and invite the bot directly from here you'll notice now the bot has joined your server and if you click on it you'll notice that it's offline don't worry we'll work on that in just a moment with your Bot added to your server we can go ahead and deal with our open AI accounts so now head over to platform.openai.com and from here first of all go ahead and sign into your account of course go ahead and register if you haven't already and after being logged in click on your account and click on manage account here you'll notice we have a free trial with 18 worth of free credit now this may not seem like a lot to you but this was already a decent bit for a free trial with the previous DaVinci model you'll notice when the new model was announced I switched to it and my usage dropped drastically even though my bot handles a couple hundred messages every day so it's safe to say that 18 worth of free credit is pretty good and it should be enough for most applications we're going to come back to this and our Discord developer portal in just a moment so go ahead and minimize your web browser now go ahead and open up your favorite code editor I'm using visual studio code which I mentioned at the start of this video and once you're here go ahead and open up a folder which you can do by clicking on file and open folder I'm going to create a new folder on my desktop and I'm going to call this GPT 3.5 go ahead and click open once you have opened up your folder in vs code go ahead and open up the terminal which you can do by clicking on view and clicking on Terminal but I will be using the shortcut throughout this video now let's initialize a new npm project by typing npm init Dash y we'll now install a few packages so type out npm install discord.js open Ai and Dot EnV the installation will take some time depending on your internet speed once it is downloaded go ahead and create a new file in the root directory of your project called dot EnV here we're going to store all our environment variables the first thing we're going to add is token which is going to be our Discord spot token so head back to the Discord developer portal go back to the bot section on the left hand side of your screen scroll up and click on reset token once you reset your token go ahead and copy this then head back to visual studio code and paste it over here we're also going to create another variable called API key and this will be our API key from openai so let's go back to our browser and go back to open AI from here go to API keys and click on create new secret key now go ahead and copy this key and back in your code editor go ahead and paste this in the dot EnV file these values are important information so make sure you don't share it with anybody else after this video is done I'm going to go ahead and reset both of them we also have to create one last variable called Channel underscore ID and we're going to set this to the ID of the channel where we want our AI bot to work so I'm going to go back to Discord and let's say I want my bots to work in this channel so I'm going to right click this and click copy ID head back to our code editor and I'm going to paste the ID over here so save your file and you can close out of both the files that we just opened and now in your root directory go ahead and create a new file called index.js so the first thing that we're going to do is we're going to import dot EnV config using require after this we're going to import some stuff from discord.js the first thing that we need to import is client and the second thing is intense bit field now I'm going to go ahead and Define a variable called client and I should mention that some people like to call this bot the choice is yours to be honest and we're going to set this equals to a new instance of the client class now pass in an object and the first property is going to be intense and this is going to be an array we're going to pass in first of all intense bit field dot Flags dot guilds this will give us access to the guild information that our bot is in Guilds by the way in Discord development means server anyway we can go ahead and duplicate this now and we can repeat this for Guild messages and this will also give us more information on every single message sent inside a server we can duplicate this one more time and we're going to use this for message contents and as the name suggests this will give us access to the message content itself we can now go ahead and add an event listener which will be triggered every time the bot comes online so let's client dot on and the event name is ready so add a function over here and inside this function we're going to go ahead and console log and say the bot is online after this make sure to log into the bot using client.login and if you remember our token is actually stored inside the dot EnV so to use that we're going to do process.env dot token however this is not going to work if you did not import dot EnV slash config so let's save this file and this code should work so open up your terminal and type node index.js in the terminal we get the bot is online and if we go back to Discord our bot is actually online so now let's go back and listen for messages so back in our code I'm going to add another event listener so I'm going to say client dot on and the other event is message create and this will give us access to the message object as the parameter and what we can do is we can go ahead and also log this message object and see what we get I'm going to save this file and restart my bot using Ctrl C and then using node index.js again once our bot comes online I can go back to Discord and here I'm going to send a message saying let's say ping now if I go back to vs code and you'll notice we get access to the message object itself if we scroll a little bit up you're gonna see we get the message content saying ping so now we know what property to access so let's go ahead and close out of this terminal and what we're going to do now is we're going to configure our open AI so let's go ahead and first import openai using require and from open AI I'm going to import first of all configuration and the next thing is open AI API now right before the message create event listener I'm going to define configuration using const configuration and I'm going to set this to a new configuration class and inside I'm going to pass in an object and the thing that we're going to pass in is the API key if you remember our API key is stored as API underscore key so let's go ahead and copy that and inside index.js we can see process.env.api underscore key now we can use this configuration to communicate with the open AI API so let's go ahead and Define a variable called open Ai and we're going to set this to a new instance of the open AI API class and we're going to pass in the configuration now we can use this variable to communicate with open Ai and generate a response however before we generate a response we need to first run some checks so I'm going to say if message author bot return this basically means if the message author was a bot then we're going to completely return from the function and I'm going to do the same if the message Channel ID does not equal to this channel ID so let's say if message dot Channel dot ID does not equal to and then we're going to pass in process.env.channel underscore ID and if that's the case we can return again and finally I'm going to add a way for people to be able to chat in that channel normally without having to trigger the bot every time we can do that by using some sort of prefix so let's say if message dot content dot starts with and over here I'm going to add an exclamation point of course you can add whatever you want but if a message starts with an exclamation point in this channel then we're going to completely ignore that message now let's go ahead and Define a conversation so I'm going to say let conversation log and this is going to be an array of objects the first object is going to be the initial social context that we want to give our bots so the role is going to be system and the content is going to be whatever you want to explain to your Bot so I'm going to say you are a friendly chatbot and if you guys want your Bot to be a little bit more sarcastic like my bot then you can basically tell it to be sarcastic over here now what I'm going to do is I'm going to push the content of this message into this conversation log so I'm going to see conversation log dot push and I'm going to push an object the first property is going to be the rule which in this case is user and the content is of course going to be message dot content now before we send an API request to open AI let's make our bot pretend that it's actually typing so I'm going to say await message.channel dot send typing and this is a method and because we're using a weight our function has to be asynchronous now after we send a typing status we can go ahead and Define results which is going to to be a weight open AI dot create chat completion and in here we're going to pass in an object and the first property is going to be the model and as I mentioned before the model is GPT 3.5 Dash turbo the next property that we need to pass is messages and this is an array of messages if you remember we created conversation log so let's go ahead and pass it in over here finally we can go ahead and reply to this message using message dot reply and we're going to pass in the result dot data dot choices and because choices is an array let's go ahead and get the first element and we're going to get the message from it now go ahead and save your file and if we now run our bot using node index.js once the bot is online let's go to Discord and test our bot out I'm going to try saying hey how are you and our bot is going to first pretend to be typing and once the response was generated rated our bot is going to reply with it and it does seem to be working perfectly fine now what we can do is we can go ahead and make it hold conversations with people so the way we can do that is we can fetch previous messages based on the person who sent the latest message back in our code what I'm going to do is I'm going to remove the send typing actually I'm going to cut it and I'm going to go ahead and put it right after defining conversation log and then after that what I'm going to do is I'm going to Define a variable called previous messages and we're going to set this to await message dot Channel dot messages dot Fetch and we're going to pass in an object with a limit of 15. this means our bot is going to fetch 15 of the previous messages to add to this conversation log now we're going to go ahead and reverse these previous messages because they are sorted in latest to oldest order so I'm going to go ahead and say previous messages dot reverse and we can now Loop through all of these messages and push them to the conversation log so what I'm going to do is I'm going to save previous messages for each and we're going to refer to each of the messages as MSG basically short for message and as we did here before if the message content starts with an exclamation point then we want to ignore that completely so let's go ahead and add that line of code now what we want to do is we want our bot to ignore other Bots but not itself so the way we can do that is we can say if message author ID is not equal to client user ID and message author is a bot if that is the case we can return finally because we're trying to hold only a single conversation with one person we can say if message author ID does not equal to message author ID then we can return as well so if a new person is trying to send a message we're not going to be using anybody else's user ID in this conversation log however you can feel free to of course modify this to your liking but this is all the conditions that we have so what I'm going to do is I'm going to go ahead and push this to conversation log using conversation log dot push and then we're going to push it as an array the role is going to be user and the content is going to be message dot content and outside of this for each method we can go ahead and remove the previous conversation log push method now I can go ahead and save this file and restart our bot now I'm going to go back to Discord and ask it what was my previous question so it did correctly answer by saying hey how are you was my previous question which it was in this case so anyway I really hope you guys enjoyed this video If it helped you out make sure to give it a like if you guys are having any sort of problems be sure to join my Discord server and ask your questions in the discord.js channel anyway thanks for watching and I'm gonna see you guys in the next one
Info
Channel: Under Ctrl
Views: 196,260
Rating: undefined out of 5
Keywords: discord, chat gpt, chatgpt, bot development, discord.js, discord.js v14, chatgpt tutorial, new api, chatgpt api
Id: CB76_GDrPsE
Channel Id: undefined
Length: 15min 43sec (943 seconds)
Published: Wed Mar 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.