第二次使用任何异步 api 调用后,Pi 上的机器人崩溃

Bot on Pi crashes after using any Async api calls a second time

我将我的 Discord 机器人放在 Rasberry Pi 上,但现在我遇到了问题。当我尝试使用命令时,它第一次起作用。但是第二次使用它时,它没有工作,只是说 "A MessageReceived handler is blocking the gateway task."。不久之后它又说

System.Exception: Server missed last heartbeat at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in :0 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in :0 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in :0 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in :0 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in :0 at Discord.ConnectionManager+<>c__DisplayClass28_0+<b__0>d.MoveNext () [0x0014b] in :0

然后断开连接,尝试重新连接一些但每次都出错。我没有使用命令界面,但我使用的是 async/await。它在我的普通电脑上运行得很好,只在我的 Pi 上坏了。

using Discord;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GlurrrBotDiscord
{
    public class Program
    {
        DiscordSocketClient client;

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

        public async Task MainAsync()
        {
            client = new DiscordSocketClient();

            try
            {
                using(StreamReader sr = new StreamReader("botcode.txt"))
                {
                    string code = sr.ReadLine();
                    await client.LoginAsync(TokenType.Bot, code);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("Code not found");
                Console.WriteLine(e.Message);
            }

            await client.StartAsync();
            await client.SetGameAsync("praise");

            client.MessageReceived += handleMessage;

            await Task.Delay(-1);
        }

        private async Task handleMessage(SocketMessage msg)
        {
            Console.WriteLine(msg.Author + " : " + msg.Content);

            if(msg.Content.Contains("/leave"))
            {
                var embed = new EmbedBuilder() {
                    Title = msg.Author + " has left",
                    Description = msg.Author + " has left the Discord and would like everyone to know they did. They are very triggered.",
                    Color = Color.DarkRed,
                };

                await msg.Channel.SendMessageAsync("", false, embed);
            }
        }
    }
}

完整代码在https://github.com/Silthreent/Glurrr-Discord-Bot

Server missed last heartbeat atA MessageReceived handler is blocking the gateway task 的结果,这意味着您 运行 的命令之一使用了网关,并使其占用足够长的时间以让连接超时( ~30 秒)。

要解决此问题,您必须实施 a command handler which is part of the basic bot implementation. This will allow you to run commands async, preventing the thread blocking. You can also take a look at the bot sample 以查看所述实施的实际效果

我通过切换到 DiscordSharpPlus 而不是使用 DiscordNet 来修复它,但实际上仍然不知道问题出在哪里。