Skip to content

Can I add my own commands?

Tien Dat Pham edited this page Jul 28, 2023 · 6 revisions

Absolutely yes!

Nameless*:tm: is a source-code-based bot, so the entire code is yours!

Method 1: Create your own Cog file

To create a set of commands (which we will refer as Cog by then), simply create a Python file with name ended with Cog, of course with extension .py - for example: HelloWorldCog.py then put it in the cogs directory.

Template code

You can use this as your starting point!

from discord.ext import commands

from nameless import Nameless, shared_vars
from NamelessConfig import NamelessConfig

class HelloWorld(commands.Cog):
    @commands.hybrid_command()
    @app_commands.guilds(*getattr(NamelessConfig, "GUILDS", []))
    async def hello_there(self, ctx: commands.Context):
        """Sends a lovely 'Hello'"""
        await ctx.send("Hello {ctx.author.mention} o/")

async def setup(bot: Nameless):
    await bot.add_cog(HelloWorld(bot))
    logging.info("Cog of %s added!", __name__)


async def teardown(bot: Nameless):
    await bot.remove_cog("HelloWorld")
    logging.warning("Cog of %s removed!", __name__)

Method 2: Use provided script

The script is located here: tools/gen-cog.py

python tools/gen-cog.py [your-cog-name-without-Cog]

example: python tools/gen-cog.py Maimai

Add to the main config

COGS = [
    "HelloWorld",  # Just put the ENTIRE FILE NAME without the 'Cog.py', we don't provide support for this.kind.of.moduleCog.py
]

Known issues

  • Command duplication - your command appears twice when using it as a slash command: read this!

Imagine a bot that is LGPL!

Also support us some coffee please!

Clone this wiki locally