如何在Discord.py中点击按钮后修改颜色为灰色并禁用按钮
解决Discord按钮点击后禁用并变灰的问题
你不需要用edit_message,直接修改按钮实例的属性就能实现效果,步骤如下:
- 将按钮的
disabled属性设为True,禁用按钮 - 将按钮的
style改为discord.ButtonStyle.grey(灰色样式) - 调用
interaction.message.edit()方法更新消息组件,让修改生效
修改后的代码示例:
@discord.ui.button(label="Daily Game", style=discord.ButtonStyle.blurple) async def daily_fantasy(self, interaction: discord.Interaction, button: discord.ui.Button): # 发送提示消息 await interaction.response.send_message(content=f"Let's get started!", ephemeral=True) # 修改按钮属性 button.disabled = True button.style = discord.ButtonStyle.grey # 更新消息组件,使修改生效 await interaction.message.edit(view=self)
注:如果是在discord.ui.View类中使用该按钮,self就是当前的View实例,直接传入edit()方法即可。
内容的提问来源于stack exchange,提问作者Ryan Thomas
相关产品推荐
相关产品推荐

