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

如何让discord.py机器人响应消息的所有新增用户反应?

实现Discord机器人跟随用户添加反应的功能

要实现用户给消息加反应后机器人跟着加相同反应的效果,你需要使用discord.py的on_reaction_add事件监听反应添加行为,具体实现如下:

完整代码示例

import discord
from discord.ext import commands

# 启用必要的意图,需在Discord开发者后台开启对应权限
intents = discord.Intents.default()
intents.reactions = True
intents.message_content = True  # 保留原有on_message逻辑时需开启此意图

bot = commands.Bot(command_prefix="!", intents=intents)

# 保留你原有的消息监听逻辑(可选)
@bot.event
async def on_message(message):
    await bot.process_commands(message)
    if "hi" in message.content.lower():
        await message.add_reaction("👋")

# 新增反应监听逻辑
@bot.event
async def on_reaction_add(reaction, user):
    # 排除机器人自身的反应,防止无限循环
    if user.bot:
        return
    # 给目标消息添加相同的反应
    await reaction.message.add_reaction(reaction.emoji)

# 替换为你的机器人Token
bot.run("YOUR_BOT_TOKEN")

关键说明

  • 意图配置:必须开启reactions意图,否则机器人无法监听反应事件;如果需要保留原有的on_message逻辑,还要开启message_content意图,同时在Discord开发者后台的机器人设置中启用这些权限。
  • 排除机器人自身:判断user.bot为True时直接返回,避免机器人自己添加反应后再次触发事件,导致无限循环。
  • 权限要求:确保机器人在对应服务器中有添加反应的权限,否则会抛出权限错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 00:12:39