DEV Community

Cover image for Making and Deploying Discord Bot with Python

Python Discord Bot Making and Deploying Discord Bot with Python

p014ri5 profile image Prasad Narkhede ・5 min read


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

  1. First of all go to Discord's Official Website. Alt Text
  2. Log in if you're not already logged in.
  3. Then go to Discord's Developer Portal and click on New Application and give it a name! Alt Text
  4. Give it a cool looking icon and hit save changes! Alt Text
  5. Switch to Bot tab in the left pane.
  6. Hit Add Bot and then Yes do it!
  7. Disable Public Bot because it's still in development and we don't want anybody to use it.
  8. Remember to hit save changes..
  9. Copy the token of Bot and save it somewhere safe.. 🀫 We gonna need it later! Alt Text

Adding Bot to our server

  1. In your Discord app make a special testing server!
  2. Now go to OAuth2 tab.
  3. Set scope to bot.
  4. Set bot permissions to Administrator. Alt Text
  5. Copy the generated URL and open it in browser. Alt Text
  6. 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

  1. Create a GitHub repository with your bot's name, and initialize it with file. I'll also recommend adding LICENSE and Python .gitignore file. (You can always make your repo private if you want to!) Alt Text
  2. Clone the repo to your PC using "git clone " and open it using your favorite code editor. Alt Text
  3. Create a bot folder. And inside it create a file Alt Text
  4. If you are just beginner in building Discord Bots, Lucas has dope tutorial on YouTube! You can also refer to official documentation for more features here.
  5. 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")

async def on_ready() :
    await client.change_presence(status = discord.Status.idle, activity = discord.Game("Listening to .help"))
    print("I am online")

async def ping(ctx) :
    await ctx.send(f"πŸ“ Pong with {str(round(client.latency, 2))}")

async def whoami(ctx) :
    await ctx.send(f"You are {}")

async def clear(ctx, amount=3) :
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode
  • We also need to install module for our program to run so hit the following in your shell.
pip install
Enter fullscreen mode Exit fullscreen mode

Alt Text

  1. Run the program. Alt Text
  2. Wait for a while and our bot would be online and idle.
  3. Test bot with commands .ping .whoami .help and .clear Alt Text

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
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode
  • Create a runtime.txt file in your project's root directory and add following to it :
Enter fullscreen mode Exit fullscreen mode
  • And finally create requirements.txt file and add our required libraries to it.
Enter fullscreen mode Exit fullscreen mode

Our folder structure might look something like this.
Alt Text

  1. Now push the changes.
  2. Go to and Sign Up or Log In if already have an account.
  3. Then go to Dashboard.
  4. And go hit New. Now give it a unique name. Alt Text
  5. Now you should have a screen like this. Alt Text
  6. Click on Connect to Github. You might need to Authorize the app. If so Do It. Alt Text
  7. It asks for repo. Enter your name of repo there. Hit search and then connect. Alt Text
  8. Now scroll down below and Enable Auto Deploy. Now Scroll a little more and hit Deploy Branch. Alt Text
  9. If everything went right, It should look something like this. Alt Text
  10. And lastly we need to setup our environment variable. So got Settings on our app's dashboard. And click Reveal Config Vars.
  11. Finally Setup our environment variable. And hit Add. Alt Text

Getting Our Bot Online.

  1. Go to Resources Tab.
  2. Click the little edit icon near our apps' name to enable our dynos. Alt Text
  3. Enable it and hit confirm. Alt Text
  4. Test the Bot.

Alt Text

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 (2)

Editor guide
perymerdeka profile image

can we run bot using gunicorn?

p014ri5 profile image
Prasad Narkhede Author • Edited

Sorry still a noob :/ No experience with gunicorn.. It sounds fun tho!