【问题标题】:Discord bot running, but will not connect to serverDiscord bot 正在运行,但无法连接到服务器
【发布时间】:2018-01-24 17:15:51
【问题描述】:

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

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

虽然它运行没有错误,但机器人本身并没有出现在我的服务器上。

在你问之前,我实际上已经用实际的 bot 令牌替换了“Bot token here”。

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);
    }
  }
}

然后是 MyBot.cs 类

namespace DiscordBot
{
  class MyBot : ModuleBase
  {

    private CommandService _service;

    public MyBot(CommandService service)
    {
        _service = service;
    }
  }
}

【问题讨论】:

    标签: c# discord discord.net


    【解决方案1】:

    您可能想要做的第一件事是向您的机器人添加一些日志记录。 因为您的代码可能是正确的,但不和谐可能会出于任何原因拒绝您的连接。

    await client.StartAsync();之后添加

    client.Log += (msg) => {return Console.WriteLine(${msg.ToString()}");};`
    

    这会将您从客户端收到的消息输出到控制台。

    现在您还需要配置应该向该事件发送的消息。这可以在创建 DiscordClient() 时完成。所以你可以使用

    而不是 client = new DiscordSocketClient();
    client = new DiscordSocketClient(
        new DiscordSocketConfig()
        {
             LogLevel = LogSeverity.Verbose
        }
     );
    

    详细信息应为您提供所需的所有信息。但是,您也可以改用 LogSeverity.Debug,这是可用的最高日志记录,因此会为您提供所有消息。

    现在您已经在控制台中获得了一些反馈,请去看看您遇到了哪些具体错误。

    另外我建议先完成链接教程的your first bot 部分,而不是直接进入命令。一旦你得到这个工作,你可以继续前进

    【讨论】:

    • 感谢您的输入,原来是在幕后发生了一些事情。 | | System.PlatformNotSupportedException: The Websocket protocol is not supported on this platform 经过一番研究,发现它与Windows 7不兼容?你知道有什么办法可以解决这个问题吗?
    • @john 是的,有。但这将是一个很长的评论,值得提出一个新问题
    • 好的,谢谢你告诉我。然后我会发布一个新问题:)
    猜你喜欢
    • 2012-11-03
    • 2015-08-15
    • 2018-08-16
    • 1970-01-01
    • 2014-10-16
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多