Discord bot正在运行,但无法连接到服务器 [英] Discord bot running, but will not connect to server

查看:1096
本文介绍了Discord bot正在运行,但无法连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将我的代码从0.9.6 Discord.NET API交换到新的1.0.1 API,这基本上是在要求对我的代码进行完全的重组。但是我实际上在启动和运行该机器人方面遇到了一些麻烦。

I've been trying to swap over my code from the 0.9.6 Discord.NET API to the new 1.0.1 API, and it's basically calling for a complete restructure to my code. But I've been having some trouble actually getting the bot up and running first of all.

我根据链接的此处

运行无误,该机器人本身并未在线出现在我的服务器中。

And while it runs without error, the bot itself is not appearing online in my server.

在您问之前,我实际上已经用实际的机器人令牌替换了此处的令牌。

And before you ask, I had in fact replaced "Bot token here" with the actual bot token.

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类

And then for the MyBot.cs class

namespace DiscordBot
{
  class MyBot : ModuleBase
  {

    private CommandService _service;

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


推荐答案

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

The first thing you might want to do is add some logging to your bot. As your code might be correct, but discord could be rejecting your connection for any amount of reason.

等待客户端之后。StartAsync (); 添加

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

现在,您还需要配置应向该事件发送的消息。这可以在创建您的事件时完成。 DiscordClient()。因此,代替 client = new DiscordSocketClient(); 您可以使用

Now you also need to configure which message should be send to this event. This can be done when creating your DiscordClient(). So instead of client = new DiscordSocketClient(); You could use

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

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

Verbose should give you all the information you need. However you could also use LogSeverity.Debug instead, which is the highest available logging, and therefore would give you all messages.

现在,您

另外,我建议您先完成链接教程的第一个bot 部分,而不是直接进入命令。完成这项工作后,您就可以继续

Also I would recommend first completing the your first bot part of the linked tutorial, instead of stepping into the commands directly. Once you got this working, you can continue onwards

这篇关于Discord bot正在运行,但无法连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆