This page is generated from
docs/guide_cog_creation.rst- please edit the source file instead of this page.
Creating cogs for Red
This guide serves as a tutorial on creating cogs for Red. It will cover the basics of setting up a package for your cog and the basics of setting up the file structure. We will also point you towards some further resources that may assist you in the process.
Getting started
To start off, be sure that you have installed Python 3.11. Next, you need to decide if you want to develop against the Stable or Develop version of Red. Depending on what your goal is should help determine which version you need.
ATTENTION
The Develop version may have changes on it which break compatibility with the Stable version and other cogs. If your goal is to support both versions, make sure you build compatibility layers or use separate branches to keep compatibility until the next Red release
- Open a terminal or command prompt and type one of the following
- Stable Version:
python3.11 -m pip install -U Red-DiscordBot
NOTE
To install the development version, replace Red-DiscordBot in the above commands with the
value below. The development version of the bot contains experimental changes. It is not
intended for normal users. We will not support anyone using the development version in any
support channels. Using the development version may break third party cogs and not all core
commands may work. Downgrading to stable after installing the development version may cause
data loss, crashes or worse. Please keep this in mind when using the development version
while working on cog creation.
Red-DiscordBot @ https://github.com/Cog-Creators/Red-DiscordBot/tarball/V3/develop
(Windows users may need to use py -3.11 or python instead of python3.11)
Setting up a package
To set up a package, we would just need to create a new folder.
This should be named whatever you want the cog to be named (for
the purposes of this example, we'll call this mycog).
In this folder, create three files: __init__.py,
mycog.py, and info.json. Open the folder in
a text editor or IDE (examples include Sublime Text 3,
Visual Studio Code, and
PyCharm).
ATTENTION
While you can intentionally override Red's cogs/extensions, this may break things. We would prefer if people wanted custom behavior for any core cog/extension, an issue and/or PR is made Overriding Permissions specifically is dangerous.
Subclassing to make changes to Red's cogs/extensions may not be a safe way to stay up to date either, as changes to cogs and their interactions with red are not guaranteed to not be breaking.
Any cogs doing this are doing so at their own risk, and should also inform users of associated risks.
Creating a cog
With your package opened in a text editor or IDE, open mycog.py.
In that file, place the following code:
from redbot.core import commands
class MyCog(commands.Cog):
"""My custom cog"""
def __init__(self, bot):
self.bot = bot
@commands.command()
async def mycom(self, ctx):
"""This does stuff!"""
# Your code will go here
await ctx.send("I can do stuff!")
Open __init__.py. In that file, place the following:
from .mycog import MyCog
async def setup(bot):
await bot.add_cog(MyCog(bot))
Make sure that both files are saved.
Testing your cog
To test your cog, you will need a running instance of Red.
Assuming you installed Red as outlined above, run redbot-setup
and provide the requested information. Once that's done, run Red
by doing redbot <instance name> --dev to start Red.
Complete the initial setup by providing a valid token and setting a
prefix. Once the bot has started up, use the link provided in the
console to add it to a server (note that you must have the
Manage Server (or Administrator) permission to add bots
to a server). Once it's been added to a server, find the full path
to the directory where your cog package is located. In Discord, do
[p]addpath <path_to_folder_containing_package>, then do
[p]load mycog. Once the cog is loaded, do [p]mycom
The bot should respond with I can do stuff!. If it did, you
have successfully created a cog!
NOTE
Package/Folder layout
You must make sure you structure your local path correctly or
you get an error about missing the setup function. As cogs are
considered packages, they are each contained within separate folders.
The folder you need to add using [p]addpath is the parent
folder of these package folders. Below is an example
- D:\
-- red-env
-- red-data
-- red-cogs
---- mycog
------ __init__.py
------ mycog.py
---- coolcog
------ __init__.py
------ coolcog.py
You would then use [p]addpath D:\red-cogs to add the path
and then you can use [p]load mycog or [p]load coolcog
to load them
You can also take a look at our cookiecutter, for help creating the right structure.
Publishing your cog
Go to Publishing cogs for Red
Becoming an Approved Cog Creator
Becoming an Approved Cog Creator explains the Cog Creator Application process and lists requirements and good practices for Cog Creators. This information is worth following for anyone creating cogs for Red, regardless of if you plan to publish your cogs or not.
Where to go from here?
Now that you have a working cog, here are some resources to help you take the next step:
- discord.py - Red is built on top of discord.py, so its documentation is the place to look for everything related to Discord itself, such as working with messages, members, channels, embeds and views. Red's own commands framework extends discord.py's, see Commands Package.
- Storing data - Use Config to store settings and other data of your cog instead of writing files yourself, and Data Manager to get the paths for any files your cog needs.
- Slash commands and interactions - Slash Commands and Interactions explains how to add application commands, buttons and other interactions to your cog.
- Utilities - Red provides many helpers for common tasks, like formatting messages, paginating long output or asking for confirmation, see Utility Functions.
- Other framework APIs - Check out the rest of the "Red Development Framework Reference" section in the sidebar, e.g. Bank, Mod log, Command Check Decorators and Internationalization Framework.
- Publishing your cog - Once you're ready to share your cog with others, Publishing cogs for Red explains how to create a repository that can be added with Downloader.
- Getting help - If you get stuck, ask in the
#codingchannel of the Red - Discord Bot server and have a look at the source of existing cogs to see how others solved similar problems.
Installation Guides
- Installing Red
- Creating a bot account
- Updating Red
- Backing Up and Restoring Red
- About Virtual Environments
- Setting up auto-restart using batch on Windows
- Setting up auto-restart on Mac
- Setting up auto-restart using systemd on Linux
Cog Reference
User guides
- Getting started
- Running Red on Fluxer
- About Privileged Intents
- About public (big) bots
- Admin
- Alias
- Audio
- Cleanup
- Cog Manager UI
- Core
- CustomCommands
- Dev
- Downloader
- Economy
- Filter
- General
- Image
- Permissions
- Mod
- ModLog
- Mutes
- Reports
- Streams
- Trivia
- Warnings
- Red and End User Data
Cog Creation Guides
- Creating cogs for Red
- Slash Commands and Interactions
- Publishing cogs for Red
- Becoming an Approved Cog Creator
- Versioning
- End-user Guarantees
- Developer Guarantees
- Breaking Change Notices
Cog Development Framework Reference
- Shared API Keys
- Bank
- Bot
- Command Check Decorators
- App Command Check Decorators
- Commands Package
- App Commands Package
- Config
- Data Manager
- Custom Events
- Internationalization Framework
- Mod log
- RPC
- Tree
- Utility Functions
Others
Legacy (Red V2)