Learn How to Make a Python Discord Bot in 17 Minutes. Host for Free!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Great post! Thank you.

👍︎︎ 5 👤︎︎ u/bestscourses 📅︎︎ Jun 04 2021 🗫︎ replies
Captions
[Music] what's going on clarity coders in this tutorial i'm going to show you how to create a discord bot from scratch line by line by the end of it you'll have a bot that can give witty responses and understand commands most tutorials i've seen online for this take around an hour we're gonna do it in 17 minutes and at the end of it i will give you a special code that you can host it and run it online for free so stick with me let's not waste any more time and jump right in all right so let's get started here the first thing you're going to do if you don't have a discord account is go ahead and sign up for one here you can click register i'm not going to walk you through this you can do that on your own it's pretty simple once you're finished you're going to come to a window that looks like this you can see mine has the plus symbol with an exclamation point in the top corner you're gonna click that you can click skip all tutorials and we'll push it again to add our server we're going to create my own for my friends and family you can give the server a name and push create now you can see here it gives us two starter channels a general text channel and a general voice channel we can send a message here in the bottom just say hi or something like that and we got everything set up now to get started we're going to head over to the discord developer portal the link will be in the description i googled it and came to this page here i'm going to click on applications and then new application you can give your application a name whatever you would like i'll just do clarity tutorial or something along those lines and then we're going to push create now on here we have some values that we're going to use we're going to go ahead and flip to the bot tab here and click add bot yes do it you can name your bot and up at the top we're going to copy this token now this token obviously is sensitive you don't want to share it with other people or else they can control your bot if it does get out in the open somehow you can click the regenerate button beside it and then you'll have a new token so no one will have access to your bot again we're going to flip over to oauth2 under scopes we're going to select bot then we're going to scroll down to bot permissions we're going to give it most of the text permissions so we can go ahead and start clicking those you can click whatever your bot intends to do and then over in the other one i'm going to give it the ability to kick members band members and it'll need to view channels once this is done we're going to need to copy that link but before we do that because we still have our token let's open up notepad and paste in our token here i'm obviously going to regenerate mine so you can't use mine you'll have to use your own we're just going to paste that in there for later and then we'll copy this invite url so this is going to allow us to actually join our bot to our discord server we'll copy that then we can paste it into the browser you'll select your server that you created and you'll hit continue you should see the permissions that we selected from before and you can hit authorize i am a human so i'm going to check that now if we go over here you'll see our bot is offline but it is on the server now so now we can go ahead and start coding our bot now i'm going to use replit you don't have to you can use whatever you would like i'm using replits simply because it is the cheapest and easiest way to kind of host your bot online 24 7. i'm going to have a link in the description for a code that will get you a free always on server as well so check that out we're going to open another tab we'll go to replit.com you can log in from here if you don't have a account you can go ahead and register it's free i'm going to sign into mine once you get all signed in you should come to a page like this we can hit the plus button select our language which is python you can call this replit whatever you want i'll have the link to mine if you want to grab my code and then you'll come to a page that looks like this now i'm going to do some quick edits real quick here just to make my font bigger and stuff so it's a little easier for you guys to see we're going to import discord this is going to be the library that we use to work with our discord bot from here we're going to set up a client so we're going to say client equals discord.client note that's client with a capital c now we can set up some asynchronous functions here so we're going to say client.event so we're listening for an event here and it's an asynchronous function we're going to define that as on underscore ready so what this is going to do is it's going to listen for our bot to be ready and logged in and then this function will run once it's logged in so to see if this is working we can just print out something i'm going to print out an f string here just because i'm going to sub in our bot's name inside curly brackets so we can use variables inside curly brackets of an f string we can say client dot user space outside of the curly brackets we'll say logged in now now it should tell us when our bots logged in now one thing you might run into here is if you run this now you're going to see there are some issues right so it's going to look like it works it's going to install our packages no if you're not on replit you would have had to do pip install discord on yourself but replied takes care of that for us so you'll see it ran it stopped running and it didn't print anything out this trips people up all the time when they use functions now it didn't actually run that function because we didn't call it and we're not listening for any events we just defined an event so what we need to do now is we need to actually set up our bot to run and listen for events so we're going to go over to the secrets tab we're going to name our secret for the key we're going to name it token and for the value we're going to paste in the value that we copied into notepad if you don't have this you can head back over to the developer discord developer page and get this again we're going to paste that into value if you're doing this locally you can create a env file for your secrets now you can see below they have some cheater code here that we can insert so we're going to import os and then we're going to insert my secret and it's just going to pull that variable out of the token now why are we putting this in secrets because replits by default are public and we don't want that secret getting out so we're going to hide it in that secrets file so now we can do client dot run my secret now our bot should actually stay running and you can see it spit out that it is logged in now and you'll notice that the program is still running so our bot's sitting online and it's waiting for in our case nothing because we don't have any other events so we just did on ready so we're gonna add another event that runs when a message is sent anywhere on our server so this is going to look similar i'm going to do at client event and this time i'm going to do an asynchronous function again i'm going to define it as on message on underscore message and it's going to give us a message object when someone sends a message this function is going to run and it's going to give us a message object now you can look at the documentation for this and i'll link that below if you're wondering how i found out what attributes are on this so i can do message.content and it's going to spit out the message to our console below now if you don't want to look at the documentation you're curious what other attributes and functions a message might have we can print that out below as well so let's do print and then this time we're going to do dur and inside of it we're going to pass in message and it'll show us the attributes and functions on that message object so once this runs you'll notice it says it's logged in and nothing happened that's because we didn't send a message so let's go over here and we'll send a message and that should trigger that function to run if we go back to our replit here you can see that it did spit the message out into the console and it also spit out a bunch of attributes and functions on the message itself so from here some of these are named intuitively so we can kind of figure out what they're doing for example you'll see that there is a delete below here so if we wanted to skip the documentation and just try to figure it out on our own we could try to delete messages now something that a lot of people do is message dot delete i'm guessing this is a function so this won't give us an error if we run it but it's probably also not going to delete our message so let's just give that a try just to show some things that trip people up from time to time so if we run this you'll notice we get logged in awesome let's go ahead and trigger this function by sending a message remember it doesn't run unless we send a message and you'll notice it didn't delete our message so already we have a red flag there and the function did run because it's got the message and the attributes below but delete did not work that's because this is actually a function so let's go ahead and run it with parentheses like a function this is going to probably not work either but it's going to give us an error that's a little easier to debug so let's go back and we'll send another message now if we go back to our replica you'll see that we do have a warning this time now with asynchronous functions if we have a blocking request we're going to have to await the response so this is an error you get a lot when you're working with asynchronous functions so you want to know what it looks like so what we're going to do here is we're actually going to await the response from our message.delete and that should now work correctly so now essentially when anyone sends a message in our server we automatically delete it so we have created the most useless discord server imaginable let's say hello bot delete me and you can see it instantly deleted the message and you'll see our code ran here so now let's try and do something a little more common and a little more useful you'll see a lot of times people look out for commands for bots you'll do a symbol and then some text and then the bot will do something so what we're going to do is anytime someone sends a message we're gonna say if message dot content dot starts with you can pick whatever you want here we'll say dollar sign greet so now this if condition is going to run if someone sends a message that starts with dollar sign greet so we're going to do message dot channel so this grabs the channel from the message so wherever they sent the message is where we're going to send this so we're going to do message.channel.send and then you can do anything you want here string wise so you could send an f string you could send a variable that's a string whatever you want in our case we're going to send a greeting so we'll say something like hello how are you let's personalize this a little bit so let's make this an f string so we're going to use curly brackets and put the f before the string and we can do message.author so now if we go back and we use our command here if we just say greet it's obviously not going to work let's do dollar sign greet again it's not going to work as you can imagine we hit that same error we had before so we're going to need to await this response as well so now if we go back and try to run it again you can see that was successful so now i'm going to drag in a csv file so we can do something a little cooler you can get this from my replit or the github and we're going to try and make a bot that gives some witty responses and i'll open this up really quick so you can see it what i did was i scraped reddit shower thoughts for some witty phrases that we can paste into our code when our bot gets a request so for example falling down or getting hit by the entire planet is the same thing so we're going to import a couple more libraries here to accomplish this so up above let's import csv so this is going to allow us to read in that csv let's create a list so we'll say phrases equals and we'll just do a blank list for right now and now let's open that csv so let's do with open phrases.csv as csv underscore file now we can do csv underscore reader equals csv dot reader we can pass in that csv underscore file and we can say our delimiter is going to be commas and that's just based on the file itself so we'll say for row in csv underscore reader and then we can do phrases dot append our row and we're going to grab out the index so square brackets one for our index the first one was like a username and then the one index was like a phrase so if we run this you'll see i have an error here because i can't spell spelled delimiter wrong so let's change that to e to an i now if we run this it's obviously not going to work yet or actually send phrases but we can see that there's at least no errors so we can stop this we need to import one more library let's import random so this is going to allow it to randomly select one of the phrases out of our list this is going to be a little different i'll show you another way to do this let's say else if let's make whatever we want our command to be so let's say as a string let's say dollar sign thought in message.content so now anywhere in message in the message they send if they have dollar sign thought it's going to run this if statement now remember this is only if it's lower case so you could do dot lower or something if you want it to be for everything so we're going to copy the line from above await message.channel.send and actually i'll do a variable above just to make this a little easier to read so let's say response equals phrases out of phrases we're going to use square brackets so we can get an index we're going to say random.randint so this is going to give us a random integer from 0 which is the starting index to the length of phrases minus 1. and then we're going to send that response whatever it is if we run this code again you'll see hopefully we have no errors we don't now let's go back to our server and from here we can do dollar sign greet me and of course this still works because it still starts with dollar sign greet and then we can do dollar sign thought and as you can see it spits out a random shower thought there are probably kickballs jammed in some school gym ceilings somewhere that have been stuck there since 1970. perfect and that's as simple as that we have our working discord bot that's up and running now replit this will fall asleep after a certain period of time i have a link in the description below for you to get always on for free for six months i think so i'll post that go ahead and click it if this link stops working in the future let me know and i'll post a new one so if we want this to run 24 7 we'll click this after you sign up on that link you should have an always on tick mark here you can tick that and it will stay alive and awake and running if you like this video and these tutorials it really helps the channel if you subscribe i would really appreciate it if you did hit that subscribe button in the bottom and the bell so you don't miss any notifications if you want to see a special video post in the comments below i have a tendency to make videos based on what the comments are so let me know what you would like to see from your discord bot or whatever you're working on and i'll get to that as soon as i can thanks for watching and until next time keep coding
Info
Channel: ClarityCoders
Views: 12,366
Rating: undefined out of 5
Keywords: pythontutorial, python, discordbot, codebot
Id: wjQ9Nin21d8
Channel Id: undefined
Length: 17min 13sec (1033 seconds)
Published: Tue Jun 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.