[Python] Access class properties or methods from within a commands.Command

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I'm building a Discord bot. The bot should store some information into some internal variables to be accessed at a later time. To do so I'm structuring it as a class (as opposed to many examples where the commands are outside a class definition). However, I discovered that when you use the @commands.command(name='test') decorator, the method becomes a kind of "static" method and no longer receives the object as first input.

    Given this, is there any way I can access class properties (such as an_instance_property in the example below) and/or class methods (such as a_class_method in the example below)?

    If this is the wrong approach, what could be a better approach for a bot with an internal state?

    import discord
    from discord.ext import commands

    with open('TOKEN', 'r') as f:
    TOKEN = f.read()

    class mybot(commands.Bot):
    def __init__(self):
    intents = discord.Intents.default()
    super().__init__(command_prefix="!", intents=intents)
    self.add_command(self.test)

    self.an_instance_property = [] # <----

    def a_class_method(x): # <----
    return x

    @commands.command(name='test')
    async def test(ctx, *args):
    # How can I access self.an_instance_property from here?
    # How can I call self.a_class_method from here?
    return

    bot = mybot()
    bot.run(TOKEN)

    Continue reading...

Compartilhe esta Página