Skip to content

Adding a commnad

Logicguy edited this page Jan 3, 2023 · 7 revisions

Getting started

Want to add a command to the project? This is the best place to start!

The bot is written using a package called discord-py, you can find its documentation here, A few changes have been made however and that is what we will be covering here.

Firstly to create a command lets look at a quick start example

# /bot/commands/ping.py
from bot.config import Config, Embed
from bot.base import Command

class cmd(Command):
    """ A discord command instance. """

    name = "ping"
    usage = "ping"
    description = "Check the current latency of the bot."

    async def execute(self, arguments, message) -> None:
        embed = Embed(title="Hello world!", description="This is a test embed")
        await message.channel.send("Im alive!!", embed=embed)

In this example we create a class with the name ping, to execute the command we will use <prefix>ping

The usage attribute describes how to use the command, this is useful info for our help system and argument parser. A element wrapped in [] is required while <> is optional.

When the user gives an argument it is possible to wrap it in quotes to input multiple words.

If you want to just catch all the following arguments in a joined one, eg. v!test save This is multiple words in a single arg you can instead use the * operator, how the usage string would look for this could be test <option> [*message], here we define an optional message argument of undefined length.

In your command you have access to self.bot, self.manager, self.db and self.logger, to work with the database we can look at the following example

Accessing the database

from bot.config import Config, Embed
from bot.base import Command

class cmd(Command):

    name = "request"
    usage = "request <title> [description]"
    description = "Add some recommendation to the server. Support multiple commands to deal with."

    async def execute(self, arguments, message) -> None:
        # [...]

        cursor = await self.db.cursor()

        await cursor.execute(
            "INSERT INTO request(Title, Description) VALUES(?, ?)", (title, text_info))
        await self.db.commit()

        await cursor.close()

        # [...]

Simply we access the self.db attribute to open a cursor and access our data.

Embeds

In the project we changed a bit how embeds work, we have a default format that of cause can be overwritten if necessary. You can use all the default attributes and methods from the discord py documentation.

As well as this we implemented a set color method to set the default color, by default embed colors are green. You need to use this to set the color of the embed, here is an example.

from bot.config import Config, Embed

embed = Embed(title="Hello world!")
embed.set_color("red")

The embed object will now be red. Supported colors currently are:

image

The v1rbox embeds follow the same template to have a more consistent look and experience for the end user.