Intelligent Discord AI Chatbot in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to build an intelligent ai chat bot for discord so let us get right into it alright so in order to create an intelligent ai discord bot we need to have a discord bot in the first place so what we're going to do is we're going to go into our browser and we're going to navigate to discord.com developers applications or you just google your way into the developer portal or you just click your way into the developer portal this is the url and we go to applications and we we create a new application uh which is going to be a bot now i have a video on how you can create your own discord bot in python so if you want to have a slow and detailed beginner friendly introduction watch this video but basically you just have some bots here you create the sumbot application uh and you go to i think oh auth2 you enable the bot feature here and you say okay it's allowed to send messages to uh read the message history to view channels and so on uh and then basically you go ahead and you copy this you um you join the channel or you join your server with the bot and you have the permissions you have the token and so on as i said everything like that is explained in the discord bot tutorial uh i assume that you somehow know how to create your discord bot and once you have your discord bot we can go ahead uh and make it intelligent so for this we're going to need a couple of libraries first of all we're going to need uh pip install discord uh is it discord or discord py i'm not sure let's go with discord.py i think it's discord.py this is the library that we're going to need for the discord bot itself and then we're going to use neural intense i think you know that library because it's written by me and i have used it in a lot of videos already so we're going to say pip install neural intense by the way make sure that you have the latest version because i have made a slight change to the um to the part where we get the response from the ai because uh up until now i printed it and now it's returned so we can actually use it in discord as well uh this is just a slight uh a minor change that i made and what i'm going to use in this video as well but you don't need to use it is dot n so pip install dot n this is just what i'm going to use uh to load the token into my script without showing it to the audience on youtube because of course if you have the token you can use my bot on my server and do whatever you want so uh even though the bot doesn't have any fancy permissions now what's the problem here it should install that in a second uh while it's doing that we can start and we can import discord this is what we're going to need mainly to connect to the discord server and operate the bot we're going to import os which is what we're going to use to actually load the environment variables and we're going to import dot n or actually we're going to say from dot n import load.n this is the function and we're going to save from neural intense import generic assistant now don't let the word assistant confuse you it's basically just generic chat bot or generic ai chat bot whatever you want why isn't this installing come on it's not that important actually so if it doesn't work it's not a big deal um however let's start with the actual uh with the actual uh coding what we basically need is we need to have a token how you get that token doesn't really matter uh you can get this token with dot n if you manage to install it you can just write it into your code directly the token is what you get from the discord developer portal for your bot to connect to your bot uh so if you have it what you need to do is you need to just say client equals discord.client and the token is later on uh used in the end when you run the client so you say client dot run and you pass the token whatever this token is it's basically just a long string of stuff like that um and it is what you get on the developer portal i'm going to use a token which i load from a file now i'm going to probably have to pause uh later on to get the token or actually let me do this right now i'm going to come back to you in a second okay i haven't installed this module for a long time so the problem is that the module is not called dot n it's called python dash dot end this is what i'm actually looking for so we can proceed in a second here it's basically just what i need to load the token into the environment variables then i can get the environment variables you can also just use a text file doesn't really matter but now that we have it installed what we're going to do is we're just going to say load dot n in order for this to work you would have to have a dot n file in the same directory because that is how it finds this file and the token is then just going to be os dot get and token because inside of the dot n file i have a string that or a line that looks like this token equals and then whatever the token is right and this is what it loads from the environment from the environment variables all right so now we have the token and the client is able to run and we're only going to focus on one event on one function we're not going to make this too complicated and this is going to be the on message function so we're going to have a sync def on message messages a parameter here and this is of course a client.event don't forget the decorator here or the annotation and we're going to say okay if the message is or if the message author is the client itself then we're just going to return otherwise we're going to say if the message dot content which is the actual text starts with and we're going to say we want every message that is addressed to the bot to start with dollar ai bot if the message content starts with a ibot then we're going to respond to it now in order to make the response uh in order to make the response we would have to have uh an intelligent ai chatbot already which we haven't done yet so we're just going to pass here and we're going to talk a little bit about the chat bot itself so about the intelligence behind the chat bot and for this we're going to use the neural intense generic assistant so what we need to do first is we need to create a chat bot um so chatbot is going to be generic assistant but this generic assistant takes a parameter called intense and intense is basically just a file path to an intense.json file and we're going to create this intense.json file i have explained this in a lot of videos already but for those of you who don't know what an intense file is an intense file is basically a file with patterns and with responses so the patterns are going to be training data for the neural network uh or for the model for the assistant to learn what specific inputs for specific or specific requests for specific categories looks uh look like so for example if i have the category greeting in the intense file i'm going to say okay this is what a greeting could look like hello hey what's up hello world uh what you're doing or something like that even though that would already be a request for something else but basically just hide whatever you want to provide as an example and based on those example it's examples it's going to recognize what a greeting looks like now when a user enters something similar that does not have to be hard coded in the intense file something similar so for example instead of hey good day he just enters hey the model is going to recognize that as a greeting and respond with a static static response with one of static responses one of these static responses so for this we're going to need an intense dot json file you can call it whatever you want but i usually call it intense.json and the json file is going to have the following structure it's going to have curly brackets inside of those curly brackets we're going to have the string intense and then we're going to have a list of intents like that and inside of this list we're going to have individual intents and those intents are going to consist of attack which is the category for example greeting or greetings we're going to have the patterns this is the training data and this is a list for example hi hello hey what's up uh or good day you can provide as many examples if you uh as you want the more examples you provide the better the model is going to perform and after the patterns we also have the responses and the responses are going to be static so we don't have to enter these exact messages we can enter similar messages and they will be classified as a greeting but the response is going to be fixed so it's not going to start generating some intelligent text it's going to respond with a static response so for example hello or hello sir whatever basically if you provide more than one response it's going to pick one of them randomly this is how it works and if you have one intent done you just add a comma and you can copy that and you have a second intent the last one is without a comma um and this one could be for example uh i don't know uh h or something and the questions in here are how old are you or what is your age you provide a bunch of examples and then the answer is i was created yesterday for example if you're a bot um and i'm not gonna write all these intents now uh in the video because it would be tedious and you got the idea of how it's done you just have these uh these things here i'm just going to copy a prepared a prepared intense file so i'm going to replace this with that as you can see the structure is the same intense list and then we have greetings what name age how uh neural nine florian which is me by and currently and those are just some examples you can add as many as you want to and we're going to use this here as an intense json file so we're going to provide the path intense.json and what we're going to do then is we're just going to train them all neural intense does all the work for you so you just have to say chatbot dot train model and then chatbot dot safe model and it's going to use a default name so you don't even have to provide a path here so chatbot creating this assistant here training the model saving the model and then down here every time we get a question every time we get a request to a ibot what we're going to do is we're just going to say the response to this message is going to be chatbot dot request so we just make a request to the chat bot with the message dot content the important thing is that we're going to cut off the first uh seven um the first seven characters because we have a i bought which are six and then one blank space so we're going to say from index 7 onwards we're going to read this as the message we're going to send this message to the chat bot we're going to get a response and we're going to say await message dot channel dot send response that is actually it so again we have an intense file with examples with patterns with static responses uh so in this case we can talk about what are you here for what can you do we can ask for the name of the bond we can ask for the age of the bot we can ask for how the bot feels we can ask for what neural nine is or what he thinks about neural nine we can ask about florian which is me again we can say goodbye and we can ask what it's doing at the moment which is something i added just right now so that it answers that we're currently recording a video i'm going to run this on my neural nine community server in a second here um so we train it on the intense file we save the model we create a discord client we load the token and then we just listen for incoming messages if one starts with that it's going to respond based on the chat bot and um yeah this is basically just the function call that is going to run the client so we can now go ahead and first of all maybe look at the discord server you can see that the ai bot is not online let me just see if i can find it somewhere here uh a i there you go ai bot this is the bot it's not online at the moment and now i'm going to run this script and you're going to see that it's going to come online if i didn't make any mistakes of course we need to train the model first and all that but once this is done maybe we should have a message that the client is now running so where do we usually enter that before we can do it like that print client running it's not exactly the point at which the client is running actually client is not the right word let's call this bot but running so when we see the message we at least know that the model is done with training and saving so at the moment it's still working with uh neural intense and once this is done we got a warning okay it's training it's training it's training it's training and bot is running so if we go to the discord server we should now see that the bot is online as you can see a ibot is online and of course we can chat with the ai bot itself so i can try with hey or hi i'm not going to get a response but if i say a i bought what is up it's going to say hello neural nine community because it is used to talk to the whole community so let's go to the channel which is called bot commands and say hey i bought how are you brother which is not a static respon static pattern that i have so how are you of course is part of the pattern but how are you brother it's not a static exact string that i provided but still it's going to be able to answer reasonably ai bot what's your name bro or let's just say yeah let's see what's your name bro ai bot what are you doing right now we are recording a video about an intelligent ai discord bot like me hey i bought what is your name my name is aibot and you can see that if i ask this question a couple of times what's your name it's going to answer either with the same with the same response or sooner or later it's going to give me a different response for example i am a ibot is different from my name and say ibot sometimes it says as you can see my name say i bought a i bought how old are you i was created on the 16th of july what other intents do we have a i bought oh someone is typing so maybe they're going to talk to a ibot now what do you think about neural nine as you can see this guy asked how are you and it says i feel great what do you think about neural nine i highly recommend watching neural nines youtube videos and so on um and now i can just go ahead and say ai bot by bro and says goodbye so the more intense you're at the more categories you add the more patterns you add the more examples and responses you add the more sophisticated this thing is going to become right uh so this bot is now going to be online as long as the script is running so if anyone decides to chat with the bot here from all the users that are online at the moment uh they're going to get a response if i terminate the script it's not going to work anymore unless of course i host it somewhere remotely in a server but that is how you build a simple easy with 26 lines of code a simple intelligent ai chat bot in python for discord all right guys so that's it for today's video i hope you enjoyed it hope you learned something as you can see here in the discord uh channel people are uh talking with a bot here neural 8 for example a neural cult member because they have neural in the name um so you can see sometimes it works sometimes it doesn't work for example a ibot how much is two plus two doesn't work because i haven't programmed that uh whereas how is uh or who made you works because i have an intent for that um i hope you enjoyed it and if you like this video let me know by hitting the like button leaving a comment in the comment section down below of course don't forget to subscribe and hit the notification bell to not miss a single future video for free and other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 12,703
Rating: undefined out of 5
Keywords: discord, discordpy, discord py, discord.py, discord ai, ai bot, ai chatbot, intelligent bot, discord chatbot, discord ai chatbot, assistant, python, python ai, python chatbot, discord python chatbot, discord python ai chatbot, intelligent chatbot
Id: urlkrueSXpI
Channel Id: undefined
Length: 18min 57sec (1137 seconds)
Published: Fri Jul 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.