Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Привет. Как добавить переменную user в класс? class vinfo(discord.ui.View):…

  1. User

    Привет. Как добавить переменную user в класс?

    class v_info(discord.ui.View): @discord.ui.button(label="button 1", style=discord.ButtonStyle.primary) async def click0(self, interaction, button): await getgud(user.id) # здесь используется требуемая переменная user @discord.ui.button(label="button 2", style=discord.ButtonStyle.primary) async def click1(self, interaction, button): await getgud(user.id) # здесь используется требуемая переменная user класс используется: @tree.command(name = "info", description = "info command") async def info(interaction, user: discord.User = None): await interaction.response.send_message(content = 'select info type', view=v_info(user=user)) # здесь указывается переменная user

  2. Assistant

    Для добавления переменной user в класс v_info, вы можете изменить его конструктор и сохранить значение user в атрибуте класса.

    class v_info(discord.ui.View):
        def __init__(self, user, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.user = user
    
        @discord.ui.button(label="button 1", style=discord.ButtonStyle.primary)
        async def click0(self, interaction, button):
            await getgud(self.user.id) # используем переменную user
    
        @discord.ui.button(label="button 2", style=discord.ButtonStyle.primary)
        async def click1(self, interaction, button):
            await getgud(self.user.id) # используем переменную user
    

    Затем в функции info вам необходимо передать значение переменной user при создании экземпляра v_info.

    @tree.command(name="info", description="info command") 
    async def info(interaction, user: discord.User = None):
        await interaction.response.send_message(content='select info type', view=v_info(user=user))
    

    Теперь переменная user будет доступна внутри класса v_info через атрибут self.user.