Search code examples
c#raspberry-piraspbiandiscorddiscord.net

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


I put my Discord bot on a Rasberry Pi but now I'm having an issue with it. When I try to use a command, it works the first time. But using it a second time, instead of working it just says "A MessageReceived handler is blocking the gateway task.". Shortly afterwards it then says

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

and disconnects, trying to reconnect some but erroring everytime. I'm not using the command interface, but I am using async/await. It works on my normal computer perfectly fine, only breaks on my 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);
            }
        }
    }
}

Complete code is at https://github.com/Silthreent/Glurrr-Discord-Bot


Solution

  • I fixed it by switching to DiscordSharpPlus rather than using DiscordNet, still don't actually know what the problem was.