英文:
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:
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:
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
好的,以下是代码部分的翻译:
class View(discord.ui.View):
def __init__(self):
super().__init__()
@discord.ui.button(label="shiny")
async def shiny_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
pass
@discord.ui.button(label="stats")
async def stats_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
# 在我们处理统计信息的时候,先延迟响应
await interaction.response.defer()
message = interaction.message
# 查找带有 "shiny" 标签的按钮并禁用它
# 保留对正确按钮的引用,以便以后启用它
shiny_button = None
for child in self.children:
if type(child) == discord.ui.Button and child.label == "shiny":
shiny_button = child
child.disabled = True
break
# 在 Discord 中更新视图
await interaction.message.edit(content=message.content, view=self)
# 完成 "stats" 按钮应该执行的其余操作
# 实际发送跟进消息给用户
await interaction.followup.send("stats")
# 如果我们想要重新启用另一个按钮,可以在这里完成
# 已注释掉的部分
# shiny_button.disabled = False
# await interaction.message.edit(content=message.content, view=self)
@discord.ui.button(label="back")
async def back_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
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.
class View(discord.ui.View):
def __init__(self):
super().__init__()
@discord.ui.button(label="shiny")
async def shiny_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
pass
@discord.ui.button(label="stats")
async def stats_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
# defer whilst we work out stats stuff
await interaction.response.defer()
message = interaction.message
# find the shiny button and disable it
# keep a reference to the right button in case we want to enable it later
shiny_button = None
for child in self.children:
if type(child) == discord.ui.Button and child.label == "shiny":
shiny_button = child
child.disabled = True
break
# update the view in discord
await interaction.message.edit(content=message.content, view=self)
# do the rest of what the `stats` button should do
# actually send followup message to user
await interaction.followup.send("stats")
# if we wanted to re-enable the other button we can do it here
# commented out
# shiny_button.disabled = False
# await interaction.message.edit(content=message.content, view=self)
@discord.ui.button(label="back")
async def back_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论