🚀 Create a Chat GPT Bot in Discord (with GPT-4)
ฝัง
- เผยแพร่เมื่อ 27 ส.ค. 2023
- 🚀 Create a Chat GPT Bot in Discord (with GPT-4)
Here’s how you can make a Chat GPT like Discord bot (with support for GPT-4) that works in multiple channels!
🧠 Resources
Source code (private): / underctrl
Node.js: nodejs.org
Visual Studio Code: code.visualstudio.com
Username regex: msg.author.username.replace(/\s+/g, '_').replace(/[^\w\s]/gi, '');
🌐 Important links
Newsletter: devnotes.underctrl.io
Discord: discord.underctrl.io
Github: github.com/notunderctrl
Patreon: / underctrl
Buy me a coffee (one-time donation): buymeacoffee.com/underctrl
Thanks for watching! 💜
If you'd like to learn how to host your Discord bot, I have a tutorial on that: th-cam.com/video/f7KjJQVcogY/w-d-xo.html
how to add langchain with knowledge base please
its not working anymore right?
Holy this tutorial is awesome. It's crazy how well you explain everything, and it all just works.
dude this is the first one that worked and ive been looking for months! THANK YOU
That's the only tutorial that worked for me so well, Thank u so much U ARE A LEGEND !!!
You are a legend, quite strange to be the best to explain discordJS v14 on TH-cam but just having 4k subs. I guess you need to make TH-cam a new algorithm for that.
🙏
Man, you did a great job in this tutorial. Now I just need to dive a little deeper to make the experience more personalized. Thank you.
Hey man! Did you find a way to make this more personalised? Thanks & Happy New year!
Under Ctrl you're such a great dev!
Love your vids bro you make the bot from the start showing everything
one thing you missed, add a name to your bot sa every user call that name and the bot will response directly to that user
Very cool! thank you for the solid tutorial!
thumbs up for the video! It helped me accomplish the integration. Thank you!
Really awesome video. 😃
Love this tutorial ❤
for those who got an error with "error is not defined" => we just deleted that line in the script and it works. Hopefully it works for you as well!
It's important to be aware that gpt4 charges more for each token than gpt3.5 turbo does. A lot more in fact. So if you want your bot to be used for simple communication purposes there is no reason to use gpt4.
Got to 12:19, where I send a "hello" message in the channel and get no response from the bot, and the error message is not displayed. I can't understand what the reason is!
Same!!! Sorry I couldn't help but hopefully this comment moves up.
You need to load up your chat gpt account with credits! I had the same issue
Nice vid
Thank you for the nice tutorial! Is it possible to let this bot join a discord call and include TTS?
Thanks
Hey man, great video! Wanted to ask, how can we adjust the bot to use custom GPT Assistant that I "fed" with my own json data?
Hi man, have you found a solution ? I am facing the same problem rn, thanks in advance
insane
when restarting the bot i have the error that says The OPENAI_API_KEY environment variable is missing or empty, i had put OPENAI_KEY in my .env file already but it still remain the same, pls help.
Thank you for making this tutorial. You explain the code excellently well. If anyone is trying to get the bot to limit messages to only 2,000 characters, the following works:
message.reply(response.choices[0].message.content.substring(0, 2000));
PRO: node.js doesn't crash
CON: The message is cut off.
I'd like to figure out a way to return a text file if the message is longer than 2000 characters.
Appreciate the kind words!
In this video I have shown near the end how to split a long result into multiple responses to avoid the bot from crashing while still sending the full response to the user.
About your approach, even though it works, here's why I wouldn't recommend it:
- Extra message length is cut off which is just a waste of OpenAI tokens (as you've mentioned as well)
- Your model won't have enough context on uncompleted text.
- Sending back as a text file will require extra configuration to not only send back, but also for the model to read from.
However, if you'd like to stick to your approach, one way you could send the response back as a text file is by converting the response to a buffer and then sending it back using the AttachmentBuilder class in discord.js.
// example snippet
const response = "This will be your response from OpenAI";
const buffer = Buffer.from(response);
const attachment = new AttachmentBuilder().setFile(buffer, 'response.txt');
await message.channel.send({ files: [attachment] });
@UnderCtrl Got to 12:19 where I message "hello" in the channel and get no response from the bot, and no error message displayed. Any idea what I might be missing? have been troubleshooting for hours, bot it online and code updated
I am also going through the same problem my code is not working and I dont know why
im pretty sure your openai key was incorrect
Is there a way I could import an entire custom personality to the bot? I would like it to have a memory of information I give it.
Great video, I was able to get a bot up and running! Is there a way to make it able to reply to DMs?
openai api that is no longer open, i didnt even get to use it and my 18$ exzpired 3months ago :(
I'd like to add a character personality and memory to the bot, any chance of a follow up tutorial for a simpleton like me?
hello ,i have followed the instructions but at 12:18 when restarting the bot i have the error that says The OPENAI_API_KEY environment variable is missing or empty, i had put OPENAI_KEY in my .env file already but it still remain the same, pls help.
Make sure you installed dotenv with `npm install dotenv` AND (more importantly) wrote `require("dotenv/config")` at the top of your index.js file.
W
Hi! I'm currently at 6:23 When I type the node index.js command, it says code:'TokenInvalid' is there anyway to fix this?
does your .env file have your bots token? if it does then look at your client.login(process.env.TOKEN) I had this same issue because i just replaced my token with process.env.token and left the ' things you need to remove them
Hi can you post a tutorial about chatgpt bot trained on our data, according to open ai recently posted blog post,
What about the api?? We need gpt 4 unlimited api usage.. I already have even image generator.. gpt 3.5 unlimited usage and gpt4 but limited usage..
Thank you very much! My research can proceed well because of your video!
Glad the video was helpful. Thanks a lot for the “super thanks”! ❤️ (weird name choice youtube)
Do your server have the bot @@UnderCtrl
anyone knows how to code the bot in the way that he o ly repsonds and reacts when you directly @ him in a chat? like clyde did the chat bot from discord before the took him down
If I created a GPT 4 with customized data (Knowledge), then how could I integrate it into discord? Is it possible and how?
Is it just text responses, or images too?
Cannot get past 18:27 with out getting this error.
node:events:497
throw er; // Unhandled 'error' event
^
ReferenceError: prevMessages is not defined
at Client. (X:\vs projects\gptbot\index.js:40:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:402:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
you forgot to initialize the variable prevMessages most probably
just check if you wrote the line
let prevMessages=[ ];
does it also support gpt-3?
what if u put my own token in there? Would it make it selfbot?
Hey UC, can u show me how to add another languages to the bot ?
I think this video needs an update as since a few days I always get the message that there are Api issues, I generated a new API and it still doesn't work, OpenAI must have changed something
Can you have the bot search a website for the question and respond back with a formulated answer?
Im getting this error: "SyntaxError: await is only valid in async functions and the top level bodies of modules"
This error means that you are running a line of code with 'await'. This only works with async functions which return a promise. You can make the entire message event listener async by writing async before declaring the (message) parameter. Demonstration:
client.on('messageCreate', (message) => {
// your code goes here
})
The important part is that your event listener has async before declaring your parameter to make the entire function async, thus allowing you to use the 'await' keyword. Hopefully this helps!
my bot thinks that everything I say is inappropriate :(
I changed 3 accounts but still getting error 429, while registering the new phone number with new gmail for api key there is always a pop up saying "this number is already associated with different account." Like seriously!! I am literally using a new number and it says that number already exist.
Paying for chat gpt plus is different than paying for the API service which is for developers to be able to use the api, make sure you are paying for that too, im 90% that aslong as you pay once for it you are allowed to use chat gpt 4 in projects forever like this guy says in the video, hope you figured out the problem by now
I can't figure out the error 5:33
SyntaxError: Identifier 'Client' has already been declared
I have same error ...
@@narajulyameans you have const Client, but it should be const client without a capital C. Client is defined by Discord.js
under ctrl, can you help me? when i write node index.js it says "SyntaxError: Identifier 'client' has already been declared"
it's been 'declared'
Hi sir I want help from you to create a command please
Basic question here how did you move the line of code at 11:53 ?
That's what I wanna know. He changed something and didn't show us what he did!
@@loren8888 probably the "Prettier" extention
when i did node index.js it didnt work any solution ???
install it
hi ,i made a discord ai chat bot using python and openai api key(about 6 months ago,didnt watch the whole video cus i dont know much js and never leant) so my question is do you need to buy OpenAI subscription in order to work or you can do this with a free Api key ?
When creating an OpenAI account, you usually get $5 worth of free credit, but it doesn't work on GPT-4 unfortunately. You'll have to switch to a pay-as-you-go plan in order to get that to work.
ohoo okay ,thank you for the response tho! i made the bot for fun so its fine .@@UnderCtrl
Do you need to run the script every time to make it work on Discord? Please advise. Thank you!
same question
There is only one last step missing. How do i use my own trained chatGPT bot? Is there a way to use the link? Because i think the openAI API can't generate a token for it...
If you figure this out, let me know !!! Would love to figure out how to insert a custom one that has been trained
when I started it, it said Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
hey is it possible to make the model not just gpt-3.5-turbo i have other 3.5 models how do i incorporate that?
Do you mean a custom trained model? If so, you can use the model name you get in your email after your model has finished training (e.g. ft:gpt-3.5-turbo-0613:personal::7f9g2) instead of "gpt-3.5-turbo".
@@UnderCtrl thank also one more thing how do i make so others cant abuse it for content policy so i dont get banned?
It doesnt work
When i type node -v it just doesnt recognize it
you need to set node.js as environment variable
doesn't work like every other code in this yt channel
When I try ping from the discord its not getting picked up on the terminal. Any ideas?
same issue here. Anybody has some insight?
Best model for role.playing?
I’m sure you can use either one just fine. What primarily matters is the instructions you give your bot if you want it to behave a certain way.
Now i had tried for 3 hours. and see if i could sort it out but still getting the 496 error
throw er; // Unhandled 'error' event
^
ReferenceError: CHANELS is not defined
something you know about?
Chanels?
If i type in "node index.js" nothing happens?
same. have you solved the problem
did you save your code then run it?
bot name not found error
Woww, this so good. Can it online for 24/7 ?? Or this need an host and running on discord js pterodactyl software??
You can keep it online 24/7 by hosting it. I have a bot hosting tutorial: th-cam.com/video/f7KjJQVcogY/w-d-xo.html
i need to buy api?
Please friend, can you give us the code ?
i would be happier if we got spoon fed ☠
how can I make this bot online forever
You'd have to host your bot on a server that stays up 24/7. I've made a tutorial on this: th-cam.com/video/f7KjJQVcogY/w-d-xo.html
@@UnderCtrl i am experiencing a issue that i do not get ping message in terminal. what is your discord?
who have the code ?
5th
Is it necessary to pay for it? If it is, is there any free version?
I believe when signing up for OpenAI you get $5 worth of free credit. You should keep in mind that it won't work for the GPT-4 model since that requires you to have paid OpenAI at least once if I remember correctly. You can however used GPT3.5 Turbo just fine.
@UnderCtrl would we implement gpt3.5 turbo any differently or are we only swapping out one line?
even for 3.5 turbo, you need to pay. otherwise you will get "insufficient_quota" error.
Is it possible to have the bot use a custom GPT?
Did you found anything? Would love to try the same.
@@MrYTThor Yes I did! Good luck funding it! lol jk. So what you want to do is use an OpenAI Assistant instead of a custom GPT. They are virtually the same but have different user interfaces and API
This video walked me through the process. You'll be able to build off of it pretty easily.
th-cam.com/video/5TU_wOC0dmw/w-d-xo.htmlsi=Wb9WHHoK1LLPUEci
Good luck!
@@HunterLawson_GameDev thanks a lot. I found out about assistants but I almost give up because the ai keeps forgetting stuff. I have a txt with a background story, but after a few chats, it forgets it. I managed to find out that I have to store all the data in a table and there does my knowledge stops.but I hope yours will do as you hope for. Thank you and good luck 🤞🏻
First one ?
yes
IS it 100% free?
Nope. You have to pay for the API
damn thats why mine isn't works@@UnderCtrl
dont waste your time not working erorr
"not working error"
Very helpful 🤝
@@UnderCtrl yea i do step by step and not working only waste time
@@jakuba6298 this video is not for people who don't know the basics of JS
@@UnderCtrl got 429 error, said it exceeded my current quota, do i really need to pay for it or is there any fix
@@cilvania you have to pay for it unfortunately
i get this error: PS C:\Users\...\Desktop\AT> node index.js
C:\Users\...\Desktop\A\index.js:5
const Client = new Client({
^
SyntaxError: Identifier 'Client' has already been declared
at internalCompileFunction (node:internal/vm:77:18)
at wrapSafe (node:internal/modules/cjs/loader:1288:20)
at Module._compile (node:internal/modules/cjs/loader:1340:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49
Node.js v20.11.1
PS C:\Users\eriks\Desktop\ASPHIX CHAT> node index.js
C:\Users\eriks\Desktop\ASPHIX CHAT\index.js:5
const Client = new Client({
^
SyntaxError: Identifier 'Client' has already been declared
at internalCompileFunction (node:internal/vm:77:18)
at wrapSafe (node:internal/modules/cjs/loader:1288:20)
at Module._compile (node:internal/modules/cjs/loader:1340:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49
Node.js v20.11.1
pls fix 😭
idrk how to
it says this error for me at 5:31
Error:
C:\Users\MyName\OneDrive\Computer\Bot(maybe bots)
ode_modules\discord.js\src\client\Client.js:214
if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid);
^
Error [TokenInvalid]: An invalid token was provided.
at Client.login (C:\Users\MyName\OneDrive\Computer\Bot(maybe bots)
ode_modules\discord.js\src\client\Client.js:214:52)
at Object. (C:\Users\Edvards\OneDrive\Dators\Bot(maybe bots)\index.js:13:8)
at Module._compile (node:internal/modules/cjs/loader:1275:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
at Module.load (node:internal/modules/cjs/loader:1133:32)
at Module._load (node:internal/modules/cjs/loader:972:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47 {
code: 'TokenInvalid'
}
Please help.
Oh nvm i fixed it i didnt save the .env heh
@@ShowNoRespect- thx i thought i had done that already but apparently i didnt either
G:\chatgpt\index.js:16
const openai = new openAI({
^
TypeError: openAI is not a constructor
at Object. (G:\chatgpt\index.js:16:16)
at Module._compile (node:internal/modules/cjs/loader:1368:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
at Module.load (node:internal/modules/cjs/loader:1205:32)
at Module._load (node:internal/modules/cjs/loader:1021:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
at node:internal/main/run_main_module:28:49
Anyone help with this error?
the arrow is pointing under n in new
It's `new OpenAI` not `new openAI`
You're talented! We should work together. Can I email you?
Thanks. You can reach out to me at avraj@underctrl.io