#archive-sharding

12465 messages · Page 6 of 13

oblique leaf

And it never even reaches the _getChannelById function, as defined in the same file, which seems to be because the context is different.

bright merlin

I didn't use any sharding manager or else why I'm getting this then?

oblique leaf

I guess I could send a message to the sharding manager with the arguments, the sharding manager could pass them to a function, and then return the results back in another message to the shard...

winged bobcat

Uhhh im unsure about your problem

pallid depot
proud mortarBOT
oblique leaf
winged bobcat
oblique leaf

Ah, okay.

Thanks for trying to find one anyway. There probably is some way... At worst, I can just copy the code. Not pretty, but functional.

bright merlin
graceful juniper

after i did it , my bot with sharding?

or i need more stuff?

pallid depot

Ye thats basically it

graceful juniper

ok ty

trim dock

Just being a bit slow today, with the way broadcastEval works now is there a way I can pass additional arguments to the function? i.e

let func = (client, string) => { console.log(string); }

client.shard.broadcastEval(func(something here?, "Hello, world!"));

Oh I actually see broadcastEval takes a second parameter

though this doesn't seem to be explained in the docs. How do I use the context option?

oblique leaf

The second parameter has to be a JSON object. It should have a property called context, which is yet another JSON object, where you can pass all the arguments.

For example:
client.shard.broadcastEval(functionName, { context: { argument1: '1', argument2: '2' } })

And then in functionName you need to have two arguments: the first being the shard, and the second being the context object, so something like this:

//stuff goes here
})```
fierce compass

it doesn't have to be a json object. just has to be json-serializable

lean swallow

hello there, is infinite promise here is correct behavior?

if I use string, eval works as expected

i'm on node 16.6.0 & typescript 4.2.2 & djs 13.0.1

lean swallow

anyone? KannaCup

dense cape
lean swallow

with this it works fine

dense cape

Prob just a bug with Shard.eval then

lean swallow

okay, thanks for your help

wraith thicket

Hi, how does one restart a shard in v13? I used to do ts const shardid = args[1]; if (shardid === undefined) { await message.channel.send({ embeds: { color: colors.red, description: ':x: You must give a shard id!', }, }); return; } await message.channel .send(`Shard ${shardid} restarted!`) await client.shard?.send(`restart ${shardid}`); in v12 and in my manager code, I did: ```ts
shard.on('message', (message) => {
const regex = /restart (\d+)/im;
logger.log(Shard ${regex.exec(message)![1]} Restarting Regex Sent)
if (regex.test(message)) {
manager.shards.get(parseInt(regex.exec(message)![1], 10))?.respawn();
}
});

mighty lake

if your bot is over 1k servers, sharding is recommend but not required, when it gets to 2k+ sharding is a must

you could try upgrading your hardware or if sharding fixes the preformance issues

wraith thicket
mighty lake

send_code

wraith thicket

My index.ts code: ```ts
const token = beta === true ? config.token.beta : config.token.prod;

const manager = new ShardingManager('dist/src/bot.js', { token });

manager.on('shardCreate', (shard) => {
logger.log(chalk.yellow.bold('[PizzaTown]') + Launched shard ${shard.id});
shard.on('death', () => {
logger.log(chalk.yellow.bold('[PizzaTown]') + Shard ${shard.id} killed. Restarting....);
});
manager.spawn();

mighty lake

lgtm

wraith thicket

Then why is it giving the error...

sleek imp

what are all the possible manager.spawn errors?

wraith thicket

And it's not allowing me to start my bot, and after this error pops up, it shows: ```js
node:events:371
throw er; // Unhandled 'error' event
^

Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed
at new NodeError (node:internal/errors:371:5)
at process.target.send (node:internal/child_process:724:16)
at Client.<anonymous> (/home/achakra/PizzaTown/node_modules/discord.js/src/sharding/ShardClientUtil.js:38:17)
at Client.emit (node:events:406:35)
at WebSocketManager.triggerClientReady (/home/achakra/PizzaTown/node_modules/discord.js/src/client/websocket/WebSocketManager.js:378:17)
at WebSocketManager.checkShardsReady (/home/achakra/PizzaTown/node_modules/discord.js/src/client/websocket/WebSocketManager.js:361:10)
at WebSocketShard.<anonymous> (/home/achakra/PizzaTown/node_modules/discord.js/src/client/websocket/WebSocketManager.js:183:14)
at WebSocketShard.emit (node:events:394:28)
at WebSocketShard.checkReady (/home/achakra/PizzaTown/node_modules/discord.js/src/client/websocket/WebSocketShard.js:474:12)
at WebSocketShard.onPacket (/home/achakra/PizzaTown/node_modules/discord.js/src/client/websocket/WebSocketShard.js:446:16)
at WebSocketShard.onMessage (/home/achakra/PizzaTown/node_modules/discord.js/src/client/websocket/WebSocketShard.js:300:10)
at WebSocket.onMessage (/home/achakra/PizzaTown/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (node:events:394:28)
at Receiver.receiverOnMessage (/home/achakra/PizzaTown/node_modules/ws/lib/websocket.js:970:20)
at Receiver.emit (node:events:394:28)
at Receiver.dataMessage (/home/achakra/PizzaTown/node_modules/ws/lib/receiver.js:517:14)
Emitted 'error' event on process instance at:
at node:internal/child_process:728:35
at processTicksAndRejections (node:internal/process/task_queues:78:11) {
code: 'ERR_IPC_CHANNEL_CLOSED'
}

paper abyss

client.shard.fetchClientValues(...).reduce is not a function

( client.shard.fetchClientValues("guilds.cache.size")).reduce((a,b)=> a + b)

the code worked fine on djs 12v

limpid jackal
paper abyss

oh alr thanks

How do you get shard id?

terse cairn
mighty lake

please don't ping me for something like this, thanks.

rancid pier

1k servers is a lotwoah

covert patio

How do I respawn all my shards?

next plaza
proud mortarBOT
covert patio

k, ty

proud mortarBOT
covert patio

How do I see if a shard is online?

Sorry for coming back to this, is this in MB by chance? Thanks

next plaza
covert patio

k, tysm. I put % for some reason lmao

mighty lake

Does it have nodejs v16?

covert patio

That's an error that occurs before you login

covert patio

client#shard#respawn is not a function. How do I respawn correctly?

next plaza
terse cairn

at how many servers should i start sharding?

winged bobcat
mighty lake
winged bobcat

No

It also depends on you bot needs if it uses alot of resources although having non active activity they can shard at 1.5k or earlier

mighty lake

Its more to be prepared for maybe a sudden increase in guild members

I thought I read somewhere that after 2,5k guilds discord will prevent you from connecting to the api if your bot isn’t sharded

Might be more though

winged bobcat

But also not everyone has the intent

mighty lake
normal cedar

Is there any way to forcibly disconnect a shard without a resume? I have the need to do a rolling restart when I push an update. When each shard of a new instance connects, the same shard of the old instance is killed immediately.

Im migrating blargbot from eris to djs due to eris updates slowing down lately, and this is the last major feature im missing as far as I can tell. In eris it was just

shard.disconnect({ reconnect: false });
shards.remove(shard);
covert patio

Anyone having shard login issues or just me?

covert patio

Yes, before 2k but recommended past 1k servers

hasty ledge

Hey, i have a problem : (node:25) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fetchClientValues' of undefined at callback (/home/container/commands/Info/shards.js:10:22)

const Discord = require("discord.js");

module.exports = {
    commands: 'shards',
    permissions: 'ADMINISTRATOR', 
    permissionError: "**Vous n'avez pas la permission !**",
        
    callback: async (shards, config, message, args) => {
        shards.shard.fetchClientValues("guilds.cache.size")
            .then(res => {
                let nbr_serveur = 0
                let nbr_shard = 0
                for(serveur in res) {
                    nbr_shard++
                    nbr_serveur += res[serveur]
                }
                const embed = new MessageEmbed()
                .setAuthor(`Statistiques Shards`, member.user.displayAvatarURL({ dynamic: true }))
                .addField('Serveurs :', `${nbr_serveur}`)
                .addField('Shards :', `${nbr_shard}`)
                .setColor('#00FFF0')
                .setImage(member.user.avatarURL())
                message.channel.send(embed)
              })
        }
   } ```
Who can help me ?
next plaza
normal cedar
next plaza

lol

astral rampart
normal cedar

Thats not documented in the typescript definitions as far as I can see, so no. Ill see if I can add a shim for it

normal cedar
astral rampart Have you tried the <Shard>.kill() method?

Ah, #kill() is on the shard structure, im working with the WebSocketShard structure as im using my own sharding management system. Blargbot clusters multiple shards onto 1 process rather than a separate process per shard to share caches

next plaza

Kill WebsocketShard

final void

Hi, i have a little problem with shards.
Using fetchClientValues, when i calculate all servers, the count depends on the number of shards, for some reason..
When i start the bot with 6 shards, my bot its in 40 servers.
When i start it with 1 they're 35 and when i start it with 20 they become 36?
Any idea on this?

hasty ledge
jolly sapphire
terse cairn

you need to compile it

jolly sapphire

🤦‍♂️

naive grail

How to get channel by id and send embed to the channel by broadcastEval in djs v13?

Im got this error so but i'm using var for defind embed

old cave
Error [SHARDING_READY_DIED]: Shard 0's process exited before its Client became ready.
    at Shard.onDeath (/home/ubuntu/bot/node_modules/discord.js/src/sharding/Shard.js:158:16)
    at Object.onceWrapper (node:events:514:26)
    at Shard.emit (node:events:394:28)
    at Shard._handleExit (/home/ubuntu/bot/node_modules/discord.js/src/sharding/Shard.js:384:10)
    at ChildProcess.emit (node:events:394:28)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12) {
  [Symbol(code)]: 'SHARDING_READY_DIED'
}

im getting this error

lusty trellis
proud mortarBOT
proud mortarBOT
devout ginkgo

guys that sharding is built-in discord or discordjs?

old cave

still same erro

echo fulcrum

is it possible to spread shards into multiple cluster(instance)?

old cave

?shardreadytimeout
🥺

covert patio
old cave

tried this
i think i got the problem
thanks for not helping me

covert patio

Else, your shard is hitting an error before the ready event runs. Try debugging the code best on a non-sharded bot

Anyways, my issue: What is the best way to have a stats command on a sharded bot? I have everything set up, but is it worth caching the results from broadcast eval so it doesn't happen too often?

dense bough

is it possible to use variables declared outside broadcastEval inside the method?
for example

let targetShard = 0;
client.shard.broadcastEval(client => {
  if (client.shard.ids[0] === targetShard) .....
})
dense cape
dense bough
echo fulcrum

than how can I do that?

mighty lake

You need to set the same shardamount on each cluster

then assign different ids to each cluster

So for example on cluster 1 you assign shards [0,1,2,3] cluster 2 you assign [4,5,6,7] etc.

But features like broadcasteval only work on the cluster it’s casted on, so not the other clusters

im not sure what you are trying to say here

Oh you probably mean unofficial, not unsupported

vestal chasm

Hello! This is bug?
I have an error Error [SHARDING_READY_TIMEOUT]: Shard 0's Client took too long to become ready.
but i set the spawnTimeout is -1:

manager.spawn({amount: 20, delay: 5500, spawnTimeout: -1});```
meager flame

Hello, I am using discord.js sharding manager and am recently having shards duplicate. E.g. if you type a command then it repeats. This has randomly started happening and I haven't changed any code. Any ideas?

manager.on('shardCreate', shard => console.log(Launched shard ${shard.id}));
manager.spawn();

I am using pm2 to run my main process using pm2 start index.js

mighty lake

Then there are likely more instances of the sharder

vestal chasm
meager flame
tame iris

Is there a way to disable the SHARDING_IN_PROCESS error? (using broadcasteval while shards are still spawning?)
I was told this would be removed in v13 but it seems like it wasn't. Every time I update d.js I have to manually comment out the line causing this and it's kind of infuriating

dense cape
tame iris

I just have a lot of shards and I don't want a lot of functionality to be lost while waiting for them to spawn

Mainly for things like the web dashboard, etc

The fact that it can be fixed by commenting a single line just makes me feel like one of the devs thought "sure it works perfectly but i don't want people to do this". Such an unnecessary limitation tbh

dense cape

I’d host the dashboard stuff from the manager

So you can await Manager.spawn before binding the webserver to the port

@tame iris

tame iris

Again I understand that it's better to wait for all the shards but in reality I'd rather just be able to have the shard be fully functional from the moment it becomes ready. It's a bit irritating when I start up the bot and am immediately spammed with constant sharding errors

dense cape

Are you using shard id to send to a specific shard in broadcastEval?

Or do you absolutely need all online shards to respond?

echo fulcrum

why i cannot spawn shard with ts file?

heady pendant
dense cape

Bc node can’t run ts files

dense cape
vestal chasm

sorry for stupid questions, i read the changelog yesterday and found answer :d

heady pendant

oh

@vestal chasm well Ive been using -1 too and I had shards timeouts error

but nothing causing issues tho, the shard was getting ready after that

vestal chasm

other shards is not starting after error

heady pendant

ah

I don't remember what I did to avoid that

quick plinth

How can I force all shards (or specific shards) to respawn from within a shard, such as having shard 1 communicate to shard 2 to respawn

quick plinth

?

terse cairn

Shard 1’s process exited before it’s client became ready

sleek imp

When do i need to use broadcastEval?

For example if i want to clone a channel from a specific guild, do i need to do that with client.shard.broadcastEval

<@&839912195994812420>, edit: the scam got deleted

silk wharf

@vestal chasm maybe try Infinity?

vestal chasm

infinity also doens't work

next plaza
sleek imp
next plaza
next plaza
echo fulcrum

i get this error while spawning shards
Error [SHARDING_IN_PROCESS]: Shards are still being spawned.

i already set the delay to 10 sec but i still get this error

sleek imp
echo fulcrum
heady pendant
tropic wren

I keep getting ```
/Root/COOL-BOI-BOT/node_modules/discord. J's/src/util/Util. J's: 336
Given [key] = def [key];
^

TypeError: Cannot create property 'shardCount' on a string 'Q7oY7kdvoHJKpwwGYtYmP7frNrrMKp9sXeBsVEAEoVLtWRnAWcgg0FTd1soxZBCi-vr1'
At Function. mergeDefault (/root/COOL-BOI-BOT/node_modules/discord.js/src/util/Util.js:336:20)
At new BaseClient (/root/COOL-BOI-BOT/node_modules/discord. j's/src/client/BaseClient. j's: 20:25)
At new WebhookClient (/root/COOL-BOI-BOT/node_modules/discord. j's/src/client/WebhookClient. j's: 26:5)
At Object.<anonymous> (/Root/COOL-BOI-BOT/index. j'shttps://cdn.discordapp.com/emojis/817012751054274581.png?size=3214)
At Module. _compile (node: internal/modules/cjs/loader: 1101:14)
At Object. Module. _extensions.. j's (node: internal/modules/cjs/loader: 1153:10)
At Module. Load (node: internal/modules/cjs/loader: 981:32)
At Function. Module. _load (node: internal/modules/cjs/loader: 822:12)
At Function. ExecuteUserEntryPoint [as renaming] (node: internal/modules/run_main: 79:12)
At node: internal/main/run_main_module: 17:47

dense cape

Not sure which line bc ur error looks weird

tropic wren

Thx for responding.

surreal tulip

can I ask why .jss are . J's?

stiff mauve

Anyone know how to fix shards loading too slowly?

hasty patrol
south geode

VIRUS_SadCat

serene robin

does sharding effect the way the events fire or commands work?

echo fulcrum

no

serene robin

then is there a down side of sharding?

naive grail

Hey, How can i make internal sharding in djs v13? Same as v12? My bot it 2000 guild

next plaza
naive grail

I have seen the document already it just run on 1 process so i need to edit my code back to normal i think i just wanna try hybrid-sharding

loud walrus

How can I check if all shards are launched before making a fetchClientValues in my ready event ?

loud walrus

Ping me

robust gull

Is sharding hard to do ?

lament vessel
hasty ledge

Hey, i have this problem when i execute my shards stats command, in console when i launch the bot i see : Shard on : 0
So it's good, but the command shards doesn't work 😦

hasty ledge
lost saddle Code?
const Discord = require("discord.js");
const { MessageEmbed } = require('discord.js')

module.exports = class ShardsCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'shards',
      memberName: 'shards',
      group: 'music',
      guildOnly: true,
      description:
        "Shards"
    });
  }

  async run(message, args, onsenfou, client) {
    if(message.author.id !== '870370835066654753') return;
        
    client.shard.fetchClientValues("guilds.cache.size")
        .then(res => {
            let nbr_serveur = 0
            let nbr_shard = 0
            for(serveur in res) {
                nbr_shard++
                nbr_serveur += res[serveur]
            }
            const embed = new MessageEmbed()
            .setAuthor(`Sanction`, client.user.displayAvatarURL({ dynamic: true }))
            .setTitle(`Statistiques Shards`)
            .addField('Serveurs :', `${nbr_serveur}`)
            .addField('Shards :', `${nbr_shard}`)
            .setColor('#00FFF0')
            .setImage(client.user.avatarURL())
            message.channel.send(embed)
           })
        }
    }```
lost saddle

"client" is null

hasty ledge
lost saddle

Can I see?

hasty ledge
hasty ledge
lost saddle This part

Oh no i'm a noob 😭
It's not the same bot so i don't put client on args 4 in my music bot i just have this

  commandPrefix: prefix,
  owner: discord_owner_id
});

client.registry
  .registerDefaultTypes()
  .registerGroups([
    ['music', ':notes: Commandes Musiques :'],
  ])
  .registerCommandsIn(path.join(__dirname, 'commands'));```
hasty ledge
lost saddle
hasty ledge
lost saddle

So does it work now?

hasty ledge
lost saddle

Can you send your code in a bin?

hasty ledge
wintry mulch

what is sharding

lost saddle
lost saddle
hasty ledge
carmine stone

A specific shards heartbeat can be found on the WebSocketShard instance, accessible at <client>.ws.shards > .ping.
Can anyone show me an example of this?

winged bobcat
hasty ledge
raw lava

Is there a way to get the amount of errors of a specific shard? With client.shards.broadcastEval()?

coarse lagoon

I have this code:

const { ShardingManager } = require("discord.js")
import { token } from "./environment_variables/variables.json"
import * as functions from "./functions"

const manager = new ShardingManager("./bot.js", {token : token})

manager.on("shardCreate", async shard => {
    await functions.processShardCreate(shard)
})

manager.spawn()``` 
Error:
```This session would have handled too many guilds - Sharding is required.
scenic aspen

just make sure you run the right file okay

coarse lagoon

@scenic aspen

scenic aspen

hmm try to force spawn the exact amount of shard? instead of auto

coarse lagoon

My bot is in 3,800 guilds, how many should I do?

4?

scenic aspen
coarse lagoon

Ok

scenic aspen

no idea then

coarse lagoon

Let me console.log it and check

scenic aspen

it should be the different error...

and its alr login the first shard so it cant be

coarse lagoon

Thats very weird

It creates a shard, yet thinks I'm not sharding?

coarse lagoon

Anyone else have any clue?

dense cape

Too many guilds

coarse lagoon
[WS => Manager] Fetched Gateway Information
    URL: wss://gateway.discord.gg
    Recommended Shards: 4
[WS => Manager] Session Limit Information
    Total: 1000
    Remaining: 200
[WS => Manager] Spawning shards: 3
[WS => Shard 3] [CONNECT]
    Gateway    : wss://gateway.discord.gg/
    Version    : 9
    Encoding   : json
    Compression: none
[WS => Shard 3] Setting a HELLO timeout for 20s.
[WS => Shard 3] [CONNECTED] Took 204ms
[WS => Shard 3] Clearing the HELLO timeout.
[WS => Shard 3] Setting a heartbeat interval for 41250ms.
[WS => Shard 3] [IDENTIFY] Shard 3/4 with intents: 515
[WS => Shard 2] An open connection was found, attempting an immediate identify.
[WS => Shard 2] [IDENTIFY] Shard 2/4 with intents: 515
[WS => Shard 3] [READY] Session 10764c132218cada0845a77a5653e4ab.
[WS => Shard 3] [ReadyHeartbeat] Sending a heartbeat.
[WS => Shard 2] [INVALID SESSION] Resumable: false.
[WS => Shard 2] Failed to connect to the gateway, requeueing...
[WS => Manager] Shard Queue Size: 1; continuing in 5 seconds...
[WS => Shard 3] Heartbeat acknowledged, latency of 2567ms.
[WS => Shard 0] Shard did not receive any more guild packets in 15 seconds.
   Unavailable guild count: 1
Logged in!
Hit a 429 while executing a request.
    Global  : false
    Method  : put
    Path    : /applications/657776310491545620/commands
    Route   : /applications/:id/commands
    Limit   : 2
    Timeout : 17772ms
    Sublimit: None
[WS => Shard 2] An open connection was found, attempting an immediate identify.
[WS => Shard 2] [IDENTIFY] Shard 2/4 with intents: 515

The bot goes online, but its in a loop of the bot disconnecting and reconnecting with shard errors?

dense cape

You hit the rate limit for setting global commands

Also doesn’t seem like you enabled the guilds intent

coarse lagoon

Trust me, they are on

dense cape

Hard to believe that when the logs says it’s not receiving guild create/unavailable events

coarse lagoon

You mean Guild members, right?

dense cape

No

The GUILDS intent

coarse lagoon

Oh

dense cape

That’s better

coarse lagoon

Thats how it was lol

dense cape

Shard 0 has an unavailable guild

I guess there’s more that are available that that if there’s 4 shards

coarse lagoon

You're saying it should be 5?

dense cape

No

coarse lagoon

Ok

dense cape

I’m not sure why Discord is sending invalid sessions upon first connect tho

coarse lagoon

Maybe Discord hates my bot

So yeah it just goes on and on until I stop node

coarse lagoon
dense cape

Are you calling it on ready?

coarse lagoon

Yes

dense cape

That’s prob why

coarse lagoon

Oh, ready is being called multiple times?

dense cape

Per shard

You only need to do it when you actually update a slash command

The guide has a script that does this and it doesn’t require the bot to login

coarse lagoon

I want to actually get the bot working before I mess with slash commands though

dense cape

I don’t think ur the only one having issues with reconnections bc of invalid session tho

Not sure if it’s a Discord issue or d.js

coarse lagoon

Hmm is there any "workaround"?

dense cape

Not rn

coarse lagoon

Can you link me to the issue? (If its on GitHub or something)

dense cape

You’ll prob be asked to provide a short reproducible code sample

coarse lagoon

I'll just go back to shards : auto until its fixed

polar idol

(node:866) UnhandledPromiseRejectionWarning: Error [SHARDING_READY_DIED]: Shard 1's process exited before its Client became ready.
I am Getting this error once i run my sharding code

coarse lagoon

Welcome to the club

paper abyss

?shard ready timeout

Nvm

winged bobcat
polar idol ```(node:866) UnhandledPromiseRejectionWarning: Error [SHARDING_READY_DIED]: Sha...

Got this error: Error [SHARDING_READY_TIMEOUT]: Shard 0's Client took too long to become ready.?
The spawnTimeout can be set to -1 or Infinity to prevent future errors:
https://discord.js.org/#/docs/main/stable/class/ShardingManager?scrollTo=spawn. This can also happen if you call ShardingManager#spawn before attaching a listener to the shardCreate event which could create a race condition possibly preventing the shard 0 to perform a successful launch.

echo fulcrum

how many ram is suggested for 10 shard which has 1000 guilds for each?

graceful juniper

Is it possible to make a "shard" that I use
quick.db ?

heady pendant

can we respawn shards individually using internal sharding?
I see no respawn function on websocketshard

next plaza
heady pendant

ah, it doesn't show as a method on the docs

next plaza

All event codes with 4xxx mostly doesnt emit a respawn of the WS Shards. Destroying with 1000 should recreate the ws shard again.

heady pendant

nvm its just a private one

next plaza
heady pendant

ok thankies

proud mortarBOT
terse cairn

please i want to know what's sharding do

proud mortarBOT

• Necessary at 2,500 guilds (discord will not let your bot connect)
• Internal sharding (recommended):

const client = new Discord.Client({ shards: 'auto' }); // auto shards
const client = new Discord.Client({ shards: [0, 1] }); // specific shards

• Sharding manager: learn more

next plaza

@terse cairn here you go^

zinc plank

is anyway to setup a redundancy client for discord.js??

somber comet

What conditions require me to create a new shard?
Could More Than 70% CPU Usage Be The Reason I Did That? Or Recommended When The Bot Has 1000 Servers Each Shard?, I'm Confused Now

next plaza
somber comet
terse cairn

Suddenly I can't get my shards to boot. Didn't change anything and now it doesn't work anymore

cedar cliff

rip dbot then 😄

coarse lagoon

I've once again returned for help with sharding.

[WS => Manager] Fetched Gateway Information
    URL: wss://gateway.discord.gg
    Recommended Shards: 4
[WS => Manager] Session Limit Information
    Total: 1000
    Remaining: 990
[WS => Manager] Spawning shards: 3
[WS => Shard 3] [CONNECT]
    Gateway    : wss://gateway.discord.gg/
    Version    : 9
    Encoding   : json
    Compression: none
[WS => Shard 3] Setting a HELLO timeout for 20s.
[WS => Shard 3] [CONNECTED] Took 225ms
[WS => Shard 3] Clearing the HELLO timeout.
[WS => Shard 3] Setting a heartbeat interval for 41250ms.
[WS => Shard 3] [IDENTIFY] Shard 3/4 with intents: 515
[WS => Shard 2] An open connection was found, attempting an immediate identify.
[WS => Shard 2] [IDENTIFY] Shard 2/4 with intents: 515
[WS => Shard 3] [READY] Session 3f25ba1c382ed2b1563de799229f9f9f.
[WS => Shard 3] [ReadyHeartbeat] Sending a heartbeat.
[WS => Shard 2] [INVALID SESSION] Resumable: false.
[WS => Shard 2] Failed to connect to the gateway, requeueing...
[WS => Manager] Shard Queue Size: 1; continuing in 5 seconds...
[WS => Shard 3] Heartbeat acknowledged, latency of 2334ms.

The session keeps getting invalidated and the bot reconnects over and over again

Also this:

   Unavailable guild count: 1
coarse lagoon

Any help would be appreciated, please

sturdy lynx

@coarse lagoon what intents do you have

coarse lagoon
hasty ledge
dense cape

It’s client.shard.ids[0]

And .cache.size

@hasty ledge

hasty ledge
dense cape

Change the eval code accordingly

hasty ledge
dense cape Change the eval code accordingly

Now when i execute cmd i have this error in console : ```
(node:14255) UnhandledPromiseRejectionWarning: ReferenceError: empty is not defined
at AboutCommand.onError (/home/akamusic/node_modules/discord.js-commando/src/commands/base.js:361:47)

dense cape

That’s an issue with commando

Or maybe you in ur about command?

hasty ledge
dense cape

Tried to use empty, but it isn’t declared nor defined

dense cape

Idk what you changed

hasty ledge

Basically in all my orders I use empty after commando
When I check my commands there is no argument that could be related to "empty".
Everything works except my status command

tough idol

does discord.js take in account of the sharding max concurrency?

next plaza
tough idol

ah gotcha, i was asking because i've implemented this in my discord.js clustering library, making sure there's no conflicts

next plaza
tough idol

workerdata?

my library doesn't use worker threads

it only uses cluster/child_process

next plaza
tough idol

there's no workerdata there

coarse lagoon
    Event Code: 1000
    Clean     : true
    Reason    : 
[WS => Shard 3] Clearing the heartbeat interval.
[WS => Shard 3] WS State: CLOSED
[WS => Shard 3] [CONNECT]
    Gateway    : wss://gateway.discord.gg/
    Version    : 9
    Encoding   : json
    Compression: none
[WS => Shard 3] Setting a HELLO timeout for 20s.
[WS => Shard 3] [CONNECTED] Took 195ms
[WS => Shard 3] Clearing the HELLO timeout.
[WS => Shard 3] Setting a heartbeat interval for 41250ms.
[WS => Shard 3] [IDENTIFY] Shard 3/4 with intents: 515
[WS => Shard 3] [READY] Session 15a7152286a1cf86ef225925dc7b29ca.
[WS => Shard 3] [ReadyHeartbeat] Sending a heartbeat.

Or an internet problem?

next plaza
stoic remnant
  1. you seem to be using discord.js-light, a mod and outdated library of discord.js, use the actual one. 2. What's your code? 3. Basing on the error, I don't think thats a WebhookClient. It's a Client, I assume, and since the -light library modifies the internal structures, then I assume it doesn't know what "data.url" is
next plaza
stoic remnant

there we go ^

next plaza
dense cape

Ur error actually reminds me of an old issue pre v13 used to have tho

exotic sapphireBOT
dense cape
haughty vector

i get this when using the example from the guide where you replace index with bot and require it

plain tangle

Is there any benefits to sharding a small bot?

urban lintel

is there a way to know what crashed a shard?

is there a benefit in using worker threads

next plaza
urban lintel is there a benefit in using worker threads

Node supports two types of Process Splitting.
Both have in comparisons some differences:
Worker_Threads:

  • uses shared ram
  • Good for CPU intensive Processes
  • canva rendering doesnt work mostly
  • faster ipc
  • sql databases have some problems
  • uses a special cloning algorithm, getting Objects on the Broadcasteval is just possible, when you stringify the result.

Process:

  • takes a little bit longer to start
  • uses a little more process ram
  • used as default
next plaza
urban lintel
next plaza
urban lintel

alr

what does Shard#error emit? client errors or like api errors?

meager flame

hello, I am getting an error "Shard's client took too long to become ready", even though the spawnTimeout is -1 here. Any ideas how I fix this? I am currently unable to spawn all my shards because of this

next plaza
lost saddle
meager flame

manager.spawn(this.totalShards, 5500, -1).catch(e => console.log(e));

@next plaza@lost saddle so something like this?

meager flame
lost saddle

I hope this helps you out!

meager flame

thank you very much!

next plaza
meager flame
next plaza

Yes, manager.totalShards

urban lintel

what does Shard#error event emit?

next plaza

Node v16.7 introduced Pointer Compression from the v8 engine.
We could hope on 30% less heap ram usage on 64bits system.

topaz cloud

Someone???!!!
People who asked after got their answers and im still waiting from 17 hours
Nvm ill find a fix for it
stackoverflow and other sites exists 😑

next plaza
topaz cloud

Shard's Client Took Too Long To Become Ready
same code from discordjs.guide

echo fulcrum

how can i restart specific shard / all shards in shardingManager?

proud mortarBOT

Documentation suggestion for @echo fulcrum:
_ Shard#respawn()
Kills and restarts the shard's process/worker.

astral rampart

I'm randomly receiving a 1000 close code on websocket which makes the bot shard just die silently without emitting any errors or trying to reconnect to the gateway.
Did anyone else experience this issue and is there a way to handle it?

I think it's an issue on Discord's side because it probably missed a heartbeat after having a sudden API latency spike on some of their servers. (The latency was logged and my other shards on the same machine didn't face any issues so it's not my internet connection)

My question is how do I resume without having to restart the shard process?

next plaza

You could listen to the shardDisconnect event

astral rampart

Nope it doesn't, the process remains running but the bot is fully disconnected from Discord.

astral rampart

But the problem is not listening to the disconnect event, it's how do I reconnect and resume the session?

next plaza
astral rampart

Yes I'm on the latest djs version if that's what u mean.
But I think I found where my real issue is, I tried to simulate a missed heartbeat and it reconnected automatically after being closed. But any active voice connection on my Lavalink node gets terminated after few seconds because the gateway session associated with it was closed..

icy crowBOT

ok

next plaza

Every shards as an attached even listener on the shard. shard.on('message', ...) and process.on('message'` for your bot.js file

next plaza

When you use Childprocesss, yes
On WorkerThreads, you have to listen on the parentPort

dark cape

when i use Sharding Manager, the bot very slow and won't respond to messages. But when I use Internal Sharding, the bot working fine. Any problem?

dark cape

all of it started but unstable and will restart itself about 5-10 mins later

but the response time slow too sad

terse cairn

How to send a message to another shard

cobalt fulcrum

I'm sorry. I have a question.
I want to use require('express'); with new ShardingManager().
No matter how many times I searched on Google, I could not find the answer.

There are many examples of using Discord.Client(), but I couldn't find any example using new ShardingManager().

Can you give me some advice?

haughty folio

Does sharing support import/export syntax?

terse cairn

Error [SHARDING_READY_DIED]: Shard 0's process exited before its Client became ready.

tardy granite
idle nebula

How would I avoid this error Error [SHARDING_IN_PROCESS]: Shards are still being spawned. when I use client.shard.fetchClientValues when a client is ready?

dark cape

how can I know when internal sharding spawning complete

idle nebula
dark cape
manager.spawn().then(... ur code here)

the spawn() function will resolve when all shard spawning done like

idle nebula

Ok, much appreciated.

long sail

how to use shards?

abstract thistle

hey guys any help

idk 🤷‍♀️ everything is right

abstract thistle
winged tiger

There are some weird things occurred to me while I'm trying to shard my bot today. When I terminate my process with ^C in the console and everything looks stop, it can still receive and response the command, even log things in console. This usually follow with error ERR_IPC_CHANNEL_CLOSED and I didn't find out what's that.
I quit the terminal but it's still there. Everything become fine after I restart my computer.

My code is the same with the guide.

const { ShardingManager } = require('discord.js');
const { bot: {token} } = require('./config.json');

const manager = new ShardingManager('./app.js', { token: token });

manager.on('shardCreate', shard => console.log(`Shard ${shard.id} is start working...`));

manager.spawn();

So basically I'm confused how can I kill all shards properly.

meager flame

how much RAM (very rough estimate) should each spawned shard use? I feel like my bot is using more RAM than it should be.

winged tiger
winged bobcat
proud mortarBOT

_ Shard#kill()
Immediately kills the shard's process/worker and does not restart it.

winged tiger
winged bobcat
winged bobcat
frigid tiger

How can I sharding across hosts? Any ideas/advices/guides? I can self-taught but I need a starting point

fickle mural
frigid tiger

Thanks

terse cairn

can someone help me with sharding , im getting confused as there are 2 types of sharding in there , my bot is in 2.2 servers

terse cairn

Hi, I'm someone who's in shard to know

meager flame

Hello, I currently run a VPS to host my Discord.js bot. I've noticed that due to the nature of JS, not all CPU cores are used when I'm sharding on a single process. If I were to use Clusters to shard my bot, will this effectively make use of all my virtual cores?

fallen herald

Hey! Pls, can someone help me, i'm trying to restart my bot but says Shard 0's Client took too long to become ready., i'm waiting for 6hours and nothing

I have 66 shards

elder lynx

How would I go with my database with sharding?

Should I make a separate process using a database in the backend and queue-ing each request it recieves from each shard?

vocal isle

Execute order 66...

elder lynx

what do you mean with that

vocal isle

66 shards... Execute order 66... Star wars?

elder lynx

well

crimson pond
elder lynx

Like, i can do it in a day - i just lack motivation often

crimson pond

Good luck I guess

fickle mural
elder lynx

which is a file...

fickle mural

sqlite is good for non-sharded bots, you'd be best migrating to a proper database if you're needing sharding

elder lynx

well, anything as performant as SQLite?

fickle mural

sqlite is lightweight but with large amounts of data it's very inefficient
any database will work just as well

elder lynx

Hmm

fickle mural

you hosting on a vps or using one of the panels that sell you database access?

elder lynx

Vps of course

fickle mural

how much data are you storing, just guild configs, etc? also whats your guild/shard count

elder lynx

guild count is 600 ish

i use 6 different SQLite files to keep filesize low, each for each category - economy, guildconfigs, prefixes, specific cooldowns etc

fickle mural

😳

you don't need to shard until like 2000 servers but i'd start moving things over now whilst it's still small
i'll dm you, since this isn't the place for this talk anyways

elder lynx

sure

abstract elm

How can I check if a specific shard is online?

@fickle mural any chance you know?

abstract elm

@tough idol are you able to help me out on this as you gave me this first, it doesn't actually say Shard X is offline when it is?

tough idol
abstract elm

I'm still currently using v12 until i get the chance to move to v13, Shard 14 (the shard which is offline), the ping shows null = nothing but it says it's online, that's what i'm trying to explain

tough idol

oh sorry, true should be a string, edited

abstract elm

Still saying it's online when it is not

tough idol

works for me

dense cape
abstract elm

?

dense cape

broadcastEval returns a Promise

Since Promises are objects, it’ll always return true

abstract elm

I see.

tough idol

oh yeah, you gotta await the promise

didn't catch that, thanks kinect

abstract elm

I already await it

dense cape

The second param of broadcastEval is an object, not a number

abstract elm

doing {shard: 14} doesn't respond at all, bare in mind i'm still using v12 currently

dense cape

Oh, then it is a number

Non-empty strings are truthy btw

So returning the error message will always be true

abstract elm

Remove .catch(...)?

dense cape

No, it’ll become an unhandled rejection

Make the catch return a falsy value

abstract elm

still returns it's online

@dense cape

dense cape

Do you have 15 shards?

abstract elm

I have 20

terse cairn

when i restart bot with ctrl c and yarn start

the old shards are still on even if i dont start

how do i turn off bot properly

winged bobcat
terse cairn

like this?

edit : manager.shards

winged bobcat

Yes

terse cairn

yea worked
ty

covert vortex

Why ?

I have to stop the bot for 45 minutes to avoid the error and this is a minimum of 5 times a day.

abstract elm

do you have setTimeout: -1 or setTimeout: 0

@covert vortex

terse cairn

the emojis from my server if they are in shard 0 will they work in shard 1

covert vortex
abstract elm

in your shardingmanager

covert vortex
abstract elm

An example of this would be:js const manager = new ShardingManager('./index.js', { totalShards: 'auto', spawnTimeout: -1, token: yourtoken})

covert vortex

okay

muted ore

hello guys i hope you doing well i have question what Sharding do?

proud mortarBOT
abstract elm

@strange echo

@valid gale

strange echo

the user has been banned

no idea why the message is still there

pearl cove

(delete it?)

abstract elm

That'd be helpful ^

@pearl cove I don't suppose you know anything about shard broadcasteval?

pearl cove

What about broadcast eval exactly?

abstract elm
abstract elm

I've been trying to get it to show the status of a shard, but when Shard X is offline it automatically returns 'true'

abstract elm

@pearl cove ^

errant ingot

can anyone explain to me what is sharding

elder lynx
wraith thicket

Hi! I've been trying to make a shard restart command for my bot. How can I make the bot wait until the shard is actually dead before restarting? Here's the code: ```ts
shard.on('message', async (message) => {
const regex = /restart_shard:(\d+)/

if (regex.test(message)) {
  const shardid = regex.exec(message)![1];
  if (manager.shards.get(Number(shardid)) === undefined) {
    logger.log(chalk.yellow.bold('[PizzaTown]') + ` Message ignored as Shard ${shardid} does not exist....`);
    return;
  }
  logger.log(chalk.yellow.bold('[PizzaTown]') + ` Shard ${shardid} will be restarting soon....`);
  manager.shards.get(Number(shardid))?.kill();
  logger.log(chalk.yellow.bold('[PizzaTown]') + ` Shard ${shardid} has been killed....`)
  return;
}

})
shard.on('death', () => {
logger.log(chalk.yellow.bold('[PizzaTown]') + Shard ${shard.id} killed. Restarting....);
shard.respawn();
});

elder lynx

kill the shard then respawn from sharding manager or something like that?

queen surge

Does each shard need to register slash commands or do I need to move them into the main file?

lilac narwhal

commands are per app so registering them should be unrelated to sharding

young totem
glass quarry
young totem
glass quarry

I don't know the relevant code or reproducible code for this nor am I familiar that much with sharding, so I'm not a good person to ask for this >:

young totem

👍 I’ll wait to see if anyone else might know more then, ty

So after further investigation I think it might be due to a shard going down. I did a rolling restart (restart each shard one at a time) and it happened at some point during that. Not positive about that, just a theory

terse cairn

How to fix this?

young totem
fervent dragon
young totem
dull musk

My bot is in 2258 servers. Should I start the sharding process?

lilac narwhal

yes

dull musk
dull musk

When I do my bot's stats command the console output showed like this.

[2260, 2260]

And the bot started to run on 2 instances. Like when I do a command it sends 2 messages instead on 1.
Sharding code

const { ShardingManager } = require('discord.js');
const config = require('./botconfig/config.json')

const manager = new ShardingManager('./bot.js', { token: config.token });

manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`));

manager.spawn()
 .then(shards => {
        shards.forEach(shard => {
            shard.on('message', message => {
                console.log(`Shard[${shard.id}] : ${message._eval} : ${message._result}`);
            });
        });
    }).catch(console.error);
abstract saddle

When y try to eval this code, it says channelId is not defined

why ?

terse cairn
abstract saddle

Thonk

but i can use the args ? ex: channelId = args[0] in the broadcast

terse cairn

All your function variables need to be inside the broadcast eval afaik

abstract saddle

ahh ok i see, ty

so i can't use externals functions in boadcast like that ?

terse cairn

I'm unsure of that probably wait for an expert sharder to come 😁

lost saddle
stuck tapir

when we shard the bot, when we print client.users.cache in a channel, will that contain the users in that shard only?

next plaza
terse cairn
const config = require('./config.json')

const manager = new ShardingManager('./index.js', { token: config.settings.token, totalShards: 5});

manager.on("shardCreate", (_) => { 0 })

manager.spawn().catch((_) => 0)```

`Error [SHARDING_REQUIRED]: This session would have handled too many guilds - Sharding is required.`
young totem
terse cairn

How to stop that the bot disconnects if a heartbeatping fails ( I want to raise the limit to like 5)

astral rampart
terse cairn

that over 8 differejnt os, tested on windows, windows xp, rasperian, debian, ubuntu, fudora, etc.

i made a issue request if u want to see my images: https://github.com/discordjs/discord.js/issues/6552

abstract elm
terse cairn
terse cairn
young totem
abstract elm
dense cape

Then the process is up

It doesn’t check if the ws is actually connected

proud mortarBOT

_ Shard (extends EventEmitter)
A self-contained shard created by the ShardingManager. Each one has a ChildProcess that contains an instance of the bot and its Client. When its child process/worker exits for any reason, the shard will spawn a new one to replace it as necessary.

thorn rampart

Hello, I have added sharding to my bot and literally nothing works now

I am trying to make a simple eval command but I don't get the message logged in console

here are my intents, what am I doing wrong? please help

last creek
const { ShardingManager } = require("discord.js");
const manager = new ShardingManager('./bot.js', {token: config.token});
const Discord = require("discord.js");

const client = new Discord.Client({
  intents: Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES
})

manager.on('shardCreate', shard => {
  shard.on("death", () => {
    const channel = client.channels.cache.get("channel-id")
    channel.send({content: `The shard: ${shard.id} death`})
  })
})

I have a question I would like to send a message to a channel if the shard death what can I do there I tried client.channels.cache.get () but that doesn't work :/

last creek
shell wolf the error?
channel.send({content: `The shard: ${shard.id} ready`})
                ^

TypeError: Cannot read property 'send' of undefined
shell wolf

the channel id is invalid

last creek

no its right

shell wolf

before send, console.log(channel)

last creek
shell wolf
last creek

But I entered the channel id correctly

shell wolf
last creek
shell wolf
last creek is :D

try: js manager.on('shardCreate', shard => { shard.on("death", async () => { const channel = await client.channels.cache.get("channel-id") channel.send({content: `The shard: ${shard.id} death`}) }) })

shell wolf
last creek
next plaza

@last creek your sharding code is wrong
You need 2 files

You could use a webhook for it

next plaza
proud mortarBOT

• Necessary at 2,500 guilds (discord will not let your bot connect)
• Internal sharding (recommended):

const client = new Discord.Client({ shards: 'auto' }); // auto shards
const client = new Discord.Client({ shards: [0, 1] }); // specific shards

• Sharding manager: learn more

next plaza

Check the last link

terse cairn

what is better

shareding manager or internal sharding

Apart from ShardingManager, discord.js also supports a sharding mode known as Internal sharding. Internal sharding creates multiple websocket connections from the same process, and does not require major code changes. To enable it, simply pass shards: 'auto' as ClientOptions to the Client constructor. However, internal sharding is not ideal for bigger bots due to high memory usage of the single main process and will not be further discussed in this guide.

last creek
terse cairn

Not necessarily

next plaza
terse cairn

Well i am using a custom worker which does that for me

So i dont need the sharding Manager at all

next plaza
terse cairn

No

next plaza

you could combine Internal Sharding and the Sharding Manager with your custom solution

terse cairn

Yes i am using internal sharding

last creek

Is it normal that if an error occurs in the bot that automatically restarts the bot?

last creek
let values = await client.shard.broadcastEval(`

            [this.shard.ids[0],

            this.guilds.cache.size

            this.uptime,

            this.ws.ping]`);

It's give me this error:

reject(new TypeError('SHARDING_INVALID_EVAL_BROADCAST'));
               ^

TypeError: SHARDING_INVALID_EVAL_BROADCAST
last creek
young totem

waitwhat

last creek
young totem

which line of what

last creek

the error is create

young totem

It doesn’t say. All the info I know is there

young totem
last creek

oh okay i look

Have you tried a try catch?

thorn rampart

Hello, I have added sharding to my bot and literally nothing works now
I am trying to make a simple eval command but I don't get the message logged in console

here are my intents, what am I doing wrong? please help

next plaza
last creek
terse cairn

how can i ignore unavaible guilds

astral rampart

just ignore them?

terse cairn

no, the bot doesnt start because of this problem

and it says no websocket avaible

lilac narwhal

just check <guild>.available before doing anything with them

terse cairn

allright i will try thx

Hey, how can I get a shard's ping? client.ws.shards.ping is undefined.

dark cape

how can i put number in broadcastEval function ?

// this one v12 still works
// defined laysodep function above
const guildCount = await getGuildCount(manager);
        const membersCount = await getMembersCount(manager);
        console.log(`${guildCount} guilds and ${membersCount} members!`);
        console.log(`Set presence: ${guildCount} guilds and ${membersCount} members!`);
        manager.broadcastEval(client => client.user.setPresence({ status: "online", activities: [{ name: `${laysodep(guildCount)} servers | ${laysodep(membersCount)} members` }] }));

in v13 it will said laysodep is undef error (already defined above)

young totem
last creek Have you tried a try catch?

Sorry, just saw this.

I have no idea where the error could be coming from or even what method could be causing it. I don't even know if it is a method, maybe it's caused by some internal task?

Any code that deals with broadcastEval or fetchClientValues throughout all of my code has catching, both with the block (try { ... } catch (e) { ... }) and with promises (.catch((e) => ...)) for good measure.

last creek
young totem

Possibly a bug then? Seems like nobody knows what could cause it.

queen surge

Can the bot shut down the sharding manager? E.G. if I want to completely shut down a test version of my bot

I tried to process.exit() but that only kills the shard

young totem
queen surge

Yeah I know, it's responding to an admin command
I want to bubble it up to the manager from the shard

young totem

Yeah so it's firing on the shard

queen surge

Yeah that's my problem

surreal tulip

how can I get list of shards in internal sharding

edit: client.ws.shards

wary raft

At about what server count is it recommended that I start working on the code to shard my bot?

wary raft

I thought that was when you literally have to shard. I want to know when a good time is to start sharding my bot.

Or can you not shard your bot if your server count is below 2500, I'm not really sure shrug

jade lily

typically its 1000 guilds per shard

wary raft

Ah, I see

jade lily

you can totally shard before 2000 guilds if you want

wary raft

I mean, my bot is at 380 servers right now but I'm just looking into the future since it's growing crazily fast.

jade lily

gonna be real, im a d.py refugee.. our bot has 11 shards each with 1000.. weve been using autosharding since about 2500-3000 guilds

so im not totally sure if d.js shard recommendations are the same, but i assume they are ThinkO_O

wary raft

Alrighty, I'll look into it some more. But thank you shibaheart

jade lily

👍

you can always implement autosharding and have it grow with your server use for future proofing

shrug

wary raft

Hmm, alright

dull musk

Alr so my bot is in 2466 servers and should I shard it now or should I wait for the bot to hit 2500 servers?

Because I tried to shard the bot at 2225 servers and it caused a lot of problems like server count for each shard showed as [2225, 2225]

dense cape

There's def smth wrong if the count is equal on both shards

dull musk
const { ShardingManager } = require('discord.js');
const config = require('./botconfig/config.json')
 
const manager = new ShardingManager('./bot.js', { 
    token: config.token
});

manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`));

manager.spawn()
dense cape

Can you show the eval?

dull musk
const {
  MessageEmbed,
  splitMessage
} = require(`discord.js`);
const config = require(`../../botconfig/config.json`);
const ee = require(`../../botconfig/embed.json`);
const emoji = require(`../../botconfig/emojis.json`);
const {
  inspect
} = require(`util`);
module.exports = {
  name: `eval`,
  category: `Owner`,
  aliases: [`evaluate`],
  description: `eval Command`,
  usage: `eval <CODE>`,
  run: async (client, message, args, guildData, player, prefix) => {
    if (!config.ownerIDS.includes(message.author.id)) {
      const nope = new MessageEmbed()
      .setColor(ee.wrongcolor)
      .setDescription(`${emoji.msg.ERROR} You are not allowed to run this command! Only the Owner is allowed to run this command`)
      return message.channel.send({embeds: [nope]})
    }
    if (!args[0]) {
      let idk = new MessageEmbed()
      .setColor(ee.wrongcolor)
      .setDescription(`${emoji.msg.ERROR} You have to at least include one evaluation arguments`);
      return message.channel.send({embeds: [idk]})
    }
    let evaled;
    try {
      if (args.join(` `).includes(`token`)) return  message.reply("Abbe sale!");

      evaled = await eval(args.join(` `));
      let string = inspect(evaled);
      if (string.includes(client.token)) return message.reply("Abbe sale!");
    } catch (e) {
      console.log(String(e.stack).bgRed)
      const emesdf = new MessageEmbed()
      .setColor(ee.wrongcolor)
      .setAuthor(`An Error Occurred`)
      .setDescription(`\`\`\`${e.message}\`\`\``);
      return message.channel.send({embeds: [emesdf]});
    }
  },
};
dull musk
dense cape

I mean the broadcast eval

Huh?

Like it was responding twice to stuff?

dull musk
dull musk

It only runs on 2 instances when I shard

dense cape

How did you get [2225, 2225]?

dull musk

I used this client.shard.fetchClientValues('guilds.cache.size').then(console.log);

dense cape

Ur on v12?

Or does v13 still do string for that method?

dull musk

I still didn't updated to v13, I'm now using a beta version before v13

dense cape

Why not use the latest?

dull musk

I have to update a lot of stuff. Do you think this issue will go away if I update the version?

dense cape

Which commit are you on?

dull musk

^13.0.0-dev.e37ef3af2151e556527ccd8e98ec531bdcc0ed70

dense cape

Oh, that commit is like 2 months old

dull musk

yes

So updating the version will fix this issue?

dense cape

I would say so

dull musk

alr, I'll try

Thx 🙂

dense cape

Yea, the ShardingManager got a cleanup and improvements done since then

Mind the breaking changes like ...ID to ...Id

austere helm

Is it possible to get the child_process's stderr and stdout? They're shown as null when I output the child_process of the shard.

static glen

Bot is replying twice after sharding in some servers

arctic vale
steady pivot

I am getting an error SHARDING_INVALID_EVAL_BROADCAST how can I fix this

abstract elm
terse cairn

It's reconnecting but it's not getting disconnected??

dull musk

I got this error when sharding.

Error [SHARDING_READY_DIED]: Shard 0's process exited before its Client became ready.

I sharded my bot using internal sharding and it's working fine. But I don't want to keep internal sharding system. I get above error every time I start my bot

primal heron
stray torrent
proud mortarBOT
abstract elm
stray torrent

i do js eval client.shard.fetchClientValues('ws.ping')

abstract elm

I'm not after the ping, it's the status of each shard i'm after, where it's online or not.

stray torrent

ah

dense cape

You can determine the status of the shard by the number

abstract elm

So it could be similar to broadcastEval with .broadcastEval(true, 8)

dense cape
abstract elm

Could there be an example? I'm quite unsure.

proud mortarBOT
dense cape

Same params as broadcastEval

abstract elm
dense cape

The eval should be in string

"ws.status"

abstract elm

Oh.

Perfect thank you! Then converting the numbers [0-8] to string would be?

proud mortarBOT

Documentation suggestion for @abstract elm:
_ Number.parseInt()
The Number.parseInt() method parses a string argument and returns an integer of the specified radix or base.

_ Status
The current status of the client. Here are the available statuses: (more...)

dense cape

Status should be in the Constants object

Well, you can just create an array as the inverse mapping based on those values

abstract elm

@dense cape Got it to work, although when i killed a shard i got the error Error [SHARDING_NO_CHILD_EXISTS]: Shard 1 has no active process or worker., anyway of showing the embed or..?

dense cape

Handle the rejection

abstract elm

Are there docs to it?

dense cape

For error handling?

abstract elm

Mhm. It just automatically screams that error out.

proud mortarBOT

_ Control flow and error handling
JavaScript supports a compact set of statements, specifically control flow statements, that you can use to incorporate a great deal of interactivity in your application. This chapter provides an overview of these statements.

abstract elm

I'm guessing a return won't do it?

dense cape

I don’t see how that’s relevant to error handling

abstract elm

I'm not quite sure if i'm explaining this properly. For if a shard is online/idle/reconnecting/offline etc to show that on the shard instead of it automatically throwing that error

dense cape

throwing that error
That’s why you have to handle it

abstract elm

a try and catch?

dense cape

Yes

Then you can do whatever you want with the error

abstract elm

How'd that work in my circumstance? With sending the embed even if a error throws up?

dense cape

Catch the error first

You can send a message or edit the embed or smth in the catch

abstract elm
dense cape

Did you await the fetchClientValues?

abstract elm

I did.

dense cape

Show code

abstract elm
dense cape

If ur doing it like that, then you have to .catch it then return whatever the "dead" index is

I would just refactor that into a variable

terse cairn
crimson turtle

Is sharding needed for application based on slash commands?

dense cape
crimson turtle

alr thanks

hollow sleet

can i put my client and the sharding manager in the same file?

dense cape

No

hollow sleet

ok

dense cape

The manager would be creating more managers in that case

brisk mantle

How much code do I need to change if I set shards: 'auto'?

abstract elm

None, also it's totalShards: 'auto'

brisk mantle

The guide says to set shards: 'auto'.

abstract elm

Oh that is for v13, i see.

brisk mantle

mhmm, I'm using v13.

scarlet storm

Does d.js handle auto reconnections if shards go offline or do I have to listen for this event and attempt reconnection myself? For v12

some shards seem to be off on my bot rn so not sure

terse cairn

What is Sharding?

proud mortarBOT
unborn cairn

Quick question - how would you pass a configuration file to a shard managed client?

I could create a singleton configuration client outside of the client that the client can import, but I'm trying to get away from that if possible. There's doesn't seem to be a straight forward way to pass a JS object to a client that's instantiated?

unborn cairn
proud mortarBOT

Documentation suggestion for @unborn cairn:
_ Shard#eval()
Evaluates a script or function on the shard, in the context of the Client.

unborn cairn

Woah! Thanks for that!

Assuming there isn't an example lying around?

young totem

the arg is just a function with the client as a parameter

unborn cairn

I'm not sure I follow - will I need to do this for each shard that is spawned?

surreal tulip

I remember there is broadcastEval() function

young totem
proud mortarBOT
unborn cairn

Yeah - I wouldn't need broadcastEval() here

Oh wait 🤔

lilac narwhal

broadcasteval is just eval but it does it on all shards

surreal tulip

🙄

young totem

yep broadcasteval is better here mb

unborn cairn

In the event that a shard goes down and a new one spawns though... it would need to be broadcasted again to that new shard right?

Guess I could track the "onShardSpawn" event (or whatever it is)

young totem

yeah. so you could listen to the spawn event in which case you could just use regular eval each time that fires

unborn cairn

Right

young totem

I’m guessing there’s a reason you can’t just fetch the config directly?

unborn cairn

I'm trying to refrain from using a singleton configuration object 👍

In the past, that's what I've done. I defined an object that read from environment variables and could be imported from any class. I didn't like this model, so I'm reading in a configuration file and passing in the relevant values to the client.

mortal badge

when i have sharding, will client.guilds.cache.get works?

abstract saddle
young totem

I have code to do a rolling restart on my bot (runs <Shard>.respawn() for each shard one at a time). The past few times that I've used it, some shards somehow spawned more than 1 process. Does anyone know if there's a way to prevent this? Is it a bug?

async function rollingRestart(): Promise<void> {
    const shards = manager.shards;
    for (let [i, shard] of shards) {
        try {
            const respawned = await restartShard(shard);
        } catch (e) { }
    }
    return;
}

async function restartShard(shard: Shard): Promise<void> {
    return new Promise(async (resolve, reject) => {
        try {
            const newShard = await shard.respawn().catch(() => {
                reject();
                return null;
            });
            if (!newShard) return reject();
            return resolve();
        } catch (e) {
            return reject();
        }
    });
}

It's also very difficult to fix the situation if someone reports it because broadcasteval will only run once so I can't easily find the process IDs to close them.

unborn cairn

For TypeScript discord sharding, what do you all use as the client path?

I'm assuming you don't reference bot.ts or client.ts ... right?

lilac narwhal

i dont use sharding but i assume it would be your compiled main file, which would probably be dist/client.js or dist/bot.js

young totem
unborn cairn
young totem

pretty sure it's just relative to where you run the command from but yeah not really d.js

unborn cairn

Anyone know a straight forward way to allow for a user to provide their own configuration file then load that config into the clients from the sharding perspective? Still struggling with that.

Most / all implementations I've seen all reference a source of truth which they can load at run time. At runtime, my config is still "undefined" until the configuration is specified by the user. The client technically needs this information before the shard is spawned; however, I'm not seeing a way to get this to work?

I created a Singleton configuration object that I load before the manager.spawn() method, but the import of it into client.js technically is evaluated at runtime (before the config is specified by the user). So, it evaluated to undefined.

The manager only provides one event: on('shardCreate') which is only emitted once the shard is created. I need to inject the config before that point. Any ideas? Is this even possible?

mortal badge

hey, how can i get guild data when i use shards?

i think, i need to get member of specific guild, in specific shard. iam obtaining guild data, but how should i get specific member from that data?

because guild data is an vanilla object

mortal badge
const req = await client.shard.broadcastEval(`this.guilds.cache.get("${ctx.guildID}")`);
        const guild = new Guild(client, req.find(res => !!res));
``` throws https://i.imgur.com/rpqE8Ub.png
pearl cove

broadcastEval takes a function now

broadcastEval(client => ...)

mortal badge

huh, letme try

proud mortarBOT
mortal badge

but i will receive Guild structure or plain Guild object @pearl cove

mortal badge
pearl cove

You can test it

I don't shard myself, so I can't

mortal badge

@pearl cove

pearl cove

Give me a minute

mortal badge

ok

pearl cove

Okay yes hello

You need to give it context

broadcastEval((client, guildId) => {
  ...
}, { context: guildId });

I think that will work

mortal badge

ok let me try

mortal badge
pearl cove

return it

return client.guilds...

mortal badge

oh, xD i think thats good idea im going to try

mortal badge
pearl cove

Still what?

@mortal badge

mortal badge

@pearl cove

pearl cove

I don't know, sorry

Never used sharding

mortal badge

okk np thx for try

damn, thats so weird. if i dont provide context, im getting error, when i provide context, im not getting response

mortal badge

@pearl cove i find issue, there is problem with this context

rapid token

can anybody help me?
i need sharding and specified shards to be auto but it doesnt work

and if i manually raise it it doesnt even start up

please somebody

@edgy edge sorry for ping but can you help me nice_shy

rapid token

oops i overread the mention

i thought only dms nice_shy
but please i really need help from somebody

lean parrot

getting this error related sharding

lost saddle
terse cairn

why am i getting such an error

candid zephyr

how can i pass a variable from index.js to each bot.js?

fickle mural

without knowing anything about how your project works, i'd say just use a database?

candid zephyr

well, i want to pass exactly the database connection, so i don't have to create a connection in each shard

terse cairn

Is it possible to shard at 90 servers? And would it be good or bad?

random jackal
random jackal

i think there wouldn't be any benifit

toxic island

welp, i got pretty confused here.
have never really played with broadcastEval before

let hmm = await client.shard.broadcastEval(`
[
this.shard.id,
this.guilds.cache.size
]
`)
young totem

Duplicate shards spawned

toxic island

yes, it is

young totem
near nymph

i need value of sharding like

server
user
Ping
uptime

this server shard / total shard

proud mortarBOT
young totem

Example is shown in docs ^

toxic island

thanks

near nymph
proud mortarBOT
young totem

#Duplicate shards spawned can someone help please

dark cape

how can i set how many servers per shard Hmm

runic gyro

Hello when spawning shards with ShardManager i cannot use process.send

runic gyro
dusty gate

how would i get the shard id of a guild?

proud mortarBOT
terse cairn
dusty gate

thanks

silent arch

what is djs shard

how it works i don't understand

terse cairn
silent arch

oh thank you.

dull musk
manager.on('shardDisconnect', shard => console.log(`\nShard ID: ${shard.id} Disconnected`));

Can I use this event to find out if a shard get disconnected?

runic gyro

Is there a way to load values when using broadcastEval ?

ashen musk
ashen musk
runic gyro

Yeah

ashen musk

First part is a function, second is the options, including the context.

The first variable of your function is the client, as noted in the example/code behind
the second is the assignment of the context variables

broadcastEval(script, options = {}) {
...
  script = `(${script})(this, ${JSON.stringify(options.context)})`;

simple example to get a server from any shard:

            client.shard.broadcastEval((c, { guildId }) => {
                return c.guilds.cache.get(guildId)
            }, { context: { guildId: your_input_here } })

first part is your function, second is the options + context.

abstract saddle
thin furnace

Whenever enabling sharding, I get the error Unknown interaction when executing slash commands yet the slash commands run fine. My client crashes right after

young totem
ripe sluice

My bot needs to use sharding. I am limited by memory, not performance. I'm assuming I should use autosharding - is there a reason to manually implement sharding besides performance?
Also, are there any tricks for lowering memory usage besides using LimitedCollections for cache managers?

terse cairn

Hey sorry to ask for smth prolly stupid but i'm trying to do a broadcastEval

this.client.shard.broadcastEval('[this.client.guilds.cache.size, this.client.channels.cache.size]') //I used it in v11 yeah i missed a lot in v12 and v13 changes

But i get an SHARDING_INVALID_EVAL_BROADCAST any idea ?

dense cape

v13 takes a function to eval

terse cairn

Wdym

Have you any example to just get the guilds number ? 🤔

dense cape

The v13 update guide includes this change

Eval((client, {id}) => client.guilds.cache.get(id)?.memberCount, { context: { id: "…" } })

terse cairn
dense cape

Keep in mind you can’t use variables outside of the function, you’ll have to pass it in via the context

terse cairn

Ik i had i used it v11 just i missed a lot 😂

dense cape

.cache.get

terse cairn
dense cape

I too have v11 relapses when I’m not using intelli-sense

fallen sleet
terse cairn

The cache thing is the easier to remember thing

terse cairn
static glen

how can I restart a specific shard?

covert vortex

Hello, why do all the shards restart every 15 minutes ?

next plaza
covert vortex

no, and the connection is good

i upgraded my bot to v13 and now it does this, before it did not

tardy granite

does anyone know how to shard a bot? I tried so many times and it just doesn't work

frail delta
const client = new Discord.Client({
    intents: ['GUILDS', 'GUILD_MEMBERS', 'GUILD_BANS', 'GUILD_MESSAGES', 'DIRECT_MESSAGES'],
    partials: ['CHANNEL'],
    allowedMentions: {
        parse: ['users', 'roles'],
        repliedUser: true
    },
    shards: 'auto'
});

if I set shards to auto it will add shards automatically?

shy nymph

but it's not ideal for bigger bots

frail delta

do I have to reboot the bot?

shy nymph

reboot the bot for what

frail delta
frail delta
shy nymph

nope as it's automated

the same with using a sharding manager file, it'll do it automatically

frail delta

ok and what is the better way when the bot is on a lot of guilds?

shy nymph

the better way is to use a sharding manager for bigger guilds:

Apart from ShardingManager, discord.js also supports a sharding mode known as Internal sharding. Internal sharding creates multiple websocket connections from the same process, and does not require major code changes. To enable it, simply pass "shards: 'auto'" as ClientOptions to the Client constructor. However, internal sharding is not ideal for bigger bots due to high memory usage of the single main process and will not be further discussed in this guide.
frail delta

ok thanks, going to check the guide about that

shy nymph

np

frail delta
shy nymph np

is there a way to reboot some shards or we can just reboot all shards?

terse cairn

i'm getting Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed for no reason

i was like coding my bot on beta bot with like 3 servers and it was working fine and then moved to bot with 5k servers and now i'm getting tons of different errors

Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed
Emitted 'error' event on process instance at:
Error [TypeError]: Cannot read property 'id' of null
Error [SHARDING_IN_PROCESS]: Shards are still being spawned.

and worst thing is that it doesn't tell where error actually is 😭

bitter shadow

so I got a ready.js file for my ready event. When my bot starts with 2 shards I ofc got 2 ready.js events with 2 shards. How could I check the shard that is using which ready.js process?

young totem
next plaza
terse cairn

i fixed it, apparently i was using client before it even started

keen abyss

whats sharding

prisma crystal

Alright so, I'm using broadcastEval but I cannot access the iconURL method inside it

Code:

await client.shard.broadcastEval(async (client, {
        guild,
        logChannel
    }) => {
        const {
            MessageEmbed
        } = require('discord.js');
        const channel = client.channels.cache.get(logChannel);

        if (channel) {
            channel.send({
                embeds: [
                    new MessageEmbed()
                    .setTitle('ADDED TO GUILD!')
                    .setDescription(`**Guild Info**`)
                    .addFields({
                        name: `Name`,
                        value: `${guild.name}`
                    }, {
                        name: `ID`,
                        value: `${guild.id}`
                    }, {
                        name: `Members`,
                        value: guild.memberCount.toLocaleString()
                    })
                    .setFooter(`Currently in ${client.guilds.cache.size.toLocaleString()} guilds`)
                    .setTimestamp()
                    .setThumbnail(guild.iconURL({
                        dynamic: true
                    }))
                    .setColor('#81FF89')
                ]
            });
        }
    }, {
        context: {
            guild,
            logChannel
        }
    });

Error:

Unhandled promise rejection:
 Error [TypeError]: guild.iconURL is not a function

Does anyone know why?

dense cape

You can’t pass full structures into broadcastEval

Well, it will be jsonified, then parsed

I would just create the embed outside of the eval

prisma crystal

Oh alright, will give it a try! Thank you :)

fierce bane

is there anything wrong with

const config = require('./config.json');
const Discord = require('discord.js');
const Statcord = require("statcord.js");

const manager = new Discord.ShardingManager('./index.js', {
  token: config.token,
  totalShards: 1
});

manager.spawn();

manager.on('shardCreate', shard => console.log(`launching shard #${shard.id + 1}`));
frigid kiln

hey
i get this error

[shards] shard 1 loaded!
Error [SHARDING_NO_CHILD_EXISTS]: Shard 1 has no active process or worker.
    at Shard.fetchClientValue (/root/legacyNewBot/node_modules/discord.js/src/sharding/Shard.js:240:62)
    at ShardingManager._performOnShards (/root/legacyNewBot/node_modules/discord.js/src/sharding/ShardingManager.js:286:68)
    at ShardingManager.fetchClientValues (/root/legacyNewBot/node_modules/discord.js/src/sharding/ShardingManager.js:264:17)
    at ShardingManager.<anonymous> (/root/legacyNewBot/sharding.js:15:13)
    at ShardingManager.emit (node:events:394:28)
    at ShardingManager.createShard (/root/legacyNewBot/node_modules/discord.js/src/sharding/ShardingManager.js:164:10)
    at ShardingManager.spawn (/root/legacyNewBot/node_modules/discord.js/src/sharding/ShardingManager.js:215:26) {
  [Symbol(code)]: 'SHARDING_NO_CHILD_EXISTS'
}

and this is my shard.js

const { ShardingManager } = require('discord.js');
const config = require("./config.json");


const legacyShard = new ShardingManager('./index.js', { 
    totalShards: 2, 
    token: "token kek"

});
legacyShard.spawn();

legacyShard.on('shardCreate', shard => {
    console.log(`[shards] shard ${shard.id} loaded!`);

legacyShard.fetchClientValues('guilds.cache.size')
  .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
  .catch(console.error);
});
frigid kiln
fierce bane
lost saddle Any debug/error logs?

yes

 C:\Users\weirdpc\Desktop\MuffinBot-v13> node .
C:\Users\weirdpc\Desktop\MuffinBot-v13\node_modules\discord.js\src\util\Util.js:336
        given[key] = def[key];
                   ^

TypeError: Cannot create property 'shardCount' on string 'TOKEN'
    at Function.mergeDefault (C:\Users\weirdpc\Desktop\MuffinBot-v13\node_modules\discord.js\src\util\Util.js:336:20)
    at new BaseClient (C:\Users\weirdpc\Desktop\MuffinBot-v13\node_modules\discord.js\src\client\BaseClient.js:20:25)
    at new WebhookClient (C:\Users\weirdpc\Desktop\MuffinBot-v13\node_modules\discord.js\src\client\WebhookClient.js:26:5)       
    at Object.<anonymous> (C:\Users\weirdpc\Desktop\MuffinBot-v13\utils\logger.js:6:23)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
PS C:\Users\weirdpc\Desktop\MuffinBot-v13> ```
dense cape

It’s a WebhookClient issue, the params have changed iirc

fierce bane
dense cape

Yea, it takes an object now

fierce bane

oh

dense cape

You only need the id + token or the url

{ id, token, url }

fierce bane

yeah

const webhookClient = new Discord.WebhookClient({config.webhook_id, config.webhook_url});``` is fine right
url is token in config.json
dense cape

No

fierce bane

url is token

dense cape

You need to specify the object keys

fierce bane

im confused

dense cape

{ myToken } is not the same as { token }

const myToken = "12345"
const token = myToken
const objA = { token }
const objB = { myToken }
const objC = { token: myToken }

objA.token // "12345"
objB.token // undefined
objC.token// "12345"```
terse cairn

Is it possible to shard at 90 servers, and would it be recommended?

I say recommended as in, will it break my bot or not?

fickle mural

yes it is possible, no it's not recommended and the answer is maybe
it depends on your code

fierce bane

looks like i didnt do a great job on updating everything

abstract saddle

How to fix ?

prisma crystal

I'm trying to get the total membercount through this piece of code

const memberValues = [];

            await client.shard.broadcastEval((client, {
                memberValues
            }) => {
                memberValues.push(client.guilds.cache.reduce((prev, guild) => prev + guild.memberCount, 0));
            }, {
                context: {
                    memberValues
                }
            });

            console.log(memberValues.reduce((prev, curr) => prev + curr, 0));

It outputs 0 every time

any idea to why?

deft mist

what is sharding?

dense cape

Well, it won’t work bc the reference via context and outer scope is lost

It’s better to just return the number in the eval and resolve the data from the eval

dense cape
stoic remnant
terse cairn

Any idea to list ALL the guilds in all Shards ? With the broadcastEval, i tried smth but it was sending first the guilds of the Shard 0 then Shard 1 (using 2 messages)

terse cairn

Solved

echo arch

I don't know what happened but my bot just received everything at once and processed it for shard 0 at once and now I cant login due to 429.

I understand the 429 part but the receiving everything at once is a mystery

deft mist
lilac narwhal

no

abstract elm
next plaza
echo arch

In the end, I couldn't figure it out xd. Wasn't the VPS's internet as other shards responded fine but it seems fine now

deft mist
next plaza
little forge

Does anyone have information on the ideal infrastructure for scaling your bot with sharding? I’m trying to understand if I can have the bot running on several different physical servers or if I have to get one beefy server and just utilize sharding to split up the processes, or a combination of both. Just looking for more concrete info.

abstract elm
next plaza
stray torrent

Hey, i have my sharding file like this., although it does not want to spawn more shards.

  
const { ShardingManager } = require('discord.js');

const config = require("./botconfig/config.json");

const manager = new ShardingManager('./bot.js', { token: config.token, totalShards: 'auto', respawn: true });

manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`));

manager.spawn();```
how would this be if i for example want to manually shard? with lets say 5 shards?
fickle mural

totalShards: 5, no?

stray torrent

o hm lemme try