'sharding is required' error, with sharding

recently upgraded to the latest version of discord.js. using the default ShardingManager that djs offers, but when I start up my bot, i still spontaneously get errors of "Error: Sharding is required" from the websocket.js file (context: 45 shards, ~104,500 servers, djs v14.11.0, node.js v16.17.0)
2 Replies
d.js toolkit
d.js toolkit2y 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.
vivian <3
vivian <3OP2y ago
Unhandled promise rejection: Error: Sharding is required
at WebSocketShard.onClose (D:\Discord Bot @13\node_modules\@discordjs\ws\dist\index.js:1057:15)
at WebSocket.emit (node:events:513:28)
at WebSocket.emitClose (D:\Discord Bot @13\node_modules\ws\lib\websocket.js:258:10)
at TLSSocket.socketOnClose (D:\Discord Bot @13\node_modules\ws\lib\websocket.js:1264:15)
at TLSSocket.emit (node:events:525:35)
at node:net:757:14
at TCP.done (node:_tls_wrap:584:7)
Unhandled promise rejection: Error: Sharding is required
at WebSocketShard.onClose (D:\Discord Bot @13\node_modules\@discordjs\ws\dist\index.js:1057:15)
at WebSocket.emit (node:events:513:28)
at WebSocket.emitClose (D:\Discord Bot @13\node_modules\ws\lib\websocket.js:258:10)
at TLSSocket.socketOnClose (D:\Discord Bot @13\node_modules\ws\lib\websocket.js:1264:15)
at TLSSocket.emit (node:events:525:35)
at node:net:757:14
at TCP.done (node:_tls_wrap:584:7)
const { ShardingManager } = require('discord.js');
const configFile = require('./config.json');
const token = configFile[`token`];
const innerToken = configFile[`protocol2`];
const { eco } = require('./index.js');
const Discord = require('discord.js');
const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.DirectMessages, Discord.GatewayIntentBits.MessageContent],
partials: [Discord.Partials.Channel], });

const manager = new ShardingManager('./index.js', { token: `${token}` });
const shardCount = 45;

const main = async () => {
await client.login(innerToken);
let msg;

manager.on('shardCreate', async shard => {
console.log(`Launched shard ${shard.id}`)
await eco.bot.set(`shard`, `${shard.id}`)



const guild = client.guilds.cache.get(`870331544911372308`);
const chan = guild.channels.cache.get(`870526084825882715`);
if (shard.id == 1) {
msg = await chan.send(`:yellow_circle: **Automated Status Update**\n\nBot is currently restarting.\n(This message will be automatically edited when bot is online)`);
await msg.crosspost();
};
if (shard.id == shardCount - 1) {
await msg.edit(`:green_circle: **Automated Status Update**\n\nBot is online on all shards. (This message is automatic)`);
await client.destroy();
};

});

}
main();

// spawn shards
manager.spawn({ amount: shardCount, delay: 10000, timeout: 1 * 1000 * 60 });
const { ShardingManager } = require('discord.js');
const configFile = require('./config.json');
const token = configFile[`token`];
const innerToken = configFile[`protocol2`];
const { eco } = require('./index.js');
const Discord = require('discord.js');
const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.DirectMessages, Discord.GatewayIntentBits.MessageContent],
partials: [Discord.Partials.Channel], });

const manager = new ShardingManager('./index.js', { token: `${token}` });
const shardCount = 45;

const main = async () => {
await client.login(innerToken);
let msg;

manager.on('shardCreate', async shard => {
console.log(`Launched shard ${shard.id}`)
await eco.bot.set(`shard`, `${shard.id}`)



const guild = client.guilds.cache.get(`870331544911372308`);
const chan = guild.channels.cache.get(`870526084825882715`);
if (shard.id == 1) {
msg = await chan.send(`:yellow_circle: **Automated Status Update**\n\nBot is currently restarting.\n(This message will be automatically edited when bot is online)`);
await msg.crosspost();
};
if (shard.id == shardCount - 1) {
await msg.edit(`:green_circle: **Automated Status Update**\n\nBot is online on all shards. (This message is automatic)`);
await client.destroy();
};

});

}
main();

// spawn shards
manager.spawn({ amount: shardCount, delay: 10000, timeout: 1 * 1000 * 60 });
client is used to log into another bot, not the one being sharded. the token used with client automatically sends a status update to the bot's official support server
const fs = require('fs');
const Discord = require('discord.js');
const db = require('quick.db');
const tf = require('@tensorflow/tfjs-node');
const { m, m2, pokeArr } = require('./utils/data/arrs.js'); // arrays of pokemon names
const { n } = require('./utils/functions.js'); // convenient helper functions
const { languages } = require('./utils/data/languages.js'); // list of languages and code-abbrevs for translate functions
const configFile = require('./config.json');
const token = configFile[profile];

const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.DirectMessages,
Discord.GatewayIntentBits.MessageContent
],
partials: [
Discord.Partials.Channel
],
restRequestTimeout: 60000,
});
const mod = new db.table("mod");
const eco = {
bot: new db.table("bot"),
guild: new db.table("guild"),
prefix: new db.table("prefix"),
reg: new db.table("reg"),
user: new db.table("user"),
seid: new db.table("seid"),
cd: new db.table("cd"),
eco: new db.table("eco"),
corp: new db.table("corp"),
server: new db.table("server"),
aegis: new db.table("aegis"),
market: new db.table("market")
};

// Export all needed values, dependencies, functions, and files
module.exports = { Discord, m, m2, pokeArr, n, languages, mod, eco };

// Initiate Collections
client.afk = new Discord.Collection();
client.commands = new Discord.Collection();
client.help = new Discord.Collection();
const fs = require('fs');
const Discord = require('discord.js');
const db = require('quick.db');
const tf = require('@tensorflow/tfjs-node');
const { m, m2, pokeArr } = require('./utils/data/arrs.js'); // arrays of pokemon names
const { n } = require('./utils/functions.js'); // convenient helper functions
const { languages } = require('./utils/data/languages.js'); // list of languages and code-abbrevs for translate functions
const configFile = require('./config.json');
const token = configFile[profile];

const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.DirectMessages,
Discord.GatewayIntentBits.MessageContent
],
partials: [
Discord.Partials.Channel
],
restRequestTimeout: 60000,
});
const mod = new db.table("mod");
const eco = {
bot: new db.table("bot"),
guild: new db.table("guild"),
prefix: new db.table("prefix"),
reg: new db.table("reg"),
user: new db.table("user"),
seid: new db.table("seid"),
cd: new db.table("cd"),
eco: new db.table("eco"),
corp: new db.table("corp"),
server: new db.table("server"),
aegis: new db.table("aegis"),
market: new db.table("market")
};

// Export all needed values, dependencies, functions, and files
module.exports = { Discord, m, m2, pokeArr, n, languages, mod, eco };

// Initiate Collections
client.afk = new Discord.Collection();
client.commands = new Discord.Collection();
client.help = new Discord.Collection();
(async function () {
// Load models

let model = await Promise.all([
tf.loadLayersModel(`file://./utils/models/1-180/model.json`),
tf.loadLayersModel(`file://./utils/models/181-360/model.json`),
tf.loadLayersModel(`file://./utils/models/361-540/model.json`),
tf.loadLayersModel(`file://./utils/models/541-720/model.json`),
tf.loadLayersModel(`file://./utils/models/721-898/model.json`),
tf.loadLayersModel(`file://./utils/models/899-fin/model.json`)
]);


/*
let model = await Promise.all([
tf.loadLayersModel(`file://./utils/models/1-272/model.json`),
tf.loadLayersModel(`file://./utils/models/273-544/model.json`),
tf.loadLayersModel(`file://./utils/models/545-816/model.json`),
tf.loadLayersModel(`file://./utils/models/817-1088_event/model.json`),
]);
*/

// Event Handler
const folders = ["admin", "legacy", "main"];
for (const val in folders) {
fs.readdir(`./events/${folders[val]}/`, async (err, files) => {
files.forEach(async (file) => {
const eventFunction = require(`./events/${folders[val]}/${file}`);
client.on(eventFunction.event, (...args) => eventFunction.run(...args, client, model));
});
});
}

// Command Handler
const subdir = ["a_suite", "ai", "misc", /*"moderation",*/ "pokemon", /*"pride",*/ "reps", "standard", "tags", "economy"];
const subdirs = subdir.values();
for (const dir of subdirs) {
const commandFiles = fs.readdirSync(`./commands/${dir}`).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${dir}/${file}`);
client.commands.set(command.data.name, command);
client.help.set(command.data.name, command.cmdInfo);
};
};

client.login(token);
})();


// Debug, Warn, and Unhandled Rejections
//client.on("debug", console.log)
client.on("warn", console.log);
process.on('unhandledRejection', error => console.error('Unhandled promise rejection:', error));
(async function () {
// Load models

let model = await Promise.all([
tf.loadLayersModel(`file://./utils/models/1-180/model.json`),
tf.loadLayersModel(`file://./utils/models/181-360/model.json`),
tf.loadLayersModel(`file://./utils/models/361-540/model.json`),
tf.loadLayersModel(`file://./utils/models/541-720/model.json`),
tf.loadLayersModel(`file://./utils/models/721-898/model.json`),
tf.loadLayersModel(`file://./utils/models/899-fin/model.json`)
]);


/*
let model = await Promise.all([
tf.loadLayersModel(`file://./utils/models/1-272/model.json`),
tf.loadLayersModel(`file://./utils/models/273-544/model.json`),
tf.loadLayersModel(`file://./utils/models/545-816/model.json`),
tf.loadLayersModel(`file://./utils/models/817-1088_event/model.json`),
]);
*/

// Event Handler
const folders = ["admin", "legacy", "main"];
for (const val in folders) {
fs.readdir(`./events/${folders[val]}/`, async (err, files) => {
files.forEach(async (file) => {
const eventFunction = require(`./events/${folders[val]}/${file}`);
client.on(eventFunction.event, (...args) => eventFunction.run(...args, client, model));
});
});
}

// Command Handler
const subdir = ["a_suite", "ai", "misc", /*"moderation",*/ "pokemon", /*"pride",*/ "reps", "standard", "tags", "economy"];
const subdirs = subdir.values();
for (const dir of subdirs) {
const commandFiles = fs.readdirSync(`./commands/${dir}`).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${dir}/${file}`);
client.commands.set(command.data.name, command);
client.help.set(command.data.name, command.cmdInfo);
};
};

client.login(token);
})();


// Debug, Warn, and Unhandled Rejections
//client.on("debug", console.log)
client.on("warn", console.log);
process.on('unhandledRejection', error => console.error('Unhandled promise rejection:', error));
perfect, thanks! i don't know how i didn't see that... the last time i had that error i fixed it the exact same way i need myself some coffee 😅
Want results from more Discord servers?
Add your server