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

discord.py 2.0结婚系统按钮交互错误排查求助

Discord.py 2.0结婚系统按钮交互错误排查与修复

错误原因分析

  • 按钮回调参数不合法:discord.py中discord.ui.button装饰的回调函数仅支持interaction: discord.Interaction和button: discord.ui.Button两个参数,你额外添加的user: discord.Member会导致框架无法匹配参数,触发交互错误。
  • 缺少上下文传递:MarryButtons类无法获取/marry指令中指定的被求婚用户,导致按钮回调里无法正确引用目标用户。

修复后的完整代码

@client.tree.command(name='marry', description="Suggest to marry")
async def marry(interaction: discord.Interaction, user: discord.Member):
    print(f'{interaction.user}||{user}')
    if interaction.user == user:
        await interaction.response.send_message(content=f"{interaction.user.mention} you can`t marry yourself :(")
        return
    
    embed_marry = discord.Embed(title='WOW.....', description=f'{interaction.user.mention} suggests marriage to {user.mention}', color=0x774dea)
    # 将被求婚用户传递给View实例
    await interaction.response.send_message(embed=embed_marry, view=MarryButtons(target_user=user))


class MarryButtons(discord.ui.View):
    def __init__(self, target_user: discord.Member):
        super().__init__(timeout=300)  # 设置5分钟超时,超时后按钮失效
        self.target_user = target_user  # 保存被求婚用户上下文

    @discord.ui.button(label="Yes", style=discord.ButtonStyle.success)
    async def agree_btn(self, interaction: discord.Interaction, button: discord.ui.Button):
        # 验证点击者为被求婚用户
        if interaction.user != self.target_user:
            await interaction.response.send_message("只有被求婚的人才能回应!", ephemeral=True)
            return
        
        proposer = interaction.message.embeds[0].description.split(" ")[0]
        embed_agree = discord.Embed(title=f'{self.target_user.mention} answered YES', description=f'{self.target_user.mention} is now married to {proposer}', color=0x00ff00)
        await interaction.response.send_message(embed=embed_agree)
        
        # 点击后禁用所有按钮,防止重复操作
        for item in self.children:
            item.disabled = True
        await interaction.message.edit(view=self)

    @discord.ui.button(label="No", style=discord.ButtonStyle.danger)
    async def disagree_btn(self, interaction: discord.Interaction, button: discord.ui.Button):
        if interaction.user != self.target_user:
            await interaction.response.send_message("只有被求婚的人才能回应!", ephemeral=True)
            return
        
        proposer = interaction.message.embeds[0].description.split(" ")[0]
        embed_disagree = discord.Embed(title=f'{self.target_user.mention} answered NO', description=f'{self.target_user.mention} declined the proposal from {proposer}', color=0xff0000)
        await interaction.response.send_message(embed=embed_disagree)
        
        for item in self.children:
            item.disabled = True
        await interaction.message.edit(view=self)

    @discord.ui.button(label="😜", style=discord.ButtonStyle.gray)
    async def emoji_btn(self, interaction: discord.Interaction, button: discord.ui.Button):
        embed_emoji = discord.Embed(title='Proposal canceled', description='Nothing changed', color=0x808080)
        await interaction.response.send_message(embed=embed_emoji)
        
        for item in self.children:
            item.disabled = True
        await interaction.message.edit(view=self)

额外优化说明

  • 增加超时机制:View设置5分钟超时,避免长期存在无效交互按钮。
  • 增加身份验证:限制只有被求婚用户能回应Yes/No按钮,防止无关操作。
  • 按钮点击后全局禁用:避免重复点击触发多次交互。
  • 修正了描述文本的语法错误,提升可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 21:06:28