How can I respawn a disconnected client that was not spawned from a ShardingManager?

I've noticed after a long time my web-server will still be running but the bot will be disconnected from discord's gateway. I do not have access to the ShardClientUtil to respawn all shards since the bot was created using the WebSocketManager.
const { Events } = require('discord.js');
module.exports = {
name: Events.ShardDisconnect,
once: false,
async execute(client, event, id) {
console.log(`Shard ${id} disconnected from Discord with code ${event.code}.`);
try {
console.log(await client.ws.shards.get(id))
} catch (error) {
console.error('Error respawning the shard:', error);
}
},
};
const { Events } = require('discord.js');
module.exports = {
name: Events.ShardDisconnect,
once: false,
async execute(client, event, id) {
console.log(`Shard ${id} disconnected from Discord with code ${event.code}.`);
try {
console.log(await client.ws.shards.get(id))
} catch (error) {
console.error('Error respawning the shard:', error);
}
},
};
4 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
noahkaiser
noahkaiserOP17mo ago
-I am using discord.js v14.11.0 -I need to reconnect my discord bot after it disconnects from the discord gateway. I did not have a disconnect event file while the issue occured. Now I am creating one so that when it happens again, the bot can reconnect to the discord gateway. In the ready event I call await client.destroy(); const { Events, AuditLogEvent } = require('discord.js');
const { Events } = require('discord.js');

module.exports = {
name: Events.ClientReady,
once: false,
async execute(client) {
console.log(`Ready! Logged in as ${client.user.tag}`); //only fired once when the application starts/logs in
await client.destroy(); // destroy the socket and attempt to reconnect...
},
};
const { Events } = require('discord.js');

module.exports = {
name: Events.ClientReady,
once: false,
async execute(client) {
console.log(`Ready! Logged in as ${client.user.tag}`); //only fired once when the application starts/logs in
await client.destroy(); // destroy the socket and attempt to reconnect...
},
};
then the shardDisconnect event fires and I am trying to reconnect the bot to the discord gateway. i've noticed when I attempt to call await client.login() after the client is destroyed my ready event is not firing again.
const {
Events
} = require('discord.js');

module.exports = {
name: Events.ShardDisconnect,
once: false,
async execute(client, event, id) {
console.log(`Shard ${id} disconnected from Discord with code ${event.code}.`);
try {
await client.login(process.env.TOKEN) //does not trigger the ready event?
} catch (error) {
console.error('Error respawning the shard:', error);
}
},
};
const {
Events
} = require('discord.js');

module.exports = {
name: Events.ShardDisconnect,
once: false,
async execute(client, event, id) {
console.log(`Shard ${id} disconnected from Discord with code ${event.code}.`);
try {
await client.login(process.env.TOKEN) //does not trigger the ready event?
} catch (error) {
console.error('Error respawning the shard:', error);
}
},
};
-Before my web-server was still running but my discord bot was disconnected from the gateway randomly. -Now I am simulating the random disconnect that occurred before I had a handle for the disconnect. -How can I reconnect my bot given that it was created from a WebSocketManager? Can you show me how to activate debug mode? The bot disconnected again without anything logging in the shardDisconnect event probably due to an unrecoverable error like you said.
d.js docs
d.js docs17mo ago
Please add the following code to your code base outside of any other event listeners and provide the full log output relevant to your issue.
client
.on("debug", console.log)
.on("warn", console.log)
client
.on("debug", console.log)
.on("warn", console.log)
- Note: if you initialize your Client as bot or other identifiers you need to use these instead of client - If the output is too long to post consider using a bin instead: gist | paste.gg | sourceb.in | hastebin
noahkaiser
noahkaiserOP17mo ago
The issue was that the web-server instance was a webservice not a background worker for the specified site.
Want results from more Discord servers?
Add your server