Learn to Code via Tutorials on Repl.it!

← Back to all posts
Basic Discord Bot Tutorial [Python]
CoolCoderSJ (690)

Discord Bots in Python

This tutorial will help you create your own Discord bot in Python, rather than asking others for bot code SMH.

Important Links

These links will help you on your way to becoming a great developer. First rule of thumb- always google your question, check StackOverflow, and check the Discord.py API Docs before asking others for help.

Let's Begin!


To run your code, you need to import packages that will help you. Since this is a multi file bot, we only need to import certain things here-

#import discord, our server file (later on), and a module to find all files in a dir, and os
import discord, discord.ext, keep_alive, glob, os
from discord.ext import commands

Initiate our Client

Your bot client will use a token to connect to Discord's API.

client = discord.Client()

To find out when your bot logs in, add this bit

# A discord.py event handler
#what do to when the bot is ready
async def on_ready():
	#Print "Logged in as Bot_user_here"
	print(f"Logged in as {client.user}")

We always want to try to split up our bot into multiple files, one for each command whenever we can. so that our bot can run faster and doesn't get too cluttered.

Add this bit of code next-

# Get our commands files
files = glob.glob("commands/*")
#Skip all files that are in this list. (I would remove it from the list, but Python does not allow removals from list/tuple/dict while an iteration is in progress)
done = []
for file in files:
	#IF file not already loaded
	if file not in done:
		#If file is a python file
		if file.endswith(".py"):
			#Convert file path to importable path
			file_to_load = file.replace(".py", "").replace("\\", "/").replace("/", ".")
			#Load command
			print(f"Loading {file_to_load}")
			#Add to list of loaded files
		#If filepath is a folder
		elif not file.endswith(".pyc"):
			#Get files in dir
			files2 = glob.glob(file+"/*")
			#add these files to our master list
			for file in files2:
		#Ignore pycache



next, to start a webserver that you will ping so that your bot stays alive. This code should error as you do not have a keep_alive.py file yet. Ignore that.

Finally, add


Don't run this yet! You still don't have all of the necessary components to run the code. This piece of code will login your bot using a token.

Part 2 - Commands

To add commands to your bot, first add a folder named commands. Next, for each command that you want to add to your bot, add a file named command_here.py. For example, for a ping command, you would add a file named ping.py, or utilities/ping.py inside of the commands folder.

Inside of the file, add

#Import discord
import discord
from discord.ext import commands

#Register the command
async def command_name_here(ctx):
	#action here

def setup(client):

Example for a ping command-

import discord
from discord.ext import commands

async def ping(ctx):
	await ctx.send('Pong! {0}ms'.format(client.latency))

def setup(client):

Part 3 - Keepalive and TOKEN

Now, in your main directory, add a file named keep_alive.py. This will help us start a webserver that you can ping using UptimeRobot (explained) so that your bot stays alive.

NOTE: Your bot will have minimal downtime no matter what each day so that Replit can switch containers and update packages

In your keep_alive.py, add

# Use flask
from flask import Flask
from threading import Thread

#define flask app
app = Flask('')

#create route for home page
def main():
	return "server online!"

#Run our flask app in a thread so that the bot and website can run simultaneously.
def run():
	app.run(host="", port=8080)

def keep_alive():
	server = Thread(target=run)
  • Now, visit UptimeRobot
  • Once you are at your dashboard, add a new monitor.
  • Select mode HTTPS
  • The url to enter is the url that replit provides when the server starts. This will be on top of a white box in the top right corner of your repl.
  • Start monitor

Part 3 1/2 - TOKENS

To obtain a token, visit the Discord Developer Dashboard.

  • Create a new application.
  • Select the Bot tab on the right
  • Click Create a Bot
  • Copy your token
  • Now come back to Replit and click the lock icon in the sidebar.
  • Enter TOKEN as the key. For the value, enter the bot token that Discord gave you.
  • Click Add new secret


And you're done! You can run your code, and your bot will come alive/online!

ichbindede (0)

how can i find the url to to enter in the UptimeRobot