I am writing a bot in Discord.Net that checks online players on the minecraft server, but when I debug my bot and I give the server to it from Discord I have an error: A MessageReceived handler is blocking the gateway task. and about minute later bot disconnecting: System.Exception: Server missed last heartbeat at Discord.ConnectionManager.<>c__DisplayClass29_0.<b__0>d.MoveNext() and next Disconnecting How can I fix it? This is my code:
public async Task MessageReceivedAsync(SocketMessage message)
{
if (message.Author.Id == client.CurrentUser.Id)
return;
await message.Channel.SendMessageAsync("Adding server");
while (true)
{
if ((message.Content.StartsWith(@"\serwer ") || message.Content.StartsWith(@"\server ")) && !message.Content.EndsWith(".") && message.Content.Contains("."))
{
var url = message.Content.ToString();
if(url.StartsWith(@"\server "))
{
url = url.Replace(@"\server ", "");
}
else
{
url = url.Replace(@"\serwer ", "");
}
try
{
IMinecraftPinger pinger = new MinecraftPinger(url, 25565);
var status = await pinger.PingAsync();
if (status != null)
{
await client.SetGameAsync(status.Players.Online.ToString() + "/" + status.Players.Max.ToString());
}
}
catch
{
await client.SetGameAsync("Serwer offline :(");
}
await Task.Delay(15000);
}
}
}
I recently had something similar to this issue.
My discord bot would receive a command and some time in the future, I'd call an independent function send a message to a discord channel. My bot would disconnect and reconnect shortly after, but wouldn't complete sending the message.
The issue occurs when you call await async functions on the DiscordSocketClient like SendMessageAsync. I think calling functions like this when you aren't working inside of a command is the issue. Anyway, removing the await from the the async client function calls fixed the disconnection, thus allowing my