You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Nextcord中点击按钮后禁用RPS游戏的View组件

解决方案

要实现点击按钮后禁用整个View组件,你需要在按钮回调中修改View内所有按钮的disabled状态,然后更新原消息的View。以下是修改后的完整代码:

import nextcord
import random

client = nextcord.Client(intents=nextcord.Intents.all())

def checkwin(useroption):
    # 简化随机选择逻辑
    options = ["Rock", "Paper", "Scissors"]
    option = random.choice(options)
    
    if option == useroption:
        return "Tie"
    # 机器人获胜的情况
    elif (option == "Rock" and useroption == "Scissors") or \
         (option == "Scissors" and useroption == "Paper") or \
         (option == "Paper" and useroption == "Rock"):
        return "Win"
    else:
        return "Loss"

class RPSBTNS(nextcord.ui.View):
    @nextcord.ui.button(label="Rock", style=nextcord.ButtonStyle.red, emoji="🪨")
    async def rock(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        victory = checkwin("Rock")
        # 确定回复内容
        if victory == "Win":
            msg = "You lose."
        elif victory == "Loss":
            msg = "You Win!"
        else:
            msg = "Tie."
        
        # 禁用View内所有按钮
        for btn in self.children:
            if isinstance(btn, nextcord.ui.Button):
                btn.disabled = True
        
        # 发送结果(ephemeral=True 让消息仅用户可见)
        await interaction.response.send_message(msg, ephemeral=True)
        # 更新原消息的View,应用禁用状态
        await interaction.edit_original_message(view=self)

    @nextcord.ui.button(label="Paper", style=nextcord.ButtonStyle.green, emoji="📃")
    async def paper(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        victory = checkwin("Paper")
        if victory == "Win":
            msg = "You lose."
        elif victory == "Loss":
            msg = "You Win!"
        else:
            msg = "Tie."
        
        for btn in self.children:
            if isinstance(btn, nextcord.ui.Button):
                btn.disabled = True
        
        await interaction.response.send_message(msg, ephemeral=True)
        await interaction.edit_original_message(view=self)

    @nextcord.ui.button(label="Scissors", style=nextcord.ButtonStyle.blurple, emoji="✂️")
    async def scissors(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        victory = checkwin("Scissors")
        if victory == "Win":
            msg = "You lose."
        elif victory == "Loss":
            msg = "You Win!"
        else:
            msg = "Tie."
        
        for btn in self.children:
            if isinstance(btn, nextcord.ui.Button):
                btn.disabled = True
        
        await interaction.response.send_message(msg, ephemeral=True)
        await interaction.edit_original_message(view=self)

@client.slash_command(name="test")
async def test(interaction: nextcord.Interaction):
    await interaction.send("Rock Paper Scissors and shoot!", view=RPSBTNS())

client.run("your_token_here")

关键修改点

  • 禁用按钮:遍历View的children属性,将所有按钮的disabled设为True,确保用户无法再次点击任何按钮。
  • 更新原消息:使用interaction.edit_original_message(view=self)将修改后的View(已禁用按钮)同步到原消息,用户会看到按钮变灰。
  • 回复结果:用interaction.response.send_message发送胜负结果,添加ephemeral=True可以让结果仅触发按钮的用户可见,避免频道消息混乱。
  • 简化逻辑:优化了checkwin函数的随机选择和胜负判断,让代码更简洁易维护。

内容的提问来源于stack exchange,提问作者Anthony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 20:15:39