How to use ChatGPT on Discord - AI Chatbot Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
chat GPT can be added to Discord and in this video I'm going to show you how you can do this and there's lots of opportunity here I've already had so much fun interacting with it myself either on my computer or even just on my mobile phone typing in prompts whenever I want as well as my community members having a lot of fun just chatting to it I'll structure this video in three parts first we'll set up node.js to interact with Discord and open Ai and its API second we'll set up Discord with the bot so that it can engage with community members and third we'll connect the two I don't have any sponsors so I'm going to sponsor myself in this video I built out this entire chat bot so you guys can download and use at any point I built out additional features as well I'm gonna list it on gumroad for 49 if you want to save yourself some time and try it out then you can do so at the link in the description below and with that said and done let's begin I'll create a new folder called chat GPT Discord chatbot then I'm going to initialize a new project by running npm init Dash y this will create the package.json here I've got all my modules installed or at least I will in a sec I'm gonna install three packages I'm gonna run npm install I'm going to install discord.js open Ai and Dot EnV next I'll create the file dot EnV as well as index.js which is where our server and configuration will lie finally I'll check that I'm running the latest node version which should be version 18 as well as my npn version which currently is version 8.8 now assuming you have Discord installed go to the server section and create a new server for yourself or for your community I'm going to call This Server the chat GPT Discord chatbot which is essentially what we're making once this is done I'm going to open up Chrome and head to a special URL called discord.com forward slash developers forward slash applications I'm going to create a new application this application I'm going to give the name chat GPT Discord chatbot and select to apply the terms and conditions this new application now can be customized the first thing I want to do is update the Avatar I want an avatar here that represents chat PT I created this earlier in figma next I want to create a bot for this application I'm going to select bot on the left hand side and I'm going to select to add a bot on the right side when I add a bot I do need to give it a name and you can't have the word Discord inside of your bot name so I do need to go back in my general information and change the name for this application to remove that word Discord once I've done that I can go back and save my changes and then select to create the bot once more I've successfully created my bot I do need the security token in order to use it properly I'm going to reset this token and capture the new token so I can start using it what I'll do is reset the token here and then I'm going to do two-factor authentication and then once I've done that I can grab this token and I can paste it inside of vs code so I'm going to open up the VS code project on the dot EnV file and I'm going to create a new variable here this variable will be Discord underscore token now I'm going to paste the value here for that token I also want to create a few other placeholder values here the open AI organization and the openai key for the API key with that done I want to scroll down to privileges for the Gateway intents and I want to enable message content intent this allows the bot to receive message content in most messages and then reply to it later on after saving those changes I just need to authenticate the bot I'm going to head to oauth 2 and I'm going to update the default authorization link here I'm going to select the option custom URL and I'm going to paste the following URL in which is discord.com forward slash auth2 authorize with a scope permissions and client ID here the client ID I'm going to grab from my client information and just place that in then I can copy that entire link I can open that up in Chrome and I should be able to now authorize my own bot to run in my own server I'm also going to give this bot permissions as an administrator and this will allow it to do different things like application commands if I want to add those later find finally I verify that I myself am not a bot and now if I visit the channel I'll see that Chad GPT bot has appeared has joined the channel but is currently in an offline State it's now time to create the node.js server that will interact with the Discord bot I'm gonna head to my index.js file and I'm going to explain what I'm doing so GitHub copilot can better do it here I'm going to create a Discord bot using the open AI API that interacts with the Discord server first I'm going to require dot EnV this will allow me to use the dot EnV file inside of my server now I want to configure Discord to be used as part of this bot I'm going to pull out two values I'm going to pull out client as well as Gateway intent bits from the discord.js module which I will require afterwards to initialize the client I'm gonna cons client equals new client here as a class and I'm going to pass in the intents that I want to use for the intents I'm going to have an array here and I'm going to pass in the following which is Gateway in intentbits.guilds.guildmessages and Dot message content now I'm going to move on to the open AI configuration for the API this is the most up-to-date configuration so I'm going to const and I'm going to pull out the values of configuration as well as open AI API and these I'm going to require from the new package open AI I'm going to initialize the configuration by calling in cons configuration equals new configuration with capital c I'm going to pass in the organization which I'm going to pull out from the environmental key that I earlier set as well as the API key which I'm also going to pull out of the environmental key I can now use this configuration to create a connection to open Ai and its API here I'm going to const out open AI equals new open AI API and pass in the configuration moving on to Discord I'm going to use the Discord command line dot on message create this will be an async function that will be pulling out the message from the messages that come from Discord users for this I want to do a try catch Loop so that in case there are any errors I can log them out here if I catch an error I'm just going to console log it and now I want to do a ping pong message where if a user sends a message I get the bot to essentially say the message back to me firstly I'll consult out a message a user sends by doing console log message.content then I'm going to do a reply of the same content in the reply I'll say you said with the message content itself and now we can connect this spot to the Discord server this is the easy part I'll call client.login and pass in the environmental key for the Discord token I might as well also console log that the chat GPT bot is now online on Discord so that I know once I start the server since I don't have my API keys from open AI yet I'll just comment out that bit of code to test out this message create code I'm going to run the server by calling node index.js and it should tell me that it's working when I get the chat GPD bot is online I can now check this let let me go into Discord and see and there it is we've got the chat GPT chatbot up and running it's got the green circle and here if I test add a message saying something like high chat GPT how are you there hopefully I get a response of a ping pong message and uh yes it continuously is pinging even itself so I better quickly cancel that Discord server and I'm gonna fix that up I want the bot to ignore messages from itself so I'm going to pass in if the message.author.bot is well essentially a bot or it's true then it just returns this function so that it doesn't continue on to the second or additional lines making sure that the bot doesn't respond to itself or other Bots can be a good practice to have in making any type of Discord bot now I'll relaunch the server it should be now online and I'm going to give it a test let's hope that this works now and I should get a single reply which I have without any additional ping pongs next I'm going to head over to the open AI website I'm going to log into their dashboard and I'm going to pull out the API key and the organization key I'm going to head to the top right and select a view API keys and here I'm going to generate a new secret key which I'll dispose of later after this video I'm gonna head back to vs code and I'm going to head over to the dot EnV file and paste it here under open AI underscore key I also need the organization ID so I'm going to head over to settings and I'm going to head to the organization ID over here and copy and paste that and also place it into the vs code file here under the variable open AI underscore org after that I'm gonna head back to my index.js file and I'm going to uncomment this open AI configuration with all the keys and organization set this is now ready for use so I can remove this console log as well as this message and start crafting a new one I'll begin with const GPT response and here I'm going to await a response from openai after I do a create completion this completion will be an object and I'm going to pass in a few values here I want to pass in the model I'll be using in this case I'm going to use the model DaVinci the next item is to pass in a prompt The Prompt is the more important part so I'll fill this in with a blank string for the time being and get back to it Max tokens will be set to 100 and I'm also going to pass in a stop sequence so whenever the word chat GPT or my name pops up the AI will stop writing more content finally I always like to put in a temperature and this is basically how random the AI is with a value of one being quite random now to fill out the prompt I'm gonna pass in chat GPT is a friendly chat bot this explains to the AI what it's doing next I want to give it a few examples of how it's interacting I'll start off with a basic message here from myself or actually maybe from the chatbot itself I'll pass in here chat GPT and then a message such as hello how are you the response will then be from either myself or actually even just from a member from the Discord channel so in this case I can pass in the message.author.username which in this case will be Adrian if I'm sending a message on Discord with the message content being the value message dot content then I'm going to break the line I'm finally going to finish off this prompt with a chat GPT as well as a blank item here so this is where it begins its response next I'll update the message reply the message reply will be coming from chatgpt as a bot so all I need to put here is the response that we get back from open AI here I'll pass in gptresponse dot data dot choices index 0 dot text and that is it I can now do a return because this is now ready for use in the terminal I'm gonna run node.js by calling nodes space index.js this will start the chatbot and I can jump back into Discord to test it out so the Moment of Truth let me ask a question here about web design and see if it comes back with a response and success looks like it's working it's taken in the message and the content and is replied with a message that is related to that and specifically to me as well this means that I should be able to continuously ask questions like are you a chatbot and I should continuously get responses this is just the beginning of what chat GPT can do as a Discord bot I've put together a package that you guys can check out in the link in the description below it's going to be additional features and you can continue to build it out yourself as well otherwise don't forget to like the video and I'll see you in the next one thank you
Info
Channel: Adrian Twarog
Views: 277,105
Rating: undefined out of 5
Keywords: chatgpt discord, chat gpt, discord, chatgpt, chatgpt discord bot, discord bot, discord chatgpt, tutorial, openai, ai, open ai, chatgpt discord tutorial, chatgpt discord server, nodejs, js, javascript, chatbot, discord chatbot
Id: roMykVsig-A
Channel Id: undefined
Length: 11min 35sec (695 seconds)
Published: Tue Jan 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.