Skip to content

Using the category system

Logicguy edited this page Jan 3, 2023 · 3 revisions

The basic setup

The category system works using dictionaries in the main command folder, currently subcategories are not supported.

To setup a new category we first create a unique folder in /bot/commands/, the name is not important however we need it later.

Now to configure the category we use class within the file at /bot/category.py.

# /bot/category.py

# [...]

class FunCategory(Category):
    """ A command category instance. """
    name = "fun"
    prefix = None
    commands: List[Command] = []

    def check_permissions(self, message: discord.Message) -> bool:
        return True

# [...]

Do not edit commands variable, that is used by the system

Here we define a super simple category with no special settings other than the ability to group commands neatly.

Notice the name fun has to be the same as the name of the folder that we made earlier, this is how the program will reference the commands.

Also not that the name of the class has to be suffixed with ...Cateogry, case sentensive

Using permissions

To setup permissions we want to utilise the check_permissions method, in the example above it was simply set to always return true but here is a different example

# /bot/category.py

# [...]

class ModCategory(Category):
    """ A command category instance. """
    name = "mod"
    prefix = None
    commands: List[Command] = []

    def check_permissions(self, message: discord.Message) -> bool:
        # Check for a specific role in the member
        return any([i.id == config.mod_role_id for i in message.author.roles])

# [...]

As we have an exposed argument message we can access the author and other information about the command, you have complete creative freedom here, only thing it expects a returned boolean value (true or false) :D

Setting up command prefixes

Lets start with the obvious question, what is a command prefix?

A command prefix is a category that has a bunch of sub commands that we all want to start with the same trigger keyword

Say we have the command ban and kick and we want a common entry keyword like v!mod ban and v!mod kick.

Using the example above agein we could set this up using

# /bot/category.py

# [...]

class ModCategory(Category):
    """ A command category instance. """
    name = "mod"
    prefix = "mod" # <--
    commands: List[Command] = []

    def check_permissions(self, message: discord.Message) -> bool:
        # Check for a specific role in the member
        return any([i.id == config.mod_role_id for i in message.author.roles])

# [...]

And now we have the behavior we described before

In your command you want to have the typical usage text without including the category prefix, if it was kick <user> <reason> before you want to keep it this way, nothing has changed here, that's also one of the great things about this framework :D