Discord Bot
Last updated
Last updated
This guide will work on any of the HelioHost servers.
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse.
The Discord app works with desktop and mobile platforms, combining the features of a chat lobby, a message board and a VoIP chatting system into one app that's not going to hog resources on your phone or PC. Discord's popularity has only multiplied in recent years, with the app now reaching over 90 million registered users.
Bots and apps are the lifeblood of the Discord development community. They come in all shapes and sizes, from small hobby projects for your server with friends, to huge projects that live in hundreds of thousands of servers. We love seeing the unique, fun, and sometimes downright strange (in a good way) creations that come from our community.
First you'll need to have a discord account, and a discord channel that you'll add the bot to later. Here's a good guide to get started:
Once you have your discord server all set up it's time to create the bot through discord's site. Go to http://discordapp.com/developers/applications and make sure you're logged in. Then click New Applcation
in the top right.
Type a name and hit create. I decided to name my bot HelioBot
. Now, on the left, click Bot
.
Then, click Add Bot
on the right followed by confirming that you do want to add a bot.
Click OAuth2
on the left navigation.
Then check the bot box under SCOPES
, and scroll down and select Administrator
under BOT PERMISSIONS
. You could select the exact permissions that your bot needs one by one, but for this simple example it's easier to just allow the bot permission to do everything.
Click copy next to the URL that is generated and open that URL in a new tab. Then, select your discord server from the dropdown menu.
Click Authorize
and complete the reCAPTCHA to add the bot to the Discord server. If you go to your Discord now you can see the bot on the offline user list.
Log in at https://www.heliohost.org/login/ and continue to cPanel. Then load up the file manager.
In the top left corner click + File
and then type the name of your new file: heliobot.py
.
You want to create this bot in your home directory:
-because you don't want to accidentally make it public. Click Create new file
to create the new file. Locate the new file, and right click on it. Select Edit
, and click Edit
again on the box that pops up. Then copy/paste this code in:
The first line is called the shebang, and it tells the system which version of python you want to use.
On Ricky, python3.6
is available, but on Tommy and Johnny, you would change it to python3.7
!
Once you have your code copy/pasted in click save changes in the top right corner, and the close the tab. We need the bot to be executable so right click on it in the file manager and select Change Permissions
.
Set the permissions to 755
.
Now we need to provide our discord token to the bot so click +file again and this time name the file .env
.
Make sure you start the filename with a period. It's easy to miss that in the screenshot. This file needs to be in the same directory as your Python script.
Go back to the Discord bot page and click bot on the left navigation again.
Locate the token for your bot and click Copy
on it.
If you ever think someone has gotten your token, for instance if you posted it on a public wiki like I just did, be sure to come back to this page and regenerate it. Anyone who has your token can do whatever permissions you granted your bot to your server. Now you need to paste this token into your .env
file that you created, but likely you won't even be able to see your .env
file because files that start with a dot in Linux are considered hidden files. So on the file manager, click Settings
in the top right corner and make sure you have hidden files shown.
Now you should be able to see the .env
file, right click on it, and select Edit
. Then paste the token into the file like this:
Make sure the line starts with DISCORD_TOKEN=
like that. Then click Save Changes
and close the tab. Saving your token separate from your code is good practice because this way you can share the code on GitHub or something like that without worrying about people getting access to your bot.
You have a functional bot now, but you need a way to start and stop it. We'll use CGI to do that so you can control your bot through your web page. With the file manager, navigate to public_html/cgi-bin
and create a new file called start.py
.
Paste in this code into the new file:
Now, we need to set the permissions of this start.py
file to be executable. On the file manager right click on start.py
and select Change Permissions
.
Set the permissions of the start.py
file to 755
.
We also need a way to stop the bot so create stop.py
in the cgi-bin
directory.
Paste this code into the new stop.py
script:
Right click on the stop.py
file and select Change Permissions
.
And change stop.py
to 755
permissions as well.
Now everything should be ready to be tested. Open your browser and go to domain.heliohost.org/cgi-bin/start.py
and it should start the bot. You can check if the bot is running by going to Discord and seeing if HelioBot is showing up as online now. If it is, test it out by typing the command:
It should respond to you.
If it doesn't work go back and check all the steps again to make sure you did everything correctly. If you can't figure out what is wrong let us know by opening a customer service ticket. If everything is working you can shut your bot down by going to domain.heliohost.org/cgi-bin/stop.py
Then watch your discord and after a few minutes your bot should show up as offline. It's a good idea to keep your bot offline if you don't need it so you don't cause too much load. We recommend monitoring your load by clicking the account load button in cPanel. If you get to 80+ it might be time to shut down the bot or consider moving to a VPS where you can run your bot as much as you want and have root shell access as well.