#archive-sharding
12465 messages · Page 13 of 13 (latest)
message.guild.shardId
how can i import my client into ShardingManager file?
You don’t, what’s the use case?
The clients (yes, plural, one per shard) are instances of the Client class and only exist in the childprocesses (or worker threads) that got spawned by the ShardingManager (named shards). You can only get plain JSON objects from there, never class instances
I managed to do what I wanted using client.on('shardReady'... in my index.js
This is working
Depends on if they use internal or traditional sharding
what's a shard? 😳
oh, ok, I'm doing it by myself with child_process :p
thank you!
So I'm trying to host my sharded bot using railway but discord send me a msg saying my bot trying logging in 1k times in a short period of time so they reset the token, anyone knows why that's happening?
Here's the code if u need it
const { ShardingManager } = require("discord.js");
const manager = new ShardingManager("./bot.js", {
totalShards: "auto",
token: process.env.token
});
manager.on('shardCreate', async (shard) => {
console.log(`${shard.id} shards launched`)
shard.on('error', (error) => {
console.log(error)
})
})
manager.spawn();
What version of djs?
13.2.8 ig
Update to 13.8.1
Ok
Ig that's gonna be a bit of rewriting then
Shouldn’t be much, since it‘s only minor versions, no huge breaking changes in djs itself, but might be some changes if you use builders
i need help 🥲 it won’t sent you the channel
client.shard.broadcastEval((c, { channelId, newGuild }) => {
const ch = c.channels.cache.get(channelId)
if (ch) {
ch.send({ embeds: [newGuild] })
}
}, { context: { channelId: logChannel, newGuild } })
Did you console.log the context you pass in there to check it is what you expect?
For cluster
Yes?
How to cluster
By starting different ShardingManager on different machines and passing them arrays containing the shard ids they should spawn. You’d need to implement your own way to communicate between these different machines though, as djs only supports IPC directly
i found the issue and it was because the logChannel was invalid but thank you
Can I someone confirm something rq
If a guild's events aren't handled by my current shard, I can still .fetch my way through to said guild?
No, you shouldn’t fetch guilds that aren’t on your current shard. Use broadcastEval to get information from a guild on another shard
How can do cluster in bot
Do you already do traditional sharding with ShardingManager?
Then you‘d pass shardList to the ShardingManager containing a list of the shard IDs that ShardingManager should spawn. Also set totalShards to the amount of all shards that all ShardingManager use.
The hard part is that you then need to implement your own way for cross-shard communication across the different machines, if you need anything like broadcastEval or fetchClientValues yet.
So one machine could spawn shardList: [0,1,2,3,4,5,6] while the next does 7-12 etc.
How can setup it? any example
What more example do you need? You need several VPS to run your code on, each one having a subset of shardList
Okay
How can I setup both machines for cluster etc stuff
Like any package require or held by djs
You‘d install your current bot on both and literally only change the shardList and totalShards property to not be 'auto' (in case they were)
And maybe add the inter-guild communication I talked about if you need it
Oh host bot in 2 machines
1 to 6 shard I'm one 7 to etc in second
Depends on resources you have available and what your bot uses of course. 1-6 was just an example
How can setup shard like half - half to total servers
Like what if both shard launch in same server at same time
By finding out how many shards you need and then assign 1-half to the first and half+1 to last to the other server
Can you explain by a short src example
Simple and smart. Thx.
Don’t really see which part of this you struggle with. I named the properties inside the ShardingManager options you need to pass, I named an example for their values. If that doesn’t suffice for you to get it working I‘m wondering how you managed to get a bot running needing clustering in the first place…
Hi, why is it going ready https://media.discordapp.net/attachments/927630453932970034/993365142400147521/IMG_1752.png
It constantly doing it, its not normal
It normally don’t say ready only reconnecting then resumed
what your djs version? there is was a bug that shard re-identify on resuming
What is worker and process ? and what the deference of them
...
why dont you actually use search bar ? ^
Hey, when I start my bot it uses 36mb of ram bot when it starts the shard it shoots to 144mb of ram and doesn't go back down. Does anyone know how to fix this?
@solid walrus You don’t get guilds from other shards. Since you won’t be able to use them outside of their shard anyway. What is your use-case. (Move sharding related conversations here)
I want to get the guild and send it back as is
To do what with it? Because it will be plain JSON data, not a class instance anymore… see pins in this channel
I have an API for my Dashboard and to get the discord server name, etc... I get the server with the bot and return the response as is
Then just use client.guilds.cache.get(context) not fetch, that way only the shard having that guild will return something that‘s not undefined
Or even better use the shard: property in broadcastEval‘s second parameter to only ask that shard to begin with.
Documentation suggestion for @solid walrus:
(static) ShardClientUtil.shardIdForGuildId()
Get the shard id for a given guild id.
Like this ?
const guildId = "123153351351513"
client.shard.broadcastEval(async (c, context) => {
return await c.guilds.fetch(context).catch(() => {
return null
})
}, { context: guildId, shard: client.shard.shardIdForGuildId(guildId) }).then(v => {
if (v[0] == null) return reply.send({error: 401, message: "Incorrect GuildID"})
return reply.send(v[0])
})
"discord.js": "^13.5.0"
update to "discord.js": "^13.8.1" ?
shareIdForGuildId gets the total amount of shards as second parameter. Pass client.shard.count for that
And it’s a static method, so ShardClientUtil. not client.shard.
And you still fetch when you should cache.get
without broadcast ?
Mmmmh 🤔 OK
No, the broadcastEval stays
How do i get the shard number for the interacting guild
Traditional or internal sharding?
internal
interaction.guild.shardId
ty
to get total shards its :client.options.shards.length?
shardCount
ok
and for my bot status i want it display...
Shard: currentShardOfGuild / TotalShards
With internal sharding I don’t know if you can set different status‘ for different shards… gotta double check
ok
Didn’t find anything supporting that
ohk
@nimble mist
client.shard.broadcastEval(async (c, context) => {
return c.guilds.cache.get(context['guildId'])
}, { context: request.params }).then(v => {
if (!v[0]) return reply.send({error: 401, message: "Incorrect GuildID"})
return reply.send(v[0])
}).catch(() => {
return reply.send({error: 401, message: "Incorrect GuildID"})
})
Same error
Where do you run that code?
Because it looks like you have an issue with IPC not working as it should
What version of djs? And did you enhance ShardingManager or Shard in any way or use a Third party library for it?
On my server 🤔
13.8.1
I only use discord.js even for shards
Show your ShardingManager file please
const Discord = require('discord.js');
const config = require('./setup/config.json')
// ------------- Shard Management ------------------
const manager = new Discord.ShardingManager("./index.js", {
totalShards: "auto",
token: config.token
})
manager.spawn([this.totalShards, 15000, 60000])
.then(shards => {
shards.forEach(async shard => {
const gNumber = await shard.eval('this.guilds.cache.size')
console.log(`[SHARD] Shard ID #${shard.id} -> ${gNumber} guilds`)
});
})
.catch(console.error);
how to fix bot starting twice. I trying to move from internal sharding to traditional
starting 2 instances on startup*
A) pass the number 2 to the spawn, not the ShardingManager constructor
B) don‘t pass any shards: property to the Client constructor
Do you have an example?
client.shard.ids
Umm, spawn takes an object with amount, timeout, delay properties. Not an array. I‘m confused why that even works…
I just looked on discordjs.guide and I'm going to take a base from 0 and we'll see if it prefers
Shard:
const Discord = require('discord.js');
const config = require('./setup/config.json')
// ------------- Shard Management ------------------
const { ShardingManager } = require('discord.js');
const manager = new ShardingManager('./index.js', { token: config.token });
manager.on('shardCreate', shard => console.log(`[SHARD] Shard ID ${shard.id}`));
manager.spawn();
Code:
client.shard.broadcastEval((c, context) => {
let gui = c.guilds.cache.get(context['guildId'])
if (gui) {
return gui
}
return null
}, { context: request.params }).then(v => {
if (v[0] == null) return reply.send({error: 401, message: "Incorrect GuildID"})
return reply.send(v[0])
}).catch((err) => {
return reply.send({error: 401, message: "Incorrect GuildID"})
})```
It's OK but guilds returned are unavailable 🤔
it's not OK..... 😐
Is that your only eval you call on shards? Or could it be another call causing that error?
It's this call because when I remove this part I don't get an error...
I‘m just confused because neither ShardingManager, Shard, ShardClientUtil or Client have a replace call outside of the login function (which won‘t get called upon broadcastEval)
And your code doesn’t either.
That's why I don't understand either 🤔
@nimble mist I found !
if I put this, I have an error:
return gui
But if I put for example :
return gui.id
It's OK
I found @nimble mist all problem come from Discord Intents
I forget to put Discord.Intents.GUILD
Thank you very much for your help
how do i spawn 2 shards manually? traditional sharding
Pass {amount:2} to the manager.spawn call
const { ShardingManager } = require("discord.js");
const config = require("./src/config.js");
const manager = new ShardingManager('./src/bot.js', {
token: config.token
});
manager.spawn({amount: 2});
like this?
Also how do I get total shards and current shards on guild
client.shard.ids[0] and client.shard.count
ok
In internal sharding can i limit how much guilds can go to 1 shard?
hello, how can i send dms messages to a user using sharding manager? 
Why do you suddenly switch to internal sharding? Which guild goes to which shard is based solely on guildId and total amount of shards. So you can only limit that by increasing the amount of shards.
You can send DMs from any shard (no need to go through the ShardingManager). Their response will only reach shard 0 though…
I’m working on 2 different bots
it's still working if not using broadcastEval while sending DMs?
oh okay, but if the user isn't in shard 0, it's still working for DMs the user?

Users don’t belong to a specific shard, Guilds do
messageCreate Events for DMs will be sent to shard 0 though
So messageCollectors will only work in DMs on shard 0 too
ah i see
thanks a lot for the information

Hello
How put a function in broadcastEval ?
function test() {
return "hello world"
}
client.shard.broadcastEval((clientShard, context) => {
return context.test()
}, context: { test: test }) // doesnt work (ERROR: context.test() is not un function)
Thanks
Put the function inside of the broadcastEval
You can’t pass functions via broadcastEval bc it’s not json serializable. And even if you do, it can’t use the variables you defined in the upper scope
when using ShardingManager, how many shards will be spawned per subprocess?
One
Will Mongodb will be an ideal database for traditional shading?
also is there a way to get shard id from client object?
or maybe client.guilds.cache.first().shardId?
client.shard.ids is an array of ids in that client’s process (usually only one id with traditional sharding only)
oh okay
thanks
how do i assign only selected guilds to a shard?
Not possible, only Discord can change the default guild-shard assignment behavior
And they only do that for larger bots, if necessary
oh, got it
When we spwn a shard process using sharding. 1 shard takes 1 thread?
So if I have a CPU with 6 Cores and 12 Threads. It takes 1 thread to spawn a shard.
So I can spawn 11 shards and 1 host?
or maybe 10 shards and 1 host and 1 for system use?
You can run as many processes as you want on that CPU (at least I hope you have an OS that supports more than 12 processes running simultaneously). A shard is a child process or a worker thread. Not a CPU core blocking magic thing
I am using Debian. Oh so, a worker thread is something else? hmmm.
Hey friends how can i fix the SHARDING_READY_TIMEOUT error even the infinity wait doesn't seem to work out for me and its still giving this error
Tag suggestion for @bold orbit:
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)
• 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 | hasteb.in | sourceb.in | hastebin
@nimble mist thanks for the log tip , here more details on this same issue
What's ur d.js version? npm ls discord.js
npm ls discord.js
00-discordbot-api@1.0.0 D:\te-ordenen\BrawlStars-game-files\00-discordbot-api
└── discord.js@13.8.1
i think this is the last version you can have
Looks like you should increase your timeout in manager.spawn of your sharding file. Since your client does seem to become ready but only after that timeout happens
-1 or infinity should disable the timeout , but it doesn't . also tried putting it rly high like 9999999 but that also didn't work
I also got this issue today. How can I disable the timeout?
manager.spawn({ timeout: -1 })
Was the solution ;D
All 7 Shards are alive again
yeah this works , life saver !
Yeah! But its strange. The last months it working fine until today where I restarted the bot for an code change
yeah same just from this week it started to act like this. then i was checking what was going on and the timeout value was not used , checked github and it was not there. so we indeed needed this object to define it ourself
Where did you define the timeout value if not there?
in the shardmanager like it was before
By before you mean v12? Because it’s been like this since v13.0.0
can I create a single db connection in the main file and not in shard file? So all shards only access a single db instance.
or do I have to create new connections on every shard?
I am using mongodb
While you can that would mean all DB requests would have to go through the IPC channel and same for the results.
what's IPC?
inter process communication
oh so the link between processes/shards?
The communication link, yes. What’s being used for broadcastEval etc
oh okay Thanks, so it's best to create new connection for each shard?
no as in v13dev
Ah, so before April 2021…
It’s what I do, yes. Else the db connection Is too slow
Got it
Thanks :)
Hey, when sharding. Is each shard is a worker thread? If yes then can I spawn more threads inside the shard?
I tried spawning a worker and it worked. So each shard is a new process?
You can set the mode in ShardingManager to either process or worker
ohh okay thanks
Whenever I start my bot, I have Shard ID 0 logging undefined servers.
bot.shard.broadcastEval(client => console.log('Shard ID', client.shard.ids[0], client.guilds.cache.map(guild => guild.name)));
Hi, I have this error Shards are still being spawned. when I launch the bot, I try to console.log(client.guilds.cache.size) because the bot is connecting to all shards. Is there a way to wait for the end of all the connections and then display in the console the number of guilds?
When do you send that broadcastEval?
In the ready event.
The way I do it is attach a listener to each shard‘s ready event and add their id to a Set. When the Set‘s size reaches the count of shards I send an 'allShardsReady' message to all shards. Then they know they can start doing sharding stuff
Of which shard?
Shard 0.
Well, shard 0 will be ready before all other shards will be (since it gets spawned first), so the other shards won‘t have all their guilds sent to them yet
I only have one shard. Error [SHARDING_READY_TIMEOUT]: Shard 0's Client took too long to become ready.
Do you have an example please because I don't really understand how it works
Why do you have a broadcastEval etc. in there if you only have one shard?
Cause I sometimes test it out with two shards.
However, why would it sometimes result with undefined servers?
const readyShards = new Set();
manager.on("shardCreate", (shard) => shard.on("message", (message) =>
{
if (message.fullyLoaded && startTime)
{
readyShards.add(shard.id);
if (readyShards.size === manager.totalShards)
{ … }}}));
That’s what my ShardingManager has
Because the API didn’t send those Guild‘s payload yet
Okay, thanks, I understand better
Is it possible that I could wait, like, 10 seconds before executing the code inside the ready.js event?
Maybe that would fix the issue while I figure out on my canary bot?
Sure, setTimeout exists
Well, that didn't work. Keeps erroring right and left.
I still can't start my bot. :/
Just checking: you start through a ShardingManager? But only 1 shard?
Yes.
Then add timeout:-1 in the object passed to <ShardingManager>.spawn
manager.spawn({ delay: 10000, timeout: -1 });
Well, let's hope it works locally so I can then try on the VPS.
Damn, still didn't start yet.
Hi, is it impossible to use the shardsManager system in ES6?
You mean ESM?
No, just set type to module in the package.json or use the mjs extension
Ah yes, perfect, thank you
Is there any way to detect when all the shards are connected? Because when my bot starts, it should update its status but I get an error because not all shards are connected and I have to add a timeout.
You can await the manager.spawn call
Perfect, thank you
That only works with timeout not set to -1 or Infinity btw.
Hello, is it possible to have the list of all the servers? Because when I do a function to get the full list, I only get the current shard servers. If I use await client.shard.fetchClientValues("guilds.cache.size");, I get an error because not all shards are connected yet.
Then call that function only once all shards are connected 🤷♂️
Ok, but I can't use to know when all shards are connected in the bot.js file like in the tuto https://discordjs.guide/sharding/#fetchclientvalues
I get this error: This session would have handled too many guilds - Sharding is required. because I export a function that returns the number of guilds. If I delete the function, I don't have the problem anymore
You can either await the promise of ShardingManager.spawn() or you can simply catch the rejection you get and retry in a setTimeout until you don’t get that rejection
Okay, I understand. But if I want to make a promise, how can I do that? Because I can't export my discord client
You need to await in the ShardingManager and then send a broadcastEval to all shards to inform them of the finished login
I send a broadcastEval in my bot.js file? Because if I do that, I get this error This session would have handled too many guilds - Sharding is required. because in my index file, I import the function and even if I import it without using it, I get the error
Where do you export (and import) that function?
You can’t import your ShardingManager in the Client‘s file. You need to use broadcastEval/fetchClientValues or process.send(…) directly to communicate with manager/other shards
Export my function to the bot.js file (This is where my discord client is created.) and then import the function to the index file (shardsManager file)
Okay, is there any example or documentation on this subject?
Yeah, don’t do that. ShardingManager and Client run in seperate processes. Importing one into the other means you create more Clients
Which part? BroadcastEval and fetchClientValues are explained in the sharding portion of the guide (see pins of this channel). process.send is a node standard function
Okay, thanks for your help, I'll look into it
ErrorCaptureStackTrace(err);
^
Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed
at new NodeError (node:internal/errors:372:5)
at process.target.send (node:internal/child_process:741:16)
at /root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/ShardClientUtil.js:85:17
at new Promise (<anonymous>)
at ShardClientUtil.send (/root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/ShardClientUtil.js:83:12)
at /root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/ShardClientUtil.js:121:12
at new Promise (<anonymous>)
at ShardClientUtil.fetchClientValues (/root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/ShardClientUtil.js:108:12)
at _loop (/root/GolyatsecGuardProd/codebase/web/index.js:143:55)
at Timeout._onTimeout (/root/GolyatsecGuardProd/codebase/web/index.js:136:17) {
code: 'ERR_IPC_CHANNEL_CLOSED'
}
I am getting this error when using ShardingManager. Can anyone help?
Did your ShardingManager keep on running after that and respawn the shard? Because it looks like the ShardingManager died first… or your host doesn’t support IPC somehow
Yep my shard manager keeps running after crash and tries again and again.
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
Error [SHARDING_IN_PROCESS]: Shards are still being spawned.
at ShardingManager._performOnShards (/root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/ShardingManager.js:285:75)
at ShardingManager.fetchClientValues (/root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/ShardingManager.js:266:17)
at Shard._handleMessage (/root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/Shard.js:359:22)
at ChildProcess.emit (node:events:527:28)
at emit (node:internal/child_process:938:14)
at processTicksAndRejections (node:internal/process/task_queues:84:21)
[8:27:10 PM] [Discord] Shard 0 died.
[8:27:10 PM] [Discord] Shard 0 spawned.
/root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/Shard.js:161
reject(new Error('SHARDING_READY_DIED', this.id));
^
Error [SHARDING_READY_DIED]: Shard 0's process exited before its Client became ready.
at Shard.onDeath (/root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/Shard.js:161:16)
at Object.onceWrapper (node:events:642:26)
at Shard.emit (node:events:539:35)
at Shard._handleExit (/root/GolyatsecGuardProd/node_modules/discord.js/src/sharding/Shard.js:407:10)
at ChildProcess.emit (node:events:527:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) {
[Symbol(code)]: 'SHARDING_READY_DIED'
Wait, that error looks like it comes from a dashboard or some other webservice running in the same process. Does that check if all shards are spawned before calling fetchClientValues ?
Hmm wait ill look into that. Could this have something to do with Contoba? (VPS)
Theoretically, sure. I don’t know them, but if it works locally but not on your VPS that should be a strong indication
Yep works in local environment.
Then it’s most definitely an issue with your VPS and how it does/doesn’t allow childprocesses and IPC channels
Thank you for your help. That issue definitely due to our VPS.
When my shard error whats all the possible reason for it
Be more specific. what error does your shard produce?
gonna have to come back when it happens again, the console is overwritten
Hi 👋 , I'm trying to get the memory usage of my shards, but I can't, I have my code here, did I do something wrong?
const promise = await client.shard.broadcastEval(`[this.uptime, process.memoryUsage().heapUsed]`)
error:
reject(new TypeError('SHARDING_INVALID_EVAL_BROADCAST'));
TypeError: SHARDING_INVALID_EVAL_BROADCAST
You have to use functions, instead of strings (depriciated)
Update: I migrated from Contabo to Google servers. But there was no solution. I kept getting the error. Now I got api limit blocked from Discord. My bot is not active. I will wait 1 day. My project only works locally. Very interesting. As a last resort I will be using a Windows machine.
if i set a global variable as the shardID, would it overwrite in process mode to the other shards?
No
Processes don’t share memory
ok thank you
Not that you should be using global variables
ik
Hi, someone has an example to get the whole server list via client.shar.fetchClientValues('')
Suggestion for @rough heron:
Sharding: FetchClientValues
read more
If you want the actual list and not just the count you‘d need to use broadcastEval instead.
ho ok, do you have one example ? because I can't find in the documentation (for the memory) c:
Documentation suggestion for @wispy citrus:
ShardClientUtil#broadcastEval()
Evaluates a script or function on all shards, or a given shard, in the context of the [Client](<https://discord.js.org/#/docs/discord.js/stable/class/Client>)s.
it has an example
it also has examples in guide, take a look at it
ok thanks !
Hi everyone, does anyone know how I can resolve that :
1|Anonymous | [2022-07-11T20:46:23.968Z] Error [SHARDING_READY_TIMEOUT]: Shard 1's Client took too long to become ready.
1|Anonymous | at Timeout.onTimeout (/home/bot/anonymous-bot/node_modules/discord.js/src/sharding/Shard.js:166:16)
1|Anonymous | at listOnTimeout (node:internal/timers:559:17)
1|Anonymous | at processTimers (node:internal/timers:502:7) {
1|Anonymous | [Symbol(code)]: 'SHARDING_READY_TIMEOUT'
1|Anonymous | }
It's when I would like to restart my bot
[SHARDS]: Launched shard 0
Error [SHARDING_IN_PROCESS]: Shards are still being spawned.
[SHARDS]: Launched shard 1
Whenever I have two shards, it errors this. 
Do you try to broadcastEval or fetchClientValues before all shards are spawned?
Wait, so my ready.js event fires from the very first shard getting spawned?
It fires for each shard individually, yes
The manager.spawn(…) resolves when all shards are ready. Use that to make your shards do stuff that needs broadcastEval etc
Is there no resolve parameter?
Where?
In the shardCreate event or something.
Umm, what should that parameter do exactly?
Nevermind, my bad.
require('dotenv').config();
const { ShardingManager } = require('discord.js');
const manager = new ShardingManager('./src/index.js', { token: process.env.TOKEN, totalShards: 2 });
manager.on('shardCreate', shard => {
shard.on('ready', async () => {
if (manager.shards.size === manager.totalShards && manager.shards.every(x => x.ready)) await manager.broadcast('allReady');
});
console.log(`[SHARDS]: Launched shard ${shard.id}`);
});
manager.spawn({ delay: 10000, timeout: -1 });
I decided to make something like this, but it never really broadcasted allReady.
For some reason, I'm facing this issue:
console.log(manager.shards.every(x => x.ready)); // true
console.log(manager.shards.size === manager.totalShards); // false It's `1 2`.
And did you try logging both values you compare there seperately?
Indeed.
Check the updated code for the logs.
Then your second shard didn’t get spawned… listen to the debug event to find out more
Is it possible to wait it out?
As I said: the <ShardingManager>.spawn() call‘s Promise resolves when all shards are ready
Is it possible to get the status of the promise?
We‘re entering basic JS territory now… #rules 2
Let's do it.. alright. 👍
Tag suggestion for @pale sapphire:
Resources to understand Promise:
• MDN: learn more
• Guide: learn more
• JavaScript info: learn more
I got it solved. 👋
Hi, what should I put in the "select redirect url" box?
@brazen herald
Nothing, what do you need guilds scope for?
Hello, my bot should send a message when a Twitch streamer starts a stream but it can't send the message because the server is not in the right shards. Do you have an idea to solve this problem?
I tried to use broadcastEval but I get this error: Cannot read properties of null (reading 'broadcastEval')
What did you call broadcastEval on?
discord.shard.broadcastEval(`this.channels.cache.get('123..').send({ embeds: ${embed.toJSON()} })`)
How is discord defined?
import { Client } from 'discord.js';
import {} from 'dotenv/config';
const discord = new Client({
intents: [
'GUILD_MEMBERS',
'GUILD_PRESENCES',
'GUILDS',
'GUILD_INTEGRATIONS',
'GUILD_MESSAGE_REACTIONS',
'GUILD_MESSAGES',
'DIRECT_MESSAGES'
],
restRequestTimeout: 60000,
waitGuildTimeout: 1000,
partials: ['CHANNEL']
});
discord.login(process.env.DISCORD_Token);
discord.once('ready', async () => {
discord.shard.broadcastEval('this.channels.cache.get("123..").send({ content: "test message ...." })');
});
This is my code for testing but it doesn't work. I load my file with the ShardingManager like on the doc
@dense cape
There’s got be smth funky going on bc it can only be null or a ShardClientUtil, not undefined
Oh wait, it does say null
How did you startup the bot?
I made this tuto https://discordjs.guide/sharding/#when-to-shard .
So my file is bot.js but I launch my bot with index.js
does broadcastEval takes an async function?
It can, yes
thanks :)
I have this broadcastEval() code, however:
bot.shard.broadcastEval(client => console.log('Shard ID', client.shard.ids[0], client.guilds.cache.map(guild => guild.name)));
When it comes to the results of the guild names, I can see, supposedly, guild called ABC in shard 0 and shard 1? What does this mean and is there something I should be doing?
i wanted to know the eta for launching @discordjs/sharder by kyra
Seems like you either have two different guilds names ABC or you tried to fetch ABC guild on a shard that shouldn’t handle it
Or you have shard overlap from wrongly configuring shards
Different guild names ABC, I checked, that's not the case.
How can I be sure on the second case?
Include the guild.shardId in your console.log (and if both claim it belongs to them log ShardClientUtil.shardIdForGuildId(guild.id, client.shard.count) to find out who‘s right
It goes with the greater number.
Either you‘re german like me or I didn’t get what you mean
I figured out the issue. 👋 Apparently, when you fetch for a specific server, the guild gets added on all shards. 
Huh? Why do you fetch a server? Guilds are always cached and shouldn’t be fetched
Yes, my bad on such so.
However, when we have multiple shards, and supposedly, we would want to get a specific user...
I would have to use fetch during that case, correct?
Users yes, members only on the guild‘s shard
Wait, how for members? Example.. ?
Just remember that Dm Messages will all reach shard 0
<Guild>.members.fetch() should only be done on the shard that guild belongs to
For sure.
client.users.fetch() you can do on any shard.
Just be aware that messages received by your bot in DMs will always reach shard 0 only
Indeed, for sure. Understandable.
Lastly.. you know, depending on the amount of shards, it equals to the amount of times my index.js file would run.
For example, if I have 4 shards, this code would run 4 times: https://cdn.hamoodihajjiri.com/1CH5DwGBsm .
Hello, I have a bot that is on more than 2500 servers, so I follow the instructions https://discordjs.guide/sharding/#how-does-sharding-work.
But when I want to send a message on a channel (client.channels.cache.get('1234567890'), I get an error because the server is not on the right shards (if I understand correctly). So I use the broadcastEval function but I get this error: Canot read properties of null (reading 'broadcastEval').
Apparently, having multiple shards and looping through guild cache, my particular server isn't cached, basically, not all servers are.
Yes. Only the guilds that shard handles are cached. And you shouldn’t try to access other shard‘s guilds through cache but use broadcastEval instead
Show me how you tried to use broadcastEval (and where)
How about this case?
Oh, damn.
Meaning creating and setting commands will run 4 times?
If you do that in your client file yes. But you shouldn’t do that in the first place anyway. Only deploy them when they changed, not on every restart
This is where I'm confused, how can I do that?
Suggestion for @pale sapphire:
Creating Your Bot: Registering commands - Command deployment script
read more
Oh, moment.
Lastly, in order to use such so, I need to wait for all shards to be spawned, whereas, I can just wait for them to be spawned before I can post slash commands, is that possible?
I have 4 shards, wait for all to be spawned, then set slash commands.
You can post slashcommands without spawning a single shard
Fair, fair. 👍
client.shard.broadcastEval('this.channels.cache.get("968262845601038436").send({ content: "TEST" })')
And where do you call that?
In my client.on('ready')
Also broadcastEval gets passed a function, not a string. And you need to do the sending in there, can’t get a Channel out of it (check this channel‘s pins)
If client.shard is null in your client‘s ready event then you don’t have a ShardingManager instantiating your client‘s file
Yes, I use ShardingManager in another file.
I did like in the discord.js tuto
And which file do you start?
I run my index.js file which is linked to bot.js
Sorry, had to run some errands. What’s in your index.js, what in bot.js and how are they linked?
index.js
bot.js
Okay, then you only need to change what you pass to broadcastEval to be a function, not a string. So (client) => and remove the ''
But your error of client.shard being null makes no sense if you run index.js like that…
What djs Version?
I don't see where I should do it.
"discord.js": "^13.8.1"
Sharding: BroadcastEval
read more
For an example where the function should be
Perfect, it works, thank you very much
I'm trying to get an entire shard's data using broadcastEval. When i log it to the console it works fine, if i try to use .then() to get it and send it to discord, it hates me for it.
this works:
await interaction.client.shard.broadcastEval((client) => console.log(client.ws.shards), { shard: shardId });
this doesn't:
interaction.client.shard.broadcastEval((client) => client.ws.shards, { shard: shardId })
.then((results) => {
const shards = results.map((result) => result);
console.log(shards);
})
.catch((err) => {
console.log(err);
});
It just gives the the error in the screenshot. I'm sure im just being stupid. But first day really working with shards. Usually other team members do this stuff. Any help and guidance is much appreciated.
after setting up sharding as in the example docs i get the following error
node:internal/url:1413
return decodeURIComponent(pathname);
^
URIError: URI malformed
at decodeURIComponent (<anonymous>)
at getPathFromURLPosix (node:internal/url:1413:10)
at fileURLToPath (node:internal/url:1423:50)
at finalizeResolution (node:internal/modules/esm/resolve:403:14)
at moduleResolve (node:internal/modules/esm/resolve:1009:10)
at defaultResolve (node:internal/modules/esm/resolve:1218:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
at link (node:internal/modules/esm/module_job:78:36)
/home/christopher/Code/dice-witch/node_modules/discord.js/src/sharding/Shard.js:161
reject(new Error('SHARDING_READY_DIED', this.id));
here's my bot.ts https://pastebin.com/RENuXUTU
no idea what the error message means and not sure where to start debugging 😢
the first thing i noticed is URI malformed
and in this case, pathname is the URI
right, but that's in node internal/url, so it doesn't really help
it starts at discord.js/src/sharding/Shard.js:161
i'm also curious about reject(new Error('SHARDING_READY_DIED', this.id));
is that the complete stack trace (error message)?
i was expecting to see your code some where in there.
that Shard.js file is in the discord.js module.
here's the rest of it
Error [SHARDING_READY_DIED]: Shard 0's process exited before its Client became ready.
at Shard.onDeath (/home/christopher/Code/dice-witch/node_modules/discord.js/src/sharding/Shard.js:161:16)
at Object.onceWrapper (node:events:642:26)
at Shard.emit (node:events:527:28)
at Shard._handleExit (/home/christopher/Code/dice-witch/node_modules/discord.js/src/sharding/Shard.js:407:10)
at ChildProcess.emit (node:events:527:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) {
[Symbol(code)]: 'SHARDING_READY_DIED'
}```
so we know the child process died but we don't know why, i'm sure there's a way to log it but i'm coming up blank for how to do it
i don't know what in the code you linked to is causing the "sharding" in the first place.
i see some console.logging in your code, do you see that output on the "terminal"?
are you running this locally? on your dev box? or somewhere else?
this is my index.ts
import { Shard, ShardingManager } from "discord.js";
import config from "../config.js"
const { discordToken } = config;
const manager = new ShardingManager('./build/src/bot.js', { token: discordToken });
manager.on('shardCreate', (shard: Shard) => console.log(`Launched shard ${shard.id}`));
manager.spawn();
i'm learning about sharding now.
the official docs are here: https://discordjs.guide/sharding/
my next suggestion is to delete all but necessary functionality in your bot.js app and see if the spawn manager will span the simplest of apps.
also, what node -v are you using?
16.6.0, the minimum req for djs v13
i just got your code running on my machine using node -v v18.2.0
i'm on a Mac M1, fresh install of your code dependencies.
"axios": "^0.27.2",
"discord.js": "^13.8.1",
"toad-scheduler": "^1.6.1"
},
interesting!! thanks for doing that. i just switched over to node v18.2.0, updated to those deps, deleted node_modules, reinstalled and am getting the same error
yw. man. are you just running node index.js?
broadcastEval can’t return class instances, as they get send over an IPC channel between processes as JSON. So you won’t ever get instances from it, only plain JSON (and JSONified instance objects)
i'm using typescript so in my dist folder but otherwise yes
there's sample code in this thread that sends the --trace-warnings which might log more info when running: https://github.com/discordjs/discord.js/issues/5364
So you start the shards from build/src/bot.js then what was the index.ts you showed earlier?
sorry that's my bot.ts, lol, i misspoke, let me edit the comment so i don't confuse anyone else
pastebin is the bot.ts, what i pasted above is the index.ts
does manager.spawn(); need to be await manager.spawn();?
because it returns a Promise
not sure about when we'd want to await it since i'm a noob to sharding, but i just tried and awaiting it doesn't resolve the issue 😢
did you add the trace-warnings cli argument?
execArgv: ['--trace-warnings'],
token: discordToken,
});```
doesn't seem to produce any additional output unfortunately
Only if you want to do something after it resolves. If you don’t care for the promise you don’t need to await it
For sharding move the content of your startServer function to top-level. There are some issues when the login call isn’t in top level for sharding
thanks for the tip! still doesn't resolve the issue 
Tag suggestion for @small nebula:
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)
• 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 | hasteb.in | sourceb.in | hastebin
☝️ the crash comes before i even declare the client, but if these methods were on ShardManager instead of Client that would be exactly what i need to get some insight here
i'm available to to pair on this if you still need help.
Stupid question, but could I just stringify it and then convert it back after?
Like with request payloads?
i figured it out. after a much needed break lol. stringify'd it and then parse it on return.
Hi, uhh how do i fixed this issue
My shards keep disconnecting and reconnecting making the bot irresponsive in multiple servers. can anyone help or guide me what to do?
My bot is in 12k servers 12 shard
Do you have debug logs of those dis-/reconnects already?
no shard error thrown
That wasn’t what I asked.
Tag suggestion for @visual fable:
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)
• 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 | hasteb.in | sourceb.in | hastebin
I see, I'm not listening to debug and warn events
Those are just files. You don’t need an extra file to add the debugging, just add those lines temporarily anywhere outside of any event
the logs are spammed with [Function: bound]
is it normal ?
Uh, no?
Where do you log it to?
Show the output please
Wait, show how you added the debug handler actually
since i shareded my bot, it keeps loosing its status message on the profile
const guildCountsArr = await client.shard.fetchClientValues('guilds.cache.size');
const guildCounts = guildCountsArr.reduce((previousCount, currentCount) => previousCount + currentCount, 0);
await client.user.setActivity(`${guildCounts} servers.`, { type: 'LISTENING' });
so.. should i run it in a look? and how often?
It might be enough to run it in shardReady event…
so. its not supposed to be in the shard, but in the shardmanager
It’s most probably an issue with the WebSocketShard reconnecting, not the actual Shard process
Ah. Alright
How can I check which shard has which guilds assigned to it?
Documentation suggestion for @terse cairn:
(static) ShardClientUtil.shardIdForGuildId()
Get the shard id for a given guild id.
It’s based upon guildID, bitshifted and then modulo the totalAmount of shards. Unless you have a really big bot but then discord would have contacted you and told you about it😉
Because I felt like it, this is the actual formula used by discord
const shard = Number(BigInt(guildId) >> 22n) % shardCount;```
oh
okay thanks
btw how can I listen to broadcast messages on shards?
@nimble mist
process.on('message' if it‘s child process mode
oh got it Thanks bro :)
It is a static property so this should work but is manager.shards.size is accurate?
@nimble mist Also which approach is better? I have some startup tasks for some guilds that are stored in a db.
Should I run startup tasks of guilds on each shards and check of that shard has that guild by checking the cache?
or I should check for the shard a guild should be in and then send a process broadcast to that shard and then do stuff?
I‘d do it in each guild shard and let them filter those that are on their shard (with client.guilds.cache). And for the amount I‘d use manager.totalShards rather (makes it work even if you decide to switch to cluster sharding later)
oh, so you say it's better to do it in each shard?
Oh yeah, that’s what I meant. Fixed it
Ah Thanks
I have a kinda complex question about sharding and the global rate limit. I've had issues in the past with hitting the global rate limit when running a job (which needs to post messages on every server) on multiple shards simultaneously, but I also know discord.js queues up actions to avoid rate limits, and I think the reason I hit the global rate limit is because there's no cross-shard communication on rate limits. I'm thinking of redesigning this job so it goes one shard at time to avoid the rate limit, but my question is this: can I run the job for many guilds all at once within the same shard, waiting for that shard to complete, then run the next shard, while discord.js avoid the rate limit / queue up those actions?
I‘d more rethink what you‘re doing. If you know your actions are resulting in ratelimits you shouldn’t do that so much…
where can i find sharding events docs? like all the names of events in sharding
Shard
A self-contained shard created by the [ShardingManager](<https://discord.js.org/#/docs/discord.js/stable/class/ShardingManager>). Each one has a ChildProcess that contains an instance of the bot and its [Client](<https://discord.js.org/#/docs/discord.js/stable/class/Client>). When its child process/worker exits for any reason, the shard will spawn a new one to replace it as necessary.
ShardingManager
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](<https://discord.js.org/#/docs/discord.js/stable/class/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.
Client (extends BaseClient)
The main hub for interacting with the Discord API, and the starting point for any bot.
In these files and their respective event sections 👆
And next time read #rules 6
manager.broadcastEval((c) => {
c.user?.setPresence({
status: "online",
activities: [
{
name: `Online | Shard ${c.shard?.ids}`,
type: ActivityType.Watching,
},
],
});
});
is there a way to get the shard ID from broadcast eval?
return c.shard.ids[0] in the broadcasteval
Archived to focus conversation on #986520997006032896 (our shiny forum channel) and kept read-only to facilitate in-app search (CTRL+F) for solutions to problems that have already been asked.
If your issue has not been solved yet, feel free to re-post in the new channel