Discord.py 按钮交互错误解析与上下文数据传递指南

聖光之護
发布: 2025-12-03 13:41:02
原创
857人浏览过

Discord.py 按钮交互错误解析与上下文数据传递指南

本文深入探讨 discord.py 中按钮交互时常见的“interaction error”问题,主要源于按钮回调函数签名不正确。教程将详细解释正确的按钮回调机制,通过代码示例演示如何修正错误,并提供在按钮交互中安全、高效地传递上下文数据(如原始命令的调用者或目标用户)的最佳实践,确保您的机器人能够稳定处理用户交互。

理解 Discord.py 按钮回调机制

Discord.py 2.0 及更高版本引入的 UI 组件(如按钮、下拉菜单)极大地增强了机器人与用户的交互能力。当用户点击一个按钮时,Discord 会向您的机器人发送一个交互事件,触发与该按钮关联的异步回调函数。然而,如果这个回调函数的签名不符合预期,就会导致 Discord 客户端显示“interaction error”。

对于 discord.ui.Button 的回调函数,其标准签名应为 async def callback_name(self, interaction: discord.Interaction, button: discord.ui.Button)。这意味着回调函数预期接收三个参数:

  1. self: View 类的实例本身。
  2. interaction: 一个 discord.Interaction 对象,包含了关于此次用户交互的所有信息,例如点击按钮的用户、消息 ID 等。
  3. button: 被点击的 discord.ui.Button 对象。

任何额外的位置参数,例如直接在回调函数签名中添加 user: discord.Member,都会导致 Discord.py 无法正确解析传入的交互事件,从而引发“interaction error”。

错误示例分析

考虑以下一个尝试实现“求婚系统”的示例代码片段,其中按钮回调函数被错误地定义:

import discord
from discord.ext import commands

# ... (client setup omitted for brevity) ...

class MarryButtons(discord.ui.View):
    def __init__(self):
        super().__init__()

    @discord.ui.button(label="Yes", style=discord.ButtonStyle.success)
    async def agree_btn(self, interaction: discord.Interaction, button: discord.ui.Button, user: discord.Member):
        # 错误:user: discord.Member 是多余的参数
        # ...
        pass

    @discord.ui.button(label="No", style=discord.ButtonStyle.danger)
    async def disagree_btn(self, interaction: discord.Interaction, button: discord.ui.Button, user: discord.Member):
        # 错误:user: discord.Member 是多余的参数
        # ...
        pass

# ... (marry command omitted for brevity) ...
登录后复制

在上述代码中,agree_btn 和 disagree_btn 回调函数中都额外添加了 user: discord.Member 参数。当用户点击这些按钮时,Discord.py 尝试调用这些函数,但由于签名不匹配,无法将 discord.Member 对象传递给 user 参数,从而触发了“interaction error”。

Dreamina
Dreamina

字节跳动推出的AI绘画工具,用简单的文案创作精美的图片

Dreamina 436
查看详情 Dreamina

正确实现按钮回调

要解决这个问题,只需移除回调函数签名中多余的参数,使其符合标准格式:

import discord
from discord.ext import commands

# ... (client setup omitted for brevity) ...

class MarryButtons(discord.ui.View):
    def __init__(self):
        super().__init__()

    @discord.ui.button(label="Yes", style=discord.ButtonStyle.success)
    async def agree_btn(self, interaction: discord.Interaction, button: discord.ui.Button):
        # 现在签名是正确的
        # ... 可以在这里处理逻辑 ...
        pass

    @discord.ui.button(label="No", style=discord.ButtonStyle.danger)
    async def disagree_btn(self, interaction: discord.Interaction, button: discord.ui.Button):
        # 现在签名是正确的
        # ... 可以在这里处理逻辑 ...
        pass

# ...
登录后复制

在按钮回调中传递上下文数据

虽然移除了多余的参数解决了交互错误,但我们通常需要在按钮回调中访问创建按钮时的一些上下文信息,例如发起求婚的用户和被求婚的用户。直接将这些信息作为回调参数是不允许的,正确的做法是将这些数据作为 discord.ui.View 实例的属性进行存储。

以下是实现上下文数据传递的步骤:

  1. 在 View 的 __init__ 方法中接收并存储数据: 当创建 MarryButtons 实例时,将发起者和目标用户的 discord.Member 对象传递给其构造函数,并存储为实例属性。
  2. 在按钮回调中访问存储的数据: 在 agree_btn 或 disagree_btn 方法中,通过 self.proposer 和 self.target_user 访问这些数据。

完整示例代码

import discord
from discord.ext import commands
import os

# 1. 初始化 Bot 客户端
intents = discord.Intents.default()
intents.message_content = True  # 允许读取消息内容,尽管这里主要使用斜杠命令
intents.members = True          # 允许获取成员信息,用于处理用户对象

client = commands.Bot(command_prefix='!', intents=intents)

# 2. Bot 启动事件
@client.event
async def on_ready():
    print(f'Bot 已登录为 {client.user} (ID: {client.user.id})')
    try:
        # 同步斜杠命令到 Discord
        synced = await client.tree.sync()
        print(f"已同步 {len(synced)} 条斜杠命令")
    except Exception as e:
        print(f"同步命令失败: {e}")

# 3. 定义按钮视图类
class MarryButtons(discord.ui.View):
    def __init__(self, proposer: discord.Member, target_user: discord.Member):
        super().__init__(timeout=180)  # 设置视图超时时间为 3 分钟
        self.proposer = proposer
        self.target_user = target_user
        self.message = None # 用于在超时时编辑原消息

    async def on_timeout(self):
        # 当视图超时时执行
        if self.message:
            embed_timeout = discord.Embed(
                title="求婚超时",
                description=f"{self.proposer.mention} 对 {self.target_user.mention} 的求婚已超时,未得到回应。",
                color=discord.Color.orange()
            )
            await self.message.edit(embed=embed_timeout, view=None)
        else:
            # 如果某种原因消息不可用,可以尝试发送一条新消息
            print("警告: 无法在超时时编辑消息,消息对象未设置。")

    @discord.ui.button(label="接受", 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

        embed_agree = discord.Embed(
            title=f'求婚成功!',
            description=f'{self.target_user.mention} 接受了 {self.proposer.mention} 的求婚!恭喜!',
            color=discord.Color.green()
        )
        # 编辑原消息,移除按钮并显示结果
        await interaction.response.edit_message(embed=embed_agree, view=None)
        self.stop() # 停止监听此视图的进一步交互

    @discord.ui.button(label="拒绝", 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

        embed_disagree = discord.Embed(
            title=f'求婚被拒绝',
            description=f'{self.target_user.mention} 拒绝了 {self.proposer.mention} 的求婚。',
            color=discord.Color.red()
        )
        await interaction.response.edit_message(embed=embed_disagree, view=None)
        self.stop()

    @discord.ui.button(label="取消", style=discord.ButtonStyle.gray, emoji="❌")
    async def cancel_btn(self, interaction: discord.Interaction, button: discord.ui.Button):
        # 只有求婚者或被求婚者可以取消
        if interaction.user not in [self.proposer, self.target_user]:
            await interaction.response.send_message("你不能取消这个求婚!", ephemeral=True)
            return

        embed_cancel = discord.Embed(
            title=f'求婚已取消',
            description=f'{interaction.user.mention} 取消了 {self.proposer.mention} 对 {self.target_user.mention} 的求婚。',
            color=discord.Color.light_gray()
        )
        await interaction.response.edit_message(embed=embed_cancel, view=None)
        self.stop()

# 4. 定义斜杠命令
@client.tree.command(name='marry', description="向某人求婚")
async def marry(interaction: discord.Interaction, user: discord.Member):
    # 检查不能和自己求婚
    if interaction.user == user:
        await interaction.response.send_message(content=f"{interaction.user.mention} 你不能和自己结婚 :(", ephemeral=True)
        return

    # 检查不能和机器人求婚
    if user.bot:
        await interaction.response.send_message(content=f"{interaction.user.mention} 你不能和机器人结婚!", ephemeral=True)
        return

    embed_marry = discord.Embed(
        title='? 求婚啦!',
        description=f'{interaction.user.mention} 向 {user.mention} 提出了求婚!',
        color=0x774dea
    )

    # 创建 MarryButtons 实例,并传入求婚者和被求婚者
    view = MarryButtons(proposer=interaction.user, target_user=user)

    # 发送消息并附带按钮视图
    # 注意:这里发送的消息不设置为 ephemeral=True,以便在超时时可以编辑
    await interaction.response.send_message(embed=embed_marry, view=view)

    # 获取原始响应消息对象,并将其存储在视图中,以便 on_timeout 可以编辑它
    # interaction.original_response() 返回一个 discord.Message 对象
    view.message = await interaction.original_response()


# 5. 运行 Bot
# 请将 'YOUR_BOT_TOKEN' 替换为你的实际 Bot Token
# client.run('YOUR_BOT_TOKEN')
登录后复制

注意事项与最佳实践

  • 回调函数签名严格性:始终牢记 discord.ui.Button 回调函数只接受 self, interaction, button 三个参数。任何额外参数都会导致错误。
  • 上下文数据存储:将命令相关的上下文数据(如用户 ID、消息内容等)存储为 discord.ui.View 实例的属性是最佳实践。这样,所有按钮回调都可以轻松访问这些数据。
  • 交互权限检查:在按钮回调中,务必检查 interaction.user 是否是预期的操作者。例如,在“求婚系统”中,只有被求婚者才能点击“接受”或“拒绝”按钮。对于不符合条件的用户,应发送临时的

以上就是Discord.py 按钮交互错误解析与上下文数据传递指南的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号