我正在try 创建一个小型的、可互换的双按钮类,我可以在任何cog中调用它,而不是反复编写相同的代码.除了按钮的标签外,其他一切都正常工作,我无法找到如何从变量中赋值.它被称为opt1
或self.opt1
,但都失败了,因为我确实理解这两个词在技术上都没有"定义",但我似乎找不到任何替代品.
该类的代码段如下所示:
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
这是否可以修复?如果是这样的话,是否有一种实际的方法可以将变量放入decorator 中,而不必 for each 用例创建单独的类?