如何使用discord.py机器人禁用特定按钮

huangapple go评论105阅读模式
英文:

how to disable specific buttons with discord.py bot

问题

当我点击"stats"按钮时,我需要让"shiny"按钮变为不可用!我之前做过类似的事情,但是是为了同一个我点击的按钮。我使用了button.disabled = True,它有效果,但是要通过点击"stats"按钮来禁用另一个按钮,我无法做到!你能帮忙吗?

英文:

I would like to know if there is a way for me to disable a button after I click on another one. Example:

如何使用discord.py机器人禁用特定按钮

when I click on the stats button, I need the shiny button to be disabled!

I did something similar, however for the same button I clicked:

如何使用discord.py机器人禁用特定按钮

I used the button.disabled = True and it worked, but to disable another button also by clicking on the stats button, I couldn't! Can you help?

答案1

得分: 2

好的,以下是代码部分的翻译:

  1. class View(discord.ui.View):
  2. def __init__(self):
  3. super().__init__()
  4. @discord.ui.button(label="shiny")
  5. async def shiny_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
  6. pass
  7. @discord.ui.button(label="stats")
  8. async def stats_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
  9. # 在我们处理统计信息的时候,先延迟响应
  10. await interaction.response.defer()
  11. message = interaction.message
  12. # 查找带有 "shiny" 标签的按钮并禁用它
  13. # 保留对正确按钮的引用,以便以后启用它
  14. shiny_button = None
  15. for child in self.children:
  16. if type(child) == discord.ui.Button and child.label == "shiny":
  17. shiny_button = child
  18. child.disabled = True
  19. break
  20. # 在 Discord 中更新视图
  21. await interaction.message.edit(content=message.content, view=self)
  22. # 完成 "stats" 按钮应该执行的其余操作
  23. # 实际发送跟进消息给用户
  24. await interaction.followup.send("stats")
  25. # 如果我们想要重新启用另一个按钮,可以在这里完成
  26. # 已注释掉的部分
  27. # shiny_button.disabled = False
  28. # await interaction.message.edit(content=message.content, view=self)
  29. @discord.ui.button(label="back")
  30. async def back_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
  31. pass

希望这个翻译对你有所帮助。如果有任何其他问题,请随时提问。

英文:

So, assuming you have a View class that inherits from discord.ui.View and your buttons are defined like the following. We can use the self property on the View class to get the view's children (ie; buttons, selects, etc) and then iterate over them and looking for buttons that have the shiny label.

  1. class View(discord.ui.View):
  2. def __init__(self):
  3. super().__init__()
  4. @discord.ui.button(label="shiny")
  5. async def shiny_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
  6. pass
  7. @discord.ui.button(label="stats")
  8. async def stats_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
  9. # defer whilst we work out stats stuff
  10. await interaction.response.defer()
  11. message = interaction.message
  12. # find the shiny button and disable it
  13. # keep a reference to the right button in case we want to enable it later
  14. shiny_button = None
  15. for child in self.children:
  16. if type(child) == discord.ui.Button and child.label == "shiny":
  17. shiny_button = child
  18. child.disabled = True
  19. break
  20. # update the view in discord
  21. await interaction.message.edit(content=message.content, view=self)
  22. # do the rest of what the `stats` button should do
  23. # actually send followup message to user
  24. await interaction.followup.send("stats")
  25. # if we wanted to re-enable the other button we can do it here
  26. # commented out
  27. # shiny_button.disabled = False
  28. # await interaction.message.edit(content=message.content, view=self)
  29. @discord.ui.button(label="back")
  30. async def back_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
  31. pass

The example is quite simple; we defer to stop discord complaining we didn't respond, then we do what I said and then edit the message immediately to disable the shiny button to prevent anyone from pressing it. Then you can do your logic. then we can send our followup message to let the user know we did what we should have done. And then, optionally, we re-enable the button again.

huangapple
  • 本文由 发表于 2023年2月8日 10:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定