admin管理员组

文章数量:1393035

I want to get every Shard id by client.shards.id but I have this error:

(node:3164) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
    at C:\Users\admin\Desktop\PoloIsASkid\bot.js:28:47
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

I have tried client.shards.Id too but it didn't work.

How do I fix it? I need it for DBL Stats posting in this mand:

dbl.postStats(size, client.shards.id, client.shards.total);

I want to get every Shard id by client.shards.id but I have this error:

(node:3164) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
    at C:\Users\admin\Desktop\PoloIsASkid\bot.js:28:47
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

I have tried client.shards.Id too but it didn't work.

How do I fix it? I need it for DBL Stats posting in this mand:

dbl.postStats(size, client.shards.id, client.shards.total);
Share Improve this question edited Oct 10, 2020 at 14:57 Marco Bonelli 69.8k21 gold badges127 silver badges146 bronze badges asked Aug 18, 2020 at 10:04 user13323454user13323454 0
Add a ment  | 

3 Answers 3

Reset to default 4

There is no way of getting the Shard ID directly within the Child process. You'll have to send a message from the Main process to the Child process with the Shard ID.

// Creating the Discord.ShardingManager.
const ShardingManager = new Discord.ShardingManager(Path.join(__dirname, "Discord.js"), {
    token: process.env.DISCORD_AUTH_TOKEN,
    totalShards: 'auto'
});

// Trying to spawn the required shards.
ShardingManager.spawn().catch(error => console.error(`[ERROR/SHARD] Shard failed to spawn.`));

ShardingManager.on("shardCreate", shard => {
    // Listeing for the ready event on shard.
    shard.on("ready", () => {
        console.log(`[DEBUG/SHARD] Shard ${shard.id} connected to Discord's Gateway.`)
        // Sending the data to the shard.
        shard.send({type: "shardId", data: {shardId: shard.id}});
    });
});

Now, on the Child process, you need to listen to the "message" event of process and grab the Shard ID.

// This is where your Discord bot's code is.
process.on("message", message => {
    if (!message.type) return false;

    if (message.type == "shardId") {
        console.log(`The shard id is: ${message.data.shardId}`);
    };
});

Update:

I just found out that you may use Guild#shardID and Guild#shard. This applies to Discord JS v12.

Example:

message.guild.shardID

The error tells you that client.shards does not exist, that it is undefined. You cannot reference the property of that which does not exist, this is why client.shards.id and client.shards.Id fails in similar ways.

In discord.js v12, Client.shard.ids will be an array of shard IDs being handled by this client (docs).

For a list of all shards from the ShardingManager, the spawn() function returns a Promise of a Collection of shards and you can get their IDs from there (docs).

本文标签: javascriptDiscordjs getting shards idStack Overflow