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

如何用Discord.py实现用户反应消息后转发至指定频道

实现Discord机器人的⭐消息汇总功能(Python版)

需求背景

我几乎完全不懂Python编程,跟着网上教程做了个私人服务器用的Discord机器人,想要实现:用户给消息加⭐反应时,把这条消息收集到指定频道做高亮汇总。之前在Stack Overflow找到过JS版方案,现在改成Python实现。

迭代记录

2023-01-19 08:53 GMT+8(初始尝试代码)

@client.event
async def on_reaction_add(reaction, member):
        if str(reaction.emoji) == "⭐":
            channel = await bot.get_channel(channel_id)
            await channel.send(reaction.message.content)

这段代码存在几个问题:混用了client和bot实例,get_channel不需要加await,而且仅发送消息文本,信息不够完整。

2023-01-20 17:14 GMT+8(最终可用代码)

@bot.event
async def on_reaction_add(reaction, user):
    if reaction.emoji == "⭐":
        astrometrics = bot.get_channel(这里填频道ID)
        embed = discord.Embed(color = 0xFEE75C)
        embed.set_author(name = reaction.message.author.name, icon_url = reaction.message.author.display_avatar)
        embed.add_field(name = "消息内容", value = reaction.message.content)
        if len(reaction.message.attachments) > 0:
            embed.set_image(url = reaction.message.attachments[0].url)
        embed.add_field(name = "跳转至原消息", value = reaction.message.jump_url, inline = False)
        await astrometrics.send(embed = embed)

关键改进点

  • 统一使用bot实例,避免实例混用引发的错误
  • 采用Discord Embed格式推送汇总消息,排版更清晰美观
  • 新增原消息作者的名称和头像展示,保留消息归属信息
  • 支持自动提取消息中的第一张图片附件
  • 添加原消息跳转链接,方便快速查看上下文
  • 移除了get_channel方法上不必要的await调用(该方法为同步方法)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 11:40:51