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