sharding with workers

const bot = new Client({
shards: "auto",

intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages
],

presence: {
status: 'online',
activities: [{ name: '/help', type: ActivityType.Listening }],
},

makeCache: (manager) => {
if (necessaryManagers.includes(manager.name)) {
return new Collection();
}

if (userRelatedManagers.includes(manager.name)) {
return new Collection();
}

return new LimitedCollection({ maxSize: 0 });
},

sweepers: {
guildMembers: {
interval: 2700,
lifetime: 1200,
filter: () => member => member.id !== bot.user.id
},
users: {
interval: 2700,
lifetime: 1200,
filter: () => member => member.id !== bot.user.id
},
channels: {
interval: 2700,
lifetime: 1200,
filter: () => channel => true
},
guildChannels: {
interval: 2700,
lifetime: 1200,
filter: () => channel => true
},
roles: {
interval: 2700,
lifetime: 1200,
filter: () => role => true
},
guilds: {
interval: 2700,
lifetime: 1200,
filter: () => guild => true
},
permissionOverwrites: {
interval: 2700,
lifetime: 1200,
filter: () => permissionOverwrite => true
},
voiceStates: {
interval: 2700,
lifetime: 1200,
filter: () => voiceState => true
}
}
});
const bot = new Client({
shards: "auto",

intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages
],

presence: {
status: 'online',
activities: [{ name: '/help', type: ActivityType.Listening }],
},

makeCache: (manager) => {
if (necessaryManagers.includes(manager.name)) {
return new Collection();
}

if (userRelatedManagers.includes(manager.name)) {
return new Collection();
}

return new LimitedCollection({ maxSize: 0 });
},

sweepers: {
guildMembers: {
interval: 2700,
lifetime: 1200,
filter: () => member => member.id !== bot.user.id
},
users: {
interval: 2700,
lifetime: 1200,
filter: () => member => member.id !== bot.user.id
},
channels: {
interval: 2700,
lifetime: 1200,
filter: () => channel => true
},
guildChannels: {
interval: 2700,
lifetime: 1200,
filter: () => channel => true
},
roles: {
interval: 2700,
lifetime: 1200,
filter: () => role => true
},
guilds: {
interval: 2700,
lifetime: 1200,
filter: () => guild => true
},
permissionOverwrites: {
interval: 2700,
lifetime: 1200,
filter: () => permissionOverwrite => true
},
voiceStates: {
interval: 2700,
lifetime: 1200,
filter: () => voiceState => true
}
}
});
this is my constructor, i was told that it's possible to do worker mode with internal sharding to make it more efficient, how do i do that? version: 14.16.2
6 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
treble/luna
treble/luna3mo ago
one thing, you should not sweep guild, channels and roles. It breaks functionality and is unsupported, hence the big red warning
d.js docs
d.js docs3mo ago
:guide: Sharding: Getting started - Sharding file read more
TÆMBØ
TÆMBØ3mo ago
You want to specify mode: "worker" in the ShardingManager options
d.js docs
d.js docs3mo ago
:class: ShardingManager @14.16.3 This is a utility class that makes multi-process sharding of a bot an easy and painless experience. It works by spawning a self-contained ChildProcess or Worker for each individual shard, each containing its own instance of your bot's Client. They all have a line of communication with the master process, and there are several useful methods that utilise it in order to simplify tasks that are normally difficult with sharding. It can spawn a specific number of shards or the amount that Discord suggests for the bot, and takes a path to your main bot script to launch for each one. :propertysignature: ClientOptions#ws @14.16.3 Options for the WebSocket :interface: WebSocketOptions @14.16.3
_yaimsputnik5
_yaimsputnik5OP3mo ago
So this would be correct right?:
const bot = new Client({
shards: "auto",

ws: {
buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 2 })
},

intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages
],

presence: {
status: 'online',
activities: [{ name: '/help', type: ActivityType.Listening }],
},

makeCache: (manager) => {
if (necessaryManagers.includes(manager.name)) {
return new Collection();
}

if (userRelatedManagers.includes(manager.name)) {
return new Collection();
}

return new LimitedCollection({ maxSize: 0 });
},

sweepers: {
guildMembers: {
interval: 2700,
lifetime: 1200,
filter: () => member => member.id !== bot.user.id
},
users: {
interval: 2700,
lifetime: 1200,
filter: () => member => member.id !== bot.user.id
},
permissionOverwrites: {
interval: 2700,
lifetime: 1200,
filter: () => permissionOverwrite => true
},
voiceStates: {
interval: 2700,
lifetime: 1200,
filter: () => voiceState => true
}
}
});
const bot = new Client({
shards: "auto",

ws: {
buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 2 })
},

intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages
],

presence: {
status: 'online',
activities: [{ name: '/help', type: ActivityType.Listening }],
},

makeCache: (manager) => {
if (necessaryManagers.includes(manager.name)) {
return new Collection();
}

if (userRelatedManagers.includes(manager.name)) {
return new Collection();
}

return new LimitedCollection({ maxSize: 0 });
},

sweepers: {
guildMembers: {
interval: 2700,
lifetime: 1200,
filter: () => member => member.id !== bot.user.id
},
users: {
interval: 2700,
lifetime: 1200,
filter: () => member => member.id !== bot.user.id
},
permissionOverwrites: {
interval: 2700,
lifetime: 1200,
filter: () => permissionOverwrite => true
},
voiceStates: {
interval: 2700,
lifetime: 1200,
filter: () => voiceState => true
}
}
});
That's all i need to modify for this to work? Well one worker per shard is fine right? also i dont need to make another file or something, this is all?
Want results from more Discord servers?
Add your server