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

discord.py中on_reaction_add事件报错:TypeError参数数量不符

问题解决:TypeError: on_reaction_add() takes 1 positional argument but 2 were given

错误原因

discord.py 中on_reaction_add事件的回调函数必须接收两个参数:

  • reaction: 触发事件的反应对象(类型为discord.Reaction)
  • user: 添加反应的用户对象(类型为discord.User/discord.Member)

你只定义了一个参数,导致Python解释器抛出参数不匹配的错误。

修正后的代码

@client.event
async def on_reaction_add(reaction: discord.Reaction, user: discord.User):
    # 排除机器人自身的反应
    if user == client.user:
        return
    
    # 判断是否是目标表情🟥
    if str(reaction.emoji) == '🟥':
        # 发送test消息到反应所在的频道
        await reaction.message.channel.send('test')

额外说明

  1. 原代码逻辑顺序有误,应先排除机器人自身的反应,避免不必要的判断
  2. 直接比较reaction.emoji和字符串可能存在问题,建议用str(reaction.emoji)统一转为字符串后再对比
  3. 类型注解要对应正确的discord对象类型,discord.user是模块,正确类型是discord.User或discord.Reaction

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 02:22:01