- A lot of this was copy/pasta from Discord.js Guide, which I appropriated and converted/enhanced for discordppy.guide
- DiscordJS Guide
- As the python guide is just starting, we of course will not have everything the DiscordJS guide has. As such, make sure to go back to the DiscordJS guide as needed as they have a huge community constantly updating the documentation.
If you are reading this, it probably means you want to learn how to make a bot with discord.py. Awesome! You've come to the right place. This guide will teach you things such as:
- How to get a bot up and running from scratch;
- How to properly create, organize, and expand on your commands;
- How to use the best practices for common situations;
- and much more. This guide will also cover subjects like common errors and how to solve them, keeping your code clean, setting up a proper development environment, etc. Sounds good? Great! Let's get started, then.
Alright, making a bot is cool and all, but there are some prerequisites to it. To create a bot with discord.py, you should have a fairly decent grasp of Python itself. While you can make a bot with very little Python and programming knowledge, trying to do so without understanding the language first will only hinder you. You may get stuck on many uncomplicated issues, struggle with solutions to incredibly easy problems, and all-in-all end up frustrated. Sounds pretty annoying.
If you don't know Python but would like to learn about it, here are a few links to help get you started:
- CodeCademy's interactive Python3 course
- Books (If you decide to only read a few books, make sure you read those):
- Dive Into Python3, a free online book.
- Problem Solving with Algorithms and Data Structures using Python a free online course!
- Automate the boring stuff with Python
- Python Cookbook
- Think Python: How to Think Like a Computer Scientist
- Python Official Documentation
- Google, your best friend
- Take your pick, learn some Python, and once you feel like you're confident enough to make a bot, come back and get started!
NOTE It is important you follow these in sequence.
- First, you need Python3
- Then, you need discord.py to be installed/added to Python3
- Then, you need RED bot to be installed
- Then, you need a discord bot account to be created, and invited
- Then, you need RED bot to be setup
- Finally, you can start interacting with cogs, and writing your own!
- RED's guide to Creating a Bot Account
- Just for your convenience, the Javascript guide for the same below:
- Writing a bot always follows the same basic steps:
- Setup your cog/bot directory structure as follows RED guide to setting up a package:
<cog_folder_name>/
├── README.md
├── __init__.py
├── info.json
└── <your_cog_name>.py
-
Setup your README.md file:
- This file should have enough information about what the cog does, all commands, things that can help users and/or future developers to use it or code/upgrade it. Imagine that you are waking up from a 30 year coma, and you have to go back to enhancing/fixing your code... this file should really help you get started!
- README Markup Syntax
-
Setup your info.json file as follows:
- NOTE: RED Info.json documentation
- NOTE2: if you need to learn what JSON format is
{
"author": [
"<yourname>",
"Legend Clan Development Team"
],
"description": "<Add a descriptive, informative description of what this cog does>",
"short": "<Add a shorter description, a summary of the above one>",
"tags": [
"<(list of strings) - A list of strings that are related to the functionality of the cog. Used to aid in searching.>",
"<.e.g greeter>"
],
"requirements": [
"<list python import dependencies, one per line>",
"e.g. pytz"
],
"type": "COG"
}
- Setup your skeleton code as follows:
from redbot.core import commands
class Mycog(commands.Cog):
"""My custom cog"""
@commands.command()
async def mycom(self, ctx):
"""This does stuff!"""
# Your code will go here
await ctx.send("I can do stuff!")
- Setup your
__init__.py
file as follows:
from .mycog import Mycog
def setup(bot):
bot.add_cog(Mycog())
-
Now, code your bot (Make sure that you follow Redjumpman coding guide)
- Things to look out for:
- Using configuration files (persistent data)
- Building Embeds
- The following tool can help you tremendously with Embeds: EMBED visualizer
- Things to look out for:
-
Check our very simple first tutorial_cog for an idea:
__init__.py
from .tutorial_cog import Tutorial_Cog
def setup(bot):
bot.add_cog(Tutorial_Cog(bot))
tutorial_cog.py
from redbot.core import commands
class Tutorial_Cog(commands.Cog):
"Minimal tutorial bot"
def __init__(self, bot):
self.bot = bot
@commands.group()
async def simple_cog(self, ctx):
pass
@simple_cog.command()
async def hello(self, ctx, *, message):
"Says something in a text channel"
await ctx.send(f"Cog says: Hello World! {message}")
- Then, on your discord test server, enter the following commands:
- !load tutorial_cog
- !help simple_cog
- !simple_cog hello 'My message'
- And you can always reload the cog by doing:
- !reload tutorial_cog