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

Discord机器人发送Message对象信息而非消息内容问题求助

问题解决:Discord机器人转发消息到指定频道

问题根源

你直接拼接了Message对象本身,Python会输出该对象的调试信息而非用户发送的文本内容,应该使用message.content属性获取消息正文。另外原代码是把消息发回当前频道,需要调整为发送到另一服务器的chat-chan频道。

修改后的代码

async def on_message(message):
    # 跳过机器人自身消息,避免循环转发
    if message.author.bot:
        return
    # 仅处理来源频道为chat-chan的用户消息
    if message.channel.name == 'chat-chan':
        # 替换为目标服务器的ID
        target_guild = message.client.get_guild(123456789)
        if not target_guild:
            print("未找到目标服务器")
            return
        # 在目标服务器中查找chat-chan频道
        target_channel = discord.utils.get(target_guild.channels, name='chat-chan')
        if not target_channel:
            print("目标服务器中不存在chat-chan频道")
            return
        # 发送格式化后的消息内容
        await target_channel.send(f"{message.author.name}: {message.content}")

关键说明

  • message.content:获取用户发送的纯文本消息内容,替代原代码中直接使用的message对象。
  • message.client.get_guild(目标服务器ID):通过服务器数字ID定位到要转发的目标服务器,需替换为你实际的目标服务器ID。
  • discord.utils.get(target_guild.channels, name='chat-chan'):在目标服务器中精准匹配名为chat-chan的频道。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 15:57:20