使用 class 实例变量在装饰器中定义按钮标签 (discord.py 2.0.0)
Use class instance variable to define a button label within the decorator (discord.py 2.0.0)
我正在尝试制作一个小型的、可互换的双按钮 class,我可以在任何齿轮中调用它,而不是一遍又一遍地写出相同的代码。一切正常,除了按钮的标签,我无法从变量中找到如何分配。称为 opt1
或 self.opt1
,它失败了,因为我理解这两者在技术上都没有“定义”,但我似乎找不到任何替代方案。
class的片段如下:
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
这可以修复吗?如果是这样,是否有一种实际方法可以将变量放入装饰器中,而无需为每个用例单独制作 class?
说明
您可以使用 View.add_item
动态添加按钮。这可以在 __init__
中完成,其中按钮的定义可以访问实例属性。
重要变化:
按钮回调仅将 interaction
作为参数 - 您已经可以访问按钮,回调只是一个函数,而不是方法(不需要 self
).
注意 Button
的大写字母 'B'。它是 class 的构造函数,而不是装饰器。
代码
class TestView(discord.ui.View):
def __init__(self, example_var):
self.example_var = example_var
super().__init__(timeout=None)
self.add_buttons()
def add_buttons(self):
button_one = discord.ui.Button(label=self.example_var)
async def button_example(interaction: discord.Interaction):
print(self.example_var)
button_one.callback = button_example
self.add_item(button_one)
参考
我正在尝试制作一个小型的、可互换的双按钮 class,我可以在任何齿轮中调用它,而不是一遍又一遍地写出相同的代码。一切正常,除了按钮的标签,我无法从变量中找到如何分配。称为 opt1
或 self.opt1
,它失败了,因为我理解这两者在技术上都没有“定义”,但我似乎找不到任何替代方案。
class的片段如下:
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
这可以修复吗?如果是这样,是否有一种实际方法可以将变量放入装饰器中,而无需为每个用例单独制作 class?
说明
您可以使用 View.add_item
动态添加按钮。这可以在 __init__
中完成,其中按钮的定义可以访问实例属性。
重要变化:
按钮回调仅将
interaction
作为参数 - 您已经可以访问按钮,回调只是一个函数,而不是方法(不需要self
).注意
Button
的大写字母 'B'。它是 class 的构造函数,而不是装饰器。
代码
class TestView(discord.ui.View):
def __init__(self, example_var):
self.example_var = example_var
super().__init__(timeout=None)
self.add_buttons()
def add_buttons(self):
button_one = discord.ui.Button(label=self.example_var)
async def button_example(interaction: discord.Interaction):
print(self.example_var)
button_one.callback = button_example
self.add_item(button_one)