C# Discord bot正在运行,但不会连接到服务器

C# Discord bot正在运行,但不会连接到服务器,c#,discord,discord.net,C#,Discord,Discord.net,我一直在尝试将我的代码从0.9.6 Discord.NET API转换为新的1.0.1 API,它基本上要求对我的代码进行完整的重构。但首先,我在启动和运行机器人时遇到了一些麻烦 我根据链接的指南设置代码体 虽然运行时没有错误,但bot本身并没有出现在我的服务器上 在你问之前,我实际上已经用实际的机器人令牌替换了“这里的机器人令牌” namespace DiscordBot{ public class Program { private CommandService comman

我一直在尝试将我的代码从0.9.6 Discord.NET API转换为新的1.0.1 API,它基本上要求对我的代码进行完整的重构。但首先,我在启动和运行机器人时遇到了一些麻烦

我根据链接的指南设置代码体

虽然运行时没有错误,但bot本身并没有出现在我的服务器上

在你问之前,我实际上已经用实际的机器人令牌替换了“这里的机器人令牌”

namespace DiscordBot{
  public class Program
  {
    private CommandService commands;
    private DiscordSocketClient client;
    private IServiceProvider services;

    static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult();

    public async Task Start()
    {
        client = new DiscordSocketClient();
        commands = new CommandService();

        string token = "<token>";

        services = new ServiceCollection()
                .BuildServiceProvider();

        await InstallCommands();

        await client.LoginAsync(TokenType.Bot, token);
        await client.StartAsync();

        await Task.Delay(-1);
    }

    public async Task InstallCommands()
    {
        // Hook the MessageReceived Event into our Command Handler
        client.MessageReceived += HandleCommand;
        // Discover all of the commands in this assembly and load them.
        await commands.AddModulesAsync(Assembly.GetEntryAssembly());
    }

    public async Task HandleCommand(SocketMessage messageParam)
    {
        // Don't process the command if it was a System Message
        var message = messageParam as SocketUserMessage;
        if (message == null) return;
        // Create a number to track where the prefix ends and the command begins
        int argPos = 0;
        // Determine if the message is a command, based on if it starts with '!' or a mention prefix
        if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
        // Create a Command Context
        var context = new CommandContext(client, message);
        // Execute the command. (result does not indicate a return value, 
        // rather an object stating if the command executed successfully)
        var result = await commands.ExecuteAsync(context, argPos, services);
        if (!result.IsSuccess)
            await context.Channel.SendMessageAsync(result.ErrorReason);
    }
  }
}

您可能要做的第一件事是向bot添加一些日志记录。 因为您的代码可能是正确的,但discord可能会出于各种原因拒绝您的连接

之后等待client.StartAsync()添加

client.Log += (msg) => {return Console.WriteLine(${msg.ToString()}");};`
这将把您从客户端收到的消息输出到控制台

现在,您还需要配置应该发送给此事件的消息。这可以在创建
DiscordClient()
时完成。因此,不是
client=newdiscordsocketclient()您可以使用

client = new DiscordSocketClient(
    new DiscordSocketConfig()
    {
         LogLevel = LogSeverity.Verbose
    }
 );
详细信息应该能提供您所需要的所有信息。但是,您也可以改用
LogSeverity.Debug
,这是可用的最高日志记录,因此将向您提供所有消息

现在您的控制台中有了一些反馈,请查看您有哪些具体错误


我还建议首先完成链接教程的部分,而不是直接进入命令。一旦这项工作正常,您就可以继续了

您可能要做的第一件事就是向您的机器人添加一些日志记录。 因为您的代码可能是正确的,但discord可能会出于各种原因拒绝您的连接

之后等待client.StartAsync()添加

client.Log += (msg) => {return Console.WriteLine(${msg.ToString()}");};`
这将把您从客户端收到的消息输出到控制台

现在,您还需要配置应该发送给此事件的消息。这可以在创建
DiscordClient()
时完成。因此,不是
client=newdiscordsocketclient()您可以使用

client = new DiscordSocketClient(
    new DiscordSocketConfig()
    {
         LogLevel = LogSeverity.Verbose
    }
 );
详细信息应该能提供您所需要的所有信息。但是,您也可以改用
LogSeverity.Debug
,这是可用的最高日志记录,因此将向您提供所有消息

现在您的控制台中有了一些反馈,请查看您有哪些具体错误


我还建议首先完成链接教程的部分,而不是直接进入命令。一旦你成功了,你就可以继续前进了

谢谢你的投入,结果发现引擎盖下发生了什么事System.PlatformNotSupportedException:此平台不支持Websocket协议经过一些研究,发现它与Windows 7不兼容?你知道有什么办法可以解决这个问题吗?@john yep,有。但这将是一个很长的评论,并将值得一个新的问题好的谢谢你让我知道。然后我会发布一个新问题:)谢谢你的输入,结果发现引擎盖下发生了什么事System.PlatformNotSupportedException:此平台不支持Websocket协议经过一些研究,发现它与Windows 7不兼容?你知道有什么办法可以解决这个问题吗?@john yep,有。但这将是一个很长的评论,并将值得一个新的问题好的谢谢你让我知道。然后我会发布一个新问题:)