'Use class instance variable to define a button label within the decorator (discord.py 2.0.0)
I'm attempting to make a small, interchangeable two-button class that I can call in any cog instead of writing out the same code over and over. Everything works fine except for the buttons' labels, which I cannot find out how to assign from the variables. Called as opt1 or self.opt1, it failed since neither are technically "defined" which I do understand, but I can't seem to find any alternatives.
The snippet of the class is as follows:
class twoButton(discord.ui.View):
def __init__(self, author, opt1="Yes", opt2="No", timeout=0):
self.author = author
self.opt1 = opt1
self.opt2 = opt2
super().__init__(timeout=timeout)
@discord.ui.button(label=opt1, style=discord.ButtonStyle.blurple, custom_id="opt1")
async def buttonOne(self, interaction: discord.Interaction, button: discord.ui.Button):
button.style = discord.ButtonStyle.green
self.buttonTwo.style = discord.ButtonStyle.gray
await interaction.response.edit_message(view=self)
@discord.ui.button(label=opt2, style=discord.ButtonStyle.blurple, custom_id="opt2")
async def buttonTwo(self, interaction: discord.Interaction, button: discord.ui.Button):
button.style = discord.ButtonStyle.green
self.buttonOne.style = discord.ButtonStyle.gray
await interaction.response.edit_message(view=self)
async def interaction_check(self, interaction: discord.Interaction):
return interaction.user.id == self.author.id
Is this possible to fix? If so, is there an actual method for getting a variable into a decorator without making a separate class for every use case?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
