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

Discord.Net中SocketClient.GetGuild(id)返回null问题求助

问题原因

你遇到的client.Guilds为空、GetGuild返回null的核心原因是:调用await client.StartAsync()后,机器人并未立即完成与Discord网关的同步,公会数据还没加载完毕。此时直接尝试获取公会自然拿不到结果,哪怕机器人已经加入了服务器。

解决方案

1. 等待机器人Ready事件完成初始化

Discord.Net的DiscordSocketClient会在完成所有初始化(包括加载公会数据)后触发Ready事件,你需要在这个事件回调里处理公会、频道的获取以及定时任务的启动。

2. 修正定时任务的实现

你的CheckForChange方法如果是要每5分钟执行一次,不能只调用一次,需要用循环+延迟来实现定时逻辑,或者使用Timer类。

修改后的代码示例

public async Task MainAsync()
{
    using var services = ConfigureServices();

    Console.WriteLine("Ready for takeoff...");
    var client = services.GetRequiredService<DiscordSocketClient>();

    client.Log += Log;
    services.GetRequiredService<CommandService>().Log += Log;

    // 从Config.json获取机器人令牌
    JObject config = Functions.GetConfig();
    string token = config["token"].Value<string>();
    ulong serverId = ulong.Parse(config["serverid"].Value<string>());
    ulong channelId = ulong.Parse(config["channelid"].Value<string>());

    // 订阅Ready事件,等待初始化完成
    client.Ready += async () =>
    {
        Console.WriteLine("机器人已完成初始化");

        // 获取目标公会和频道
        var guild = client.GetGuild(serverId);
        if (guild == null)
        {
            Console.WriteLine("未找到目标服务器,请确认机器人已加入该服务器且ID正确");
            return;
        }

        var channel = guild.GetTextChannel(channelId);
        if (channel == null)
        {
            Console.WriteLine("未找到目标频道,请确认频道ID正确且机器人有权限访问");
            return;
        }

        // 启动定时任务:每5分钟执行一次CheckForChange
        while (true)
        {
            await CheckForChange(channel);
            await Task.Delay(TimeSpan.FromMinutes(5));
        }
    };

    // 登录Discord并启动机器人
    await client.LoginAsync(TokenType.Bot, token);
    await client.StartAsync();

    await services.GetRequiredService<CommandHandlingService>().InitializeAsync();

    // 保持机器人运行
    await Task.Delay(-1);
}

额外注意事项

  • 不需要将ID转为小写,Discord的ID是纯数字字符串,直接用ulong.Parse转换即可,避免不必要的错误。
  • 确保机器人在目标服务器中有查看频道和发送消息的权限,否则即使获取到频道,发送消息也会失败。
  • 如果需要更可靠的定时任务,可以考虑使用Microsoft.Extensions.Hosting的后台服务,而不是简单的循环延迟,适合长期运行的机器人。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 11:30:56