最近我决定重写我的不和谐机器人并添加按钮。到目前为止我遇到的主要问题是,我无法在按下按钮后立即禁用按钮,人们告诉我,事实上button.disabled=True,它会禁用按钮,但它只是将其禁用,所以它永远不会被禁用按下。我想要的是能够单击它并执行它的操作,然后禁用它。
作为参考,我将放置一些代码
我使用 disnake,一个discord.py 分支,它确实具有与 dpy 相同的语法,但我们有按钮和斜杠命令、下拉菜单等
class BlurpleButton(Button):
def __init__(self, label, emoji=None):
super().__init__(label=label, style=discord.ButtonStyle.blurple, emoji=emoji)
Run Code Online (Sandbox Code Playgroud)
这是为了更容易使用按钮,我创建了一个模板,我可以在任何命令上使用它
class CustomView(View):
def __init__(self, member: disnake.Member):
self.member = member
super().__init__(timeout=180)
async def interaction_check(self, inter: disnake.MessageInteraction) -> bool:
if inter.author != self.member:
await inter.response.send_message(content="You don't have permission to press this button.", ephemeral=True)
return False
return True
Run Code Online (Sandbox Code Playgroud)
这是为了按钮只能由提到的成员按下,例如,如果我这样做/test @member(由于不和谐新的特权意图,我迁移到斜杠命令),那么只有该成员才能按下它,而其他人则无法按下。
到目前为止,一切正常,现在我们在命令中“组装”它之后
@commands.slash_command(description='test')
async def test(self, inter):
(do stuff in there)
. . .
button1 = BlurpleButton("Button name")
view=CustomView(member)
view.add_item(button1)
async …Run Code Online (Sandbox Code Playgroud)