Closed Ratstail91 closed 6 years ago
Discord will every so often (seemingly random/unpredictable intervals of time) close the connection a bot is using. When that happens, the 'disconnect' event will be fired on the Client and the code
parameter will be equal to 1000 (the general disconnect code). When that happens, you just need to tell the bot to reconnect.
If you'd like an example, I've got one here at https://github.com/ahlec/Phil/blob/master/src/phil.ts, but the relevant parts (simplified for example) are as follows:
class MyBot {
public start() {
...
this.bot.on('disconnect', this.onDisconnect.bind(this));
...
}
...
private onDisconnect(err: Error, code: number) {
if (code === 1000) {
console.error('Reconnecting now...');
this.bot.connect();
}
}
...
}
This is about the most annoying thing in the world right now. It can't be Discord's fault, can it? It's disconnecting and reconnecting over and over despite my having no connection problems for several days now.
@DragonOfMath is the error code always 1000? because if it isn't 1000 it's probably disconnecting for a separate reason. but if it is, though, whether or not it's because of Discord or because of this library, there's nothing to be lost from coding under the assumption that your connection will timeout at some point and should re-establish itself if necessary.
(as for it being discord's fault or not, i don't really know, but i'd guess it's probably something they're doing, yeah. I can't tell from the screenshot how far apart the connection and disconnection are, but I'd imagine that Discord terminates connections if they've been open for a certain duration of time as a defence against programmers who would open connections and either forget to close them after they're done or would intentionally leave them open for malicious reasons)
Thanks, I'll try this next time I'm working on it.
My bot seems stable now, so thank you @ahlec ! I'm closing this now.
I'm running a cloned branch (https://github.com/Woor/discord.io/tree/gateway_v6) using forever.js. However, even with forever, the bot logs out without warnings or errors.
Here's my bot: https://github.com/Ratstail91/discordbot
Thanks in advance.