Deploy Your Discord Python Bot For Free

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in today's video we're going to learn how to deploy a discord python bot for free online so let's get right into it [Music] all right so we're going to start by creating a simple discord bot and then we're going to deploy this discord bot to a platform called heroku this is a cloud application platform it supports a bunch of different languages and we're going to use it because it has a free plan where we can use it for 550 hours we can deploy our bot and run it for 550 hours and i think beyond that for 450 more if we add a credit card information um and this is the reason we use it here you can then also choose to pay if you want to use it longer or you can create a new account or you can just switch to a different platform but we're going to use it because of that free plan the focus of this video is going to be on the deployment so we're going to create a discord bot a very simple uh hello world or message repetition bot so it's just going to say hey you sent me that message hello world whatever we're not going to focus on creating an interesting discord bot because i have already a video on this channel where i show you how to create a discord uh chatbot using python i also think i have one about an intelligent ai discord chat bot so you might want to check those out if you're more interested in discord bot itself in today's video we're gonna focus more on the deployment so that you have your discord bot online in the cloud running without you having to run it locally on your machine and for that we're going to start here in the developer panel so discord.com developer applications and we're going to create a new application we're going to call it neural nine deploy bot or whatever we're gonna say that we accept the terms of service and then we're going to uh go to the bot section we're gonna add a bot we're gonna say yes and we're gonna say now down here that we want to enable the so-called message content intent this is something that is i think new compared to my old discord bot tutorial uh we didn't have to do that but now we have to do it because otherwise we will not be able to see the channels to read the messages so we're going to enable this in order to also be able to um to uh write into the channels to read the messages to interact with the channels and in python we're going to make use of this intent and then what we're going to do is we're going to go to all auth 2 we're going to go to the url generator here and we're going to say that we want to have an in-app authorization we're going to click bot and we're going to give a couple of permissions read messages view channels send messages send messages send messages maybe manage maybe read message history mention everyone add reactions whatever and then where do i get my link now oh sorry i was in the wrong section we're going to go here to oauth url generator i misclicked we select button we do the same thing essentially so we go again read messages send messages send send manage manage something like this there you go and once you have all the permissions you want you copy this link here you open a new tab you paste it and then if you're the admin of the server and you have the permission to to allow access to a bot you can just say okay add it to the server neural nine community uh in this case i need to use my authentication code my two factor authentication here which at the moment is this one here doesn't really matter because it changes every couple of uh minutes i guess we're going to authorize this bot now we're going to say i'm human and now the bot is in the server so here we should be able to see in my discord server that you can join by the way by clicking the link in description down below we should be able to see that we now have a what did i call it neural uh actually we shouldn't be able to see it here because it's not online but you can see the neural nine deploy bot now join the server and the important thing is now before we go into the python code you want to copy the um the token of the bot so you want to go to bot and you want to go to token and you want to copy the token if you don't know what a token is and you didn't see it when creating the bot you just reset the token this is what i'm going to do here in a second and then you copy that token and you paste it into a text file because that token is what you're going to use to connect to this bot using your python script so you want to do that you want to click on reset token you want to generate a token uh you want to copy paste it into a file and then you want to use it in your python script all right so the next step is to now go into your python script and to open up a command line to type pip install discord dot py which is the library that we're going to use in order to create the discord bot and then we're going to say import discord and we're going to say intense equals this is what we enabled when we enabled the message intent intense is going to be discord dot intense dot default and then we're going to say intense dot message content this is the thing that we enabled we're gonna enable it here as well by saying this is equal to true and then we're gonna create a simple client by saying client equals discord dot client with a capital c and we're going to give the intents that we have up there now for the token we're going to say token equals open [Music] the token file that i created in reading mode and read the content so again the token is what you which you get from the developer portal you just click on reset token you copy paste it into a file in my case i have it here already and this is what i load into the script of course you want to keep it secret because if someone has your token they can just use your bot uh and then as i said the bot is gonna be kept quite simple we're gonna say here client.event i'm gonna say async def on connect so when we connect to the server we want to just print that we connect it and then we want to also have client event async def on message so if a message is somewhere are sent somewhere and the bot can see the message what we're going to do is we're going to say okay if the message dot content dot starts with so message content is essentially the text and message content starts with is now a string function we're gonna say okay uh neural bots is gonna be the tag so dollar neural bot uh and then a white space is what we're going to require in order to be uh to to reply to this as the neural bot or the neural nine deploy bot and we're gonna say now await message dot channel so we're gonna get the channel that this message was posted in and we're gonna send to that same channel the following message hello you set and then plus message dot content six colon there you go um [Music] no actually not six sorry this was for my prepare code where i had a different uh where i had a different command this is going to be one two three four five six seven eight nine ten eleven so i think eleven colon should be it and then we're gonna say client run and we're gonna say that the token is the token let's run this let's see if it works connected let's go to the discord server let's see if the bot is online this is the neural nine deployment bot or deploy bot now let's just write something to it it should not respond and now if i say neural bot test it says hello you set test if i say something else test it does not work if i say neural bot hello world it says you said hello world okay so this works and it should also work in the channel so if i go um if i go into where is the channel i have a bot commands channel there you go neural bots test you set test okay so this works um [Music] people asking if i'm doing a tutorial yes recording right now you can also be part of the discord server and be featured in a video if you want to there you go so this works and the goal is now to take this and instead of running this on my computer here locally instead of running the python script locally we're going to now deploy to the cloud so that i don't have to keep my computer on to keep the bot on now of course i only have 550 limited hours but uh of course um yeah i don't have to do it myself i don't have to keep my computer online and of course i can also pay money to have unlimited amount of uh of hosting all right so the next step is to now go to heroku.com and create a new account i skipped this step because i already have an account and it's quite self-explanatory so we don't have to make a tutorial on this you just go to heroku.com you sign up you create an account you log in and then you're going to see this interface here probably without the bots because i already played around a little bit and what you need to do is you need to click here on new and on create new app and then you give the app a name let's say this is the neural deploy bot and we're going to say the region is europe we're going to create the app and then we have this interface here we have this uh instruction or those instructions here and basically what we need to do is we need to install the heroku cli so you click on heroku cli you follow the installation instructions here so um basically i think for windows we have the installer so you just download the installer you install heroku that's it and uh for for the rest so for the git stuff you use heroku combined with git so you should have git on your system you should have heroku on your system once you run the installer you should have the heroku command so you can open up your command line and type heroku again this is something that i'm not going to do because i have it already um but also you have the instructions so you click on heroku cli you download the installer you go through the process next next next next you know how it goes and then you have heroku and then what you need to do in order to create this new repository is you open up your command line and you navigate to the directory that you're working at or working in so in my case this is this directory and i now say heroku login then press any key to open up browser to login there you go i pressed any key log in now i can close the page and return to the terminal there you go logged in as accountsnorl9.com so this worked and then let's follow the instructions we can now create this new repository by basically doing exactly what it says we go to the directory that we're working in i'm already in there and then we say git init initialized empty git repository and then we essentially say heroku git colon remote dash a uh so heroku git colon was it colon yeah remote dash a neural deploy bot so let's check again heroku git remote dash a neural deploy bot we press enter this is gonna connect this to the heroku repository and then we essentially just add all the files commit and push to heroic master so we say git we can do git status first we can say git add all this information here then we say git um commits and i think i will have to set the conflict first so get config user name is going to be neural nine git config user email is gonna be accounts at neural9.com and then i'm going to say git commit dash m whatever i'm not going to do a meaningful commit message and then i'm going to say git push ah sorry get push uh to heroku master heroku master there you go this is gonna start a process this is gonna build the whole thing and it's gonna give us here what's the problem here determining push rejected to neural deploy bots failed to push some refs to this pre-receive hook decline this occurs when heroku cannot detect a build pack to use oh yeah the problem is of course that when we want to push it to heroku we need to include a couple more files so first of all we need to have the requirements.txt file and we need to also have a proc file or a proc process file i think so we create a new file here we're going to call it requirements.txt and this is going to make heroku recognize that this is a python package that it can build so we're going to say here discord uh discord dot py is the package i think it should be enough to just list it i'm not sure but i think it should be enough um and then we're also gonna say we wanna create a process so a proc file with a capital p and we're gonna say here that this file is um [Music] has the information worker python and then dot py so essentially this is the command this is the process worker and it's going to do the following thing it's going to start main py with the python command so let's do this again get at everything get commit complete get push heroku master and let's see if it works now so this should probably start building now there you go it installs python it installs pip setup tools wheel installs sqlite3 it goes through the whole building process now that it has the discord uh py requirement you can also see that it's uh where was this there you go it installs discord py now it's compressing everything so now we don't get the build error because we have the requirements txt file so for those of you who don't know what the requirements txt file is it's essentially a file that packages have to indicate what packages you need to be able to run this so if you have a package like for example i have the package bitstream that i published on github uh just a mini package but i think it's based on pi audio it's based on opencv so in the requirements txt file i have pi audio and opencv so when you install my package it's automatically also going to install those packages um and now we have done release there you go so everything works let's go to the heroku platform reload the page uh and let's go to resources there you go now it works you can see here we have the worker python main py this is the process that we have here so let's go to the discord um server and see if the bot is online uh first of all here's someone who's called neural and because of that we're going to give them the role of a neural cult member this is a rule with the server but i cannot see the bot being online so we're now going to put it online we're going to click on edit here we're going to turn it on and we're going to say confirm and if there are no errors if we don't have any problems we should be able to see that the server is going to be online here in a second there you go it's online so neural bot test you said test you can see the bot is not running on my system in fact i can close pycharm uh entirely i don't have anything running here i can also close um actually i don't want to close the command line but you can see here i'm not doing anything here in the server so this thing is running actually on heroku right now and i can keep it there i can just do nothing and it's gonna be there uh you will be able to interact with it so if i go to again bot commands or if some of you guys go to bot commands right now uh you can do neural bot hello world and it works because it's online on heroku if i go now and i disable it i confirm that i disable it it should go offline in a couple of seconds um you can see it's still online but it should be offline here pretty soon or at least it should not be able to respond so if i say neural bot test you can see we don't get an answer even though it says it's online it doesn't really do anything and will be offline here in a second as well or in a minute um but yeah this is how you do that and maybe one thing that you might be interested in knowing is that if you have some problems so if you see okay your bot doesn't work the way it should work or it doesn't even start at all you might want to look at the locks and what you can do here is you can just say heroku logs and you will get the logs off the server so you can see what exactly the server does and if there is um if there is an error message for example you know couldn't execute that because i got an exception value error at this line of code you can look at the logs and see what happened at the server and troubleshoot the problem so let's see if the bot is still online uh yeah okay it's still online but it doesn't work so it's it's going to go offline eventually and it doesn't work because it's offline i can put it online and then it works again but this is how you deploy a discord python bond for free on heroku so that's it for today's video i hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 9,826
Rating: undefined out of 5
Keywords: python deploy bot, python deploy discord bot, discord.py, python discord, discord bot, python, discord, python discord bot, deploy discord bot, deploy python discord bot, heroku, cloud, deploy, deployment
Id: SVKhbjcH6WQ
Channel Id: undefined
Length: 18min 46sec (1126 seconds)
Published: Wed Sep 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.