#Custom Properties And Attributes

1 messages · Page 1 of 1 (latest)

gentle flare
#

In the past there was only one context that I could subclass and add my own properties. But now there is ApplicationContext and Interaction. I want to be able to access my db instance from both by doing interaction.db and ctx.db respectively. First is there a way to do this without making 2 subclasses that look exactly the same besides the name. Second for ApplicationContext I need to subclass the bot and use this code

    async def get_application_context(self, interaction: Interaction, cls=None) -> discord.ApplicationContext:
        return await super().get_application_context(interaction, cls=cls or AdvContext)

But I am not seeing an equivalent function to override for interaction.

This is my full file right now

class AdvContext(discord.ApplicationContext):
    @property
    def prespond(self, *args, **kwargs):
        return PrettyRespond(self)
class AdvInteraction(discord.Interaction):
    @property
    def prespond(self, *args, **kwargs):
        return PrettyRespond(self)


setattr(discord.ApplicationContext, "db", database)
setattr(discord.Interaction, "db", database)


class SurveyWolf(discord.Bot):
    async def get_application_context(self, interaction: Interaction, cls=None) -> discord.ApplicationContext:
        return await super().get_application_context(interaction, cls=cls or AdvContext)
dark oriole
#

interaction doesnt have a built-in way to override it

gentle flare
#

So we currently can not add properties to Interaction without major overwrites?

cloud canyon
#

Easiest way to do it

gentle flare
#

I want to be able to also have a class that takes an instance of interaction as an argument, so I dont think that would work.

dark oriole
#

interaction has a client attr

#

that can be used to access the bot iirc

gentle flare
#

👍 thanks