Intro
I know this is so basic and easy thing to do, but I would like to accumulate everything I know about it in this article. It includes:
- Creating and adding a bot to our server
- Pushing it to GitHub
- Deploying it on Heroku
So grab some ☕ and lets get straight into it!
Creating the bot
- First of all go to Discord's Official Website.
- Log in if you're not already logged in.
- Then go to Discord's Developer Portal and click on New Application and give it a name!
- Give it a cool looking icon and hit save changes!
- Switch to Bot tab in the left pane.
- Hit Add Bot and then Yes do it!
- Disable Public Bot because it's still in development and we don't want anybody to use it.
- Remember to hit save changes..
- Copy the token of Bot and save it somewhere safe.. 🤫 We gonna need it later!
Adding Bot to our server
- In your Discord app make a special testing server!
- Now go to OAuth2 tab.
- Set scope to bot.
- Set bot permissions to Administrator.
- Copy the generated URL and open it in browser.
- Select you server and hit Continue and the Authorize. Complete the CAPTCHA and you will see the bot in your server!
Making a bot work!
Okay, the bot is in the server but we need to get it online and working. So
- Create a GitHub repository with your bot's name, and initialize it with README.md file. I'll also recommend adding LICENSE and Python .gitignore file. (You can always make your repo private if you want to!)
- Clone the repo to your PC using "git clone " and open it using your favorite code editor.
- Create a bot folder. And inside it create a main.py file
- If you are just beginner in building Discord Bots, Lucas has dope tutorial on YouTube! You can also refer to discord.py official documentation for more features here.
- But for now I got a sample code ready for testing our bot!
import discord
from discord.ext import commands
import os
client = commands.Bot(command_prefix=".")
token = os.getenv("DISCORD_BOT_TOKEN")
@client.event
async def on_ready() :
await client.change_presence(status = discord.Status.idle, activity = discord.Game("Listening to .help"))
print("I am online")
@client.command()
async def ping(ctx) :
await ctx.send(f"🏓 Pong with {str(round(client.latency, 2))}")
@client.command(name="whoami")
async def whoami(ctx) :
await ctx.send(f"You are {ctx.message.author.name}")
@client.command()
async def clear(ctx, amount=3) :
await ctx.channel.purge(limit=amount)
client.run(token)
Setting environment variable and getting it online.
- Now looking at code, you can see we will be using a environment variable to store our discord bot's token. Why? Because it's a good thing to do, as our code is on GitHub. So for that we need to setup a environment variable by adding this to your .bashrc or .zshrc (or whatever dotfile your shell uses) :
export DISCORD_BOT_TOKEN={Here goes your bot token}
- We also need to install discord.py module for our program to run so hit the following in your shell.
pip install discord.py
- Run the program.
- Wait for a while and our bot would be online and idle.
- Test bot with commands .ping .whoami .help and .clear
Yay, we got our bot up and running!
Now you can push your code to GitHub using :
git add .
git commit -m "Initial Commit 🚀"
git push origin master
Deploying bot to Heroku
We can't keep our PC running 24/7 for our bot. Can we?
Let's deploy Nemo to Heroku.
- Create a file called Procfile in your project's root directory and add following to it. It will contain a worker command that will start our bot.
worker: python bot/main.py
- Create a runtime.txt file in your project's root directory and add following to it :
python-3.8.2
- And finally create requirements.txt file and add our required libraries to it.
discord.py
Our folder structure might look something like this.
- Now push the changes.
- Go to heroku.com and Sign Up or Log In if already have an account.
- Then go to Dashboard.
- And go hit New. Now give it a unique name.
- Now you should have a screen like this.
- Click on Connect to Github. You might need to Authorize the app. If so Do It.
- It asks for repo. Enter your name of repo there. Hit search and then connect.
- Now scroll down below and Enable Auto Deploy. Now Scroll a little more and hit Deploy Branch.
- If everything went right, It should look something like this.
- And lastly we need to setup our environment variable. So got Settings on our app's dashboard. And click Reveal Config Vars.
- Finally Setup our environment variable. And hit Add.
Getting Our Bot Online.
- Go to Resources Tab.
- Click the little edit icon near our apps' name to enable our dynos.
- Enable it and hit confirm.
- Test the Bot.
Hurray 🎉
Our bot is now online and ready to serve 24/7. And with every commit you do to your remote repo, Our bot will be automatically updated and deployed.
Discussion (0)