-
-
Notifications
You must be signed in to change notification settings - Fork 743
Description
Check The Docs
- I double checked the docs and couldn't find any useful information.
https://docs.discordnet.dev/guides/concepts/connections.html
Don't worry about trying to maintain your own connections, the connection manager is designed to be bulletproof and never fail - if your client does not manage to reconnect, you have found a bug!
Verify Issue Source
- I verified the issue was caused by Discord.Net.
Check your intents
- I double checked that I have the required intents.
Description
Discord.NET gateway does not reconnect when Discord's servers requests a disconnect. This happens after you've done nothing for a long period of time (I think it's somewhere between 15 minutes and an hour).
From a user's perspective, the bot appears to initially work, but after awhile, it'll stop receiving interactions or events, even though the program is still running and you'll see "application did not respond" or "interaction failed" in Discord. This resolves itself when you restart the program.
Note: The code sample included is basically a stripped down version of a basic bot, except for the snowflake option change.
I couldn't find a reconnect function, so I just recreated a new instance of DiscordSocketClient every time this happens. See code sample.
Version
3.16.0
Working Version
No response
Logs
// DiscordSocketClient.OnReady event fired
[Info] [Gateway] Ready
[Warning] [Gateway] A Disconnected handler is blocking the gateway task.
[Warning] [Gateway]
Server requested a reconnect
at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
[Info] [Gateway] Disconnecting
// DiscordSocketClient.OnDisconnected event firedSample
Reproduction
using Discord.WebSocket;
static DiscordSocketClient DiscordClient;
static string DiscordBotToken;
static async Task<int> Main(string[] args) {
DiscordClient = new DiscordSocketClient(new DiscordSocketConfig() {
UseInteractionSnowflakeDate = false
});
DiscordClient.Ready += Discord_OnReady;
await DiscordClient.LoginAsync(TokenType.Bot, DiscordBotToken);
await DiscordClient.StartAsync();
await Task.Delay(Timeout.Infinite);
return 0;
}
static async Task Discord_OnReady() {
ApplicationCommandProperties TestCommand = new SlashCommandBuilder()
.WithName("test")
.WithDescription("Prints \"Hello World\"")
.Build()
List<ApplicationCommandProperties> commands = new List<ApplicationCommandProperties> { TestCommand };
await DiscordClient.BulkOverwriteGlobalApplicationCommandsAsync(commands.ToArray(), RequestOptions.Default);
DiscordClient.SlashCommandExecuted += Discord_OnSlashCommandExecuted;
}
static async Task DiscordClient_OnSlashCommandExecuted(SocketSlashCommand request) {
request.RespondAsync("Hello World");
}Handling Disconnect
async Task DiscordClient_OnDisconnected(Exception exception) {
Console.WriteLine("Disconnected from Discord!");
// Recreate the entire Discord instance...
await DiscordClient.DisposeAsync();
CancellationTokenSource.Cancel();
Console.WriteLine("Reconnecting to Discord...");
// Regenerate a new "Discord"
Program.DiscordClient = new DiscordSocketClient(new DiscordSocketConfig() {
UseInteractionSnowflakeDate = false
});
Program.App = new App(Program.DiscordClient, Program.MailClient, Program.SqliteDatabase);
await Program.App.Run(); // DiscordClient.StartAsync()
}Packages
None
Environment
- OS: Windows 10 22H2 OS Build 19045.6332
- Architecture: x64
- SDK: 9.0.304