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

Discord跨语言Bot互响应问题:C# Bot无法获取Bot消息内容

问题排查与解决方案

核心问题分析

从你的描述和代码来看,Bot2能检测到Bot1的消息但无法触发命令,核心原因大概率是消息内容无法正确获取,导致前缀判断失败,进而无法执行命令。以下是具体排查点和修复方案:

1. 必须在Bot2客户端配置中启用MessageContent Intent

你已经在Discord开发者平台开启了MessageContent Intent,但Discord.Net(C#库)需要在客户端初始化时明确声明要使用的Intent,否则即使平台开了权限,客户端也不会请求该Intent,导致message.Content和message.CleanContent为空。

修复方式(Discord.Addons.Hosting场景)

如果使用appsettings.json配置:

{
  "Discord": {
    "Token": "your-bot2-token",
    "GatewayIntents": "Guilds, GuildMessages, MessageContent"
  }
}

如果在代码中配置(Program.cs):

builder.Services.AddDiscordHost((config, socketConfig) =>
{
    socketConfig.Token = config["Discord:Token"];
    socketConfig.LogLevel = LogSeverity.Info;
    // 明确指定需要的Intent
    socketConfig.GatewayIntents = GatewayIntents.Guilds 
                                | GatewayIntents.GuildMessages 
                                | GatewayIntents.MessageContent;
});

2. 确认Ping命令模块正确编写并注册

你的CommandHandler代码中没有包含具体的Ping命令模块,必须编写对应的命令模块才能触发回复:

using Discord.Commands;

namespace CommunicationServerBot.Modules
{
    public class PingModule : ModuleBase<SocketCommandContext>
    {
        [Command("ping")]
        public async Task PingAsync()
        {
            await ReplyAsync("pong");
        }
    }
}

确保该模块所在的程序集是Assembly.GetEntryAssembly()(即项目主程序集),这样AddModulesAsync才能正确加载它。

3. 验证前缀判断逻辑

Bot1发送的@bot2 ping会触发HasMentionPrefix判断,但如果message.Content为空(因Intent未配置),该判断会返回false。修复Intent配置后,可以在HandleMessage中添加日志确认message.Content是否正常:

if (message.Source == MessageSource.Bot)
{
    Console.WriteLine($"Bot消息内容:{message.Content}");
    Console.WriteLine($"Bot消息CleanContent:{message.CleanContent}");
};

如果能正确打印出@bot2 ping,说明前缀判断逻辑可以正常触发。

4. 其他注意事项

  • 确保Bot2的Token正确,且已经邀请到服务器并赋予了足够权限(你已经设置管理员权限,这部分没问题)。
  • 检查Discord开发者平台中Bot2的"Privileged Gateway Intents"是否全部开启(Guild Members、Message Content等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 04:52:42