Skip to content
Sign upLog in
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs

Discord Bot Template w/ Cogs

Discord.PY Bot Template w/ Cogs

By LuxAsani

So you wanna make a bot with Well you've come to the wrong right place.
(Though you should probably follow a tutorial if you don't know what you're doing)

In this readme I'll explain a few of the files and how to operate them, as well as what some things do. If you need any help, there's one thing all people can do. Google!

With that in mind, thank you for using this template- Lets get on with the explaination file

The file is what does most of the work in getting the bot set up and running. I suggest not changing this unless you know what you're doing, you're adding to the cogs, or you're editing the prefix. (It's a bit sensitive, y'know)

Please note that it will NOT ruin your bot, so don't be too scared to experiment! You can always restore this if you really mess something up.

1.1 - Imports & Variables

As you can see, we have imports at the top of our file. These are some of the ones I suggest keeping for a basic bot, but if you want to find any more you can do some research and use them in your own bot!

Let me give you a run-down on what all of these do

import discord - The basic functions for discord bots

import os - Has many useful commands, but in this template it's mainly used for accessing env variables

import time - Can be used for various things for later on, but is optional

from keep_alive import keep_alive - The file that will help keep your bot running ~24/7 (Will be addressed later)

from discord.ext import commands - The needed function for any bot, helps use commands and accessing discord's servers in general (Literally can't do without e.g. bot = commands.Bot(command_prefix = "!"))

All of these have their own uses to extend python in order for you to make your bot the way you want to. Remember that you can add your own extensions too, and the best part: it's not only for discord bots! (Well you probably figured that out)

There are also a few variables we can go over here to give you a better understanding of what they are and why we need them.

intents = discord.Intents.default() - Shortens the discord.Intents.default() attribute to make it easier to use for later reasons.

intents.members = True - Makes use of the intents variable. It tells the bot to get a list of the members in the servers it's in.

NOTE: Intents are only usable with Server Members Intent enabled from the Discord Developer Portal

bot = commands.Bot(command_prefix = "!") - A variable to hold the bot object. It's mainly for quick use- (Like who wants to write out that whole mess whenever calling for the bot)

cogs = { "example" } - A table to hold all command cogs (Will be talked about later) It will be easier to call the cogs from a table instead of writing each out individually.

These variables will work with the rest of the program way faster than we could. It's a lazy way of calling big things, and as a programmer- Lazyness is our speciality (I mean for real we get computers to do stuff automatically instead of doing it ourselves)

1.2 - Loading Cogs

If you look at the file, you should see this jumble of stuff.

for filename in os.listdir("./cogs"): if filename.endswith(".py"): bot.load_extension(f"cogs.{filename[:-3]}")

Don't worry, though. All it does is load the cogs for you automatically instead of you having to type bot.load_extension("cog") each time you make a new one.

In the for statement (for filename in os.listdir("./cogs"):) it's just seeing however many files there are in the cogs folder, and running that loop as many times as there are files. (e.g. If there are 4 files it will run 4 times)

The if filename.endswith(".py"): just double checks if the file ends with the .py extension. Basically just making sure it's a cog.

The bot.load_extension(f"cogs.{filename[:-3]}" loads each cog for the bot to access.

It's nothing too hard so don't worry, you won't need to change anything here!

1.3 - On Ready

The @bot.event you see near the end of the page is there to check when the bot comes online. Its a super useful event which can help set the bot status as well as many other things.

Not much to go over- and this function is optional, too.

1.4 - keep_alive()

Keep Alive is a function to make sure your bot stays online with the help of a third-party pinging source. It's a more tedious task to do and isn't always reliable, but if you're not happy with it- Replit provides a Hacker Plan which will allow you to keep your repl always on. (The Hacker Plan has so much more though, it's very good I urge you to check it out- only 7$/month too.)

But for the poor people (/j) we can use keep_alive(). The file you may have noticed earlier is a flask app. Flask apps are basically web pages in python, so you can use that however you like, but I suggest not messing with it unless you know what you're doing.

Now lets see how to actually use it- If you run your bot (given you have the TOKEN in a env variable (scroll up to see that if you haven't)), you should see a web page appear above your console on the right side of your screen. Copy the url to that page.

Now you can use any one of the million pinging websites. Personally I suggest Freshping or UptimeRobot but you can use anything you find!

Then when it asks you for a url to ping, input the one from your webpage.

1.5 - Tokens

On the left of your developing enviornment there should be a box displaying the files. If you go down to that little lock button and click it you'll be greeted with a new screen. This is how we'll make our environment variables.

Enviornment Variables are special secret variables that nobody but the developer can see. If you didn't know, people such as yourself can click into other's replit projects and see their code. It's a super useful feature, but also a bit dangerous when developing a discord bot.

When making a discord bot, your bot will need access to the user, otherwise it can't connect to the discord account. This is done through a bot token. To find your bot token you can go here. It also has a tutorial available for those of you who don't know how to use (Yes, if you weren't aware this isn't a tutorial)

After you get your bot token, you can add it to a secret! Put the name of your secret (in this case we'll use TOKEN ) and then paste your bot token into the value box. Then click Add new secret .

Example Cog

2.1 - Class

Class is how you declare the title of the cog. As you can see in the file, the class declaration looks like this:

class ExampleCog(commands.Cog): # Code #

Be sure to pass the commands.Cog attribute into the parenthesis. There is also some funky quotes with some text in them """Example Commands""". That's known as the cog description, giving you a summary of what to expect in that category of commands.

2.2 - Init

The __init__ event inside of cogs are what runs as soon as the cog is loaded, and will always contain this:

def __init__(self, bot: commands.Bot): = bot

You could add other things, but I advise against it

2.3 - Commands

Commands in cogs are easier to edit, access, and categorize. Making a command is super easy when you have the basics of down. For this we'll assess the command we've provided you with:

@commands.command() async def ping(self, ctx: commands.Context): """Display the bot latency""" await ctx.send(f"Pong! | {round( * 1000)}ms")

The @commands.command() decorator is used to declare the start of a command. There're more decorators and attributes you can use here, but we left those out for now.

async def ping(): is naming the command. Currently, the ping command will get it's trigger from this statement (e.g. you have to do !ping). But you can also include a name in the decorator which will change it. Example:

@commands.command(name="ping") async def super_radical_command():

If we hadn't added the name="ping" as an attribute, we would need to type !super_radical_command every single time. Very annoying, do not reccomend.But since we declared a name, it will use that name instead of the name the command was given in the async statement.

The quotes will add a description to the command, just like the quotes did to the cog.

2.4 - Setup

The setup function will set the cog up for loading.

DO NOT- and I repeat DONT. RENAME. THIS. FUNCTION. will specifically look for the setup function in cogs! Without it, your cog will not work.


Thank you for using this template. I worked hard on it and I hope it helps some of you get a headstart on a discord bot. If you have any questions I will gladly answer them!

Have a good day <3

- LuxAsani

1 year ago
You are viewing a single comment. View All


To get a bot to reply, just replace the await ctx.send() to await ctx.reply()

You can also make it not mention the person if you do await ctx.reply("Hello!", mention_author=False)


An example of what it will look like, provided by Frederick Reynolds on Stack Overflow (Page Link)

1 year ago