Creating a Discord Bot in Python 3.9 Tutorial (Fast & Easy)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys in this video i'm going to be showing you how you can create your very own personal chat bot in discord it will process all of the information that it receives and you'll be able to return a response to certain users or to everyone depending on how you want to set it up but let's just go over all of the basics and we will be doing this in python but the first thing i want to do is go over an example of what we will be making so let's pretend we have a chat and we write hello you'll notice that our chatbot maria is going to respond to our messages and if we create a command such as random she's going to generate a random number for us we can also say bye or use another command such as anywhere and so far these messages will only be responded to in this channel but if we go to a different channel such as welcome and we write slash anywhere she will also respond there but instead if we try to write hello she will not respond to the message here because we have specified it to only work in our discord bot channel and i'm going to show you how to set that up later so you can make it respond to messages in certain channels as well but let's go ahead and get started by going to firefox because the first thing we have to do is go to the discord developer portal and just type that in on google so that you'll get this as a search result and we'll just click on discord developer portal and that's going to take you to the documentation but we will just click on application because the first thing we should do is go ahead and create a new application and for this we will just name it whatever you want to name it i will name it sample underscore bot and click on create and the first page you will land on is a general information page so this is just to define essentially what the bot does and and it's just something that allows you to give it a small description so you can understand what it is later in case you have lots of bots these are not really good names because first button sample bots can be really confusing if you have a lot of bots so of course give it something a little bit more descriptive but for this situation we're just going to skip this section because it is not necessary and we are going to go to the bot and as you can see right here there's a section that's called build a bot and all you have to do is click on add bots and it'll say yes do it then we will have our first bot and the first thing i want to do is give it an image that i have in downloads it's going to have this face over here and we're going to give it a name which is going to be jojo and let's click on save changes the next thing we want to do is go ahead and copy this token so just click on copy and go to your python project this is a completely new python project and the first thing we should write up here is token in capital letters and we are just going to insert it inside here your token is going to be different than this one of course so make sure you use your own token because this will not work if you try to copy it and let's go back to the documentation and there's a few more things we should do so right here we have a few settings such as public bot can be added by anyone and since we do not want anyone else to use this bot we only want us to be able to add it to servers we are going to disable this option and then we can go ahead and click on save changes and the next thing to do is to go to this authentication tool because we need to create a link that we can use to invite it to our server so right now we should go down and click on bots and then here we want to give it some permissions so this will allow you to decide what you want this bot to do and all we wanted to do is essentially be able to interact with text permissions so we'll just select all of those but you can select whatever you want that your bot should be able to interact with for safety i would not give it the administrator rule immediately because that could be dangerous but it's up to you then we'll click on copy now we can go ahead and just paste that in a new tab and you will see that it will ask us if we want to add our sample bot to the server so we'll add it to my server which is code palace and we'll click on continue then it's going to tell you do you want to give it all of these permissions and of course you can select the ones you want and the ones you don't and then we'll click on authorize then i should also specify that i'm a human and it will tell you congratulations your bot is now part of the server now if you go back to your discord server you'll notice that the bot that we just created will be listed under the offline people because we have not yet started the server and we cannot yet see the bots because of this so jojo is here and jojo's alive so as i said earlier it's very important that in general information you give it some descriptive names and some descriptive text possibly an image that will make it easy for other people to recognize it otherwise it will look as it did when we tried to invite it but with that being done we are actually finished with this section over here now we can actually go and work with the code in python so the first thing we have to do inside here is type in pip install discord and once we have successfully installed that we can close our terminal and we can go and add a few imports so the first one we have to import is the discord package so import discord then we want to import random the next thing we have to do is go ahead and create a client which is going to be our discord.client then the first thing we can do inside here is create a client event so client.event and we're going to have to define this as an async function so async def and that's going to call unready which means that once we start the bot it will call this function and the default message you'll see is going to be a print statement that says we have logged in as and then we have to add this template which is a0.user and we are just going to use the format that is provided by the client of this message and we can already go ahead and run this so let's go ahead and type in client.run and the only argument you have to supply here is the token that we have that we have retrieved from the discord documentation but if you go ahead and click on run the first thing you should notice is that you'll get a print statement that says the discord bot has successfully been logged in as jojo at this hashtag and in addition to that if we go to our discord server and scroll all the way up you'll notice that our jojo chatbot is now online and let's go ahead and give it a roll which is the admin role which is my role essentially and now it's at the top with the other chat bot now of course the last thing to do in this tutorial is to make it so it can actually respond to things because right now all it does is come online and doesn't really do anything in particular so the next thing we have to do is go ahead and create another client event so let's go ahead and type in at client.event and we have to make this also an async function so async def and it's going to be on message and that's going to take a message as a parameter and now we can do a lot with this message and this message is going to be every single message that enters the server so we want to be able to process this message and actually be able to return it but there are a few variables i want to create before we move on with responding to the messages and the first one is called username we just want to be able to get the username so we can use it later and that's just going to be the string of message dot author and we want to split it at the string of hash and that's going to be at the index of zero so this does is get the username which is this whole string over here with the number and the hashtag and it splits it once it finds a hashtag which all usernames in this code have and then it gets the first element of the list that we just split it into which is going to be the name and returns that so we just end up with the name but after that we want to go ahead and create a variable that's called user message and that's going to equal the string of the message and that will take the content of the message and then we want to go ahead and make sure we can log which channel the message is coming from so let's go ahead and create another string of message dot channel dot name and finally we have to go ahead and print all of this information so we can look at it later so formats and then we should add a pair of curly brackets and inside here we will add username colon use a message then we should add a pair of parentheses and inside here we will add the channel so this print statement will take care of logging everything that happens on our server so when we run the server right now you'll notice that all the messages that go on the server will be logged inside here but the next thing we have to do is make sure that the chat bot does not infinitely respond to itself and this is very important because if the chat bot responds right now it will continuously grab the latest message which means it will grab its own message and it will always respond to its own message so to do that we just have to type if message.author is equal to client.user then we will just return and we won't do anything so that just takes care of making sure that the bot does not respond to itself next we have to go ahead and check if the channel is the corresponding channel that we have specified so messages only get responded to in that certain channel otherwise it's going to be a mess because it will respond everywhere on your whole entire discord server and you don't really want that so we're going to type if message channel dot name is equal to the channel name that you specify in this case i want it to be part of discord bot tutorial and in case you're confused about what a channel name looks like just go to your discord server and as you can see down here these are all channel names which means if you want something to only be written in html type in html i'm using discord bot tutorial and that's why i wrote discord bot tutorial then we have to add this colon here and now we can actually add a message that it should recognize before responding so for example if use a message and we want to make sure it gets translated to lower case otherwise it probably won't recognize it is equal to hello then we are going to create an await which is going to tell the channel to send a message back and that's just going to be a formatted string which is going to say hello and it's going to respond to the username that sent it to us and then we want to type in return so it returns out of this statement lift the user message to lower is equal to by we're just going to copy this section right here and say see you later and i actually made a typo here this should be user message then let's go ahead and specify one more response which is going to be alif user message to lower is equal to random with a exclamation mark so it looks kind of like a command then we're going to create a response which is going to be a formatted string and it will say this is your random number and inside here we need to call the random function which is going to take a random range and i'm just going to insert the number of 1 million then we can go ahead and take the await message function and paste it right under but of course we want to replace the string with the response and we need to return this as well so this takes care of responding to messages that are only located in this channel right here but let's also create a message that can be responded to anywhere on the server so to do that all we have to do is type in if user message dot lower is equal to exclamation mark anywhere then we will create a message channel.sent message.channel.sent and all right this can be used anywhere and then we want to return this so it doesn't look for other messages and with that we can go ahead and rerun the program and actually test it in our discord server so as you can see right now we have logged in as jojo0524 and let's go to the server so now we will go to discord bot tutorial and we will say hello and you'll see jojo will respond to us in the chat and we can also say bye and it will say see you later federico and if you go back to your pi charm file or wherever you're running python you'll also notice that we will get all of the logs of everything that's happening in our console so right now i wrote hello and if that was in the discord bot tutorial so this works for logging everything that happens in your entire server essentially but it's just good to know it's good to log this stuff so you know what's happening but let's go ahead and try our random command so let's go random and it'll say this is your random number and it will give you a random number and the last thing to test is if we can use our anywhere command in any chat we want so i tried to post it in a place without the required permissions and as you can see i got an error because i needed administrator permissions to post in one of my sections but if you post it in any section that you have already given the bot the permission it should work fine so let's go to website feedback for example and let's do exclamation mark anywhere and you'll see that it will respond to us this can be used anywhere including in the chat that we were using it before of course so essentially that's how you create a very basic chatbot or bot in discord and if you have any other questions feel free to leave them in the comment section below and i'll do my best to look at them but otherwise with that being said thanks for watching and i'll see you guys in the next video see you
Info
Channel: Code Palace
Views: 41,544
Rating: undefined out of 5
Keywords: android studio, code, coding, program, programming, tutorials, android, studio, java, kotlin, python, c#, how to, app, applications, developer, developement, learn coding, learn programming, free, coding in flow, androidevs, code palace, windows, computers, ios, technology, ai, bot, machine learning, neural networks
Id: fU-kWx-OYvE
Channel Id: undefined
Length: 12min 58sec (778 seconds)
Published: Mon Feb 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.