[PYTHON] nest cog in discord.py

Premise

The code in this article is just a simplified code for explanation, so only nonsense code will appear. Please note.

cog has become complicated

The discord.py command extension (https://discordpy.readthedocs.io/ja/latest/ext/commands/index.html) has the ability to group several commands and events called cog. in this way

hoge.py


from discord.ext import commands as c

class CogHogeFuga(c.Cog):
    def __init__(self, bot, hoge):
        self._bot = bot
        self._hoge = hoge

    @c.command()
    async def hoge1(self, ctx):
        await ctx.send("hoge")
    
    @c.command()
    async def hoge2(self, ctx):
        foo = await hoge3()
        return foo

    async def hoge3(self):
        return self._hoge

If you write, just loading CogHogeFuga will load both hoge1 and hoge2.

However, as the development progressed, it became as follows.

hoge.py


from discord.ext import commands as c

class CogHogeFuga(c.Cog):
    def __init__(self, bot, hoge):
        self._bot = bot
        self._hoge = hoge
        self._fuga = None

    @c.command()
    async def hoge1(self, ctx):
        await ctx.send("hoge")
    
    @c.command()
    async def hoge2(self, ctx):
        foo = await hoge3()
        await ctx.send(foo)

    async def hoge3(self):
        return self._hoge

    """
Commands added below from here
    """
    
    @c.command()
    async def fuga1(self, ctx):
        return ctx.send("fuga")

    async def fuga2(self, ctx, new_):
        self._fuga = new_

    @c.command()
    async def fuga3(self, ctx):
        foo = await fuga4()
        await ctx.send(foo)

    async def fuga4(self):
        return self._fuga

I added somehow, but the commands related to hoge and the commands related to fuga are mixed. Perhaps there will be more commands related to bar in the future, and I would like to separate them if possible.

However, simply increasing the number of cogs will increase the time and effort required to load the cogs. Both hoge and fuga are similar, so if possible, I would like to do it with just one effort of bot.add_cog (CogHogeFuga (bot)).

nest cog

So, I think it would be nice to be able to nest cogs. In fact, by writing the following, the loading of the cog itself can be left as bot.add_cog (CogHogeFuga (bot)).

hoge.py


from discord.ext import commands as c

class CogHogeFuga(c.Cog):
    def __init__(self, bot, hoge):
        self._bot = bot

        self._bot.add_cog(CogHoge(self._bot, hoge)) #point
        self._bot.add_cog(CogFuga(self._bot)) #point

class CogHoge(c.Cog):
    def __init__(self, bot, hoge):
        self._bot = bot
        self._hoge = hoge


    @c.command()
    async def hoge1(self, ctx):
        await ctx.send("hoge")
    
    @c.command()
    async def hoge2(self, ctx):
        foo = await hoge3()
        await ctx.send(foo)

    async def hoge3(self):
        return self._hoge
    

class CogFuga(c.Cog):
    def __init__(self, bot):
        self._bot = bot
        self._fuga = None

    @c.command()
    async def fuga1(self, ctx):
        return ctx.send("fuga")

    async def fuga2(self, ctx, new_):
        self._fuga = new_

    @c.command()
    async def fuga3(self, ctx):
        foo = await fuga4()
        await ctx.send(foo)

    async def fuga4(self):
        return self._fuga

By passing the bot itself when adding the Cog as described above, we were able to add other Cogs to the bot during initialization.

At the end

Please let me know if there is an easier way.

What is grateful

It is the back of the chiller. As mentioned above, each cog needs to be add_cog to the bot. There is also a function to load the entire file called ʻExtension, but this also eventually executes the setup () method in the file, so you have to write ʻadd_cog here.

I can

――I want to write down highly relevant things together, I want to understand ――I want to avoid editing multiple places with one repair

So, it is realized by nesting Cog. With this writing method, for example, if you want to divide a certain Cog, you only have to rewrite the Cog you want to divide, and there is no need to describe the process of loading a new Cog in another part.

Recommended Posts

nest cog in discord.py
Understand Cog and Extension in discord.py
[Discord.py] Understanding Cog
Nest modules in Boost.Python
Updated messages in discord.py
Pass arguments to Task in discord.py