discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.46k stars 3.97k forks source link

The shardCount option does not allow the value 1 #5611

Closed SliceCraft closed 3 years ago

SliceCraft commented 3 years ago

Please describe the problem you are having in as much detail as possible: I am sharding my discord bot and while testing I only wanted to get the basics setup first so I was only going to use 1 shard. In the client option I defined the option: shardCount: 1 If I try to run the code after that I get the error: TypeError [CLIENT_INVALID_OPTION]: The shardCount option must be a number greater than or equal to 1 I looked into the problem a bit and found that when the shardCount option is set to 1 and I place a breakpoint at line 437 (where the error originates from) in /client/Client.js the debugger says the shardCount option is NaN. If I change the shardCount option to 2 it will show the expected result again. I believe the problem lies somewhere in the if at line 52 of that file. Before the if is run this.option.shardCount is 1 but after the if the value has been set to NaN. The debuger also says that data.SHARD_COUNT is "auto". So on line 54 this.options.shardCount is set to a number with the value of a string which returns NaN.

Include a reproducible code sample here, if possible:

// File: index.js
const { ShardingManager } = require("discord.js");
const managerOptions = {
    token: "",
    respawn: false
};
const manager = new ShardingManager("./bot.js", managerOptions);
const shard = manager.createShard(0);
shard.spawn();
// File: bot.js
const discord = require("discord.js");
const client = new discord.Client({ shardCount: 1 });
client.login();

Further details:

Relevant client options:

vladfrangu commented 3 years ago

You do not set shardCount on the process spawned by a ShardingManager, as it does that for you.

Although it being NaN is mildly concerning. Half-bug found!

SliceCraft commented 3 years ago

Also in the case I don't define shardCount I still get the error. New bot.js file is:

const discord = require("discord.js");
const client = new discord.Client();
client.login();

That is also how I first noticed the problem, I decided to manually set shardCount as I realized setting that to 2 would fix the problem which is why I included that in the bug report.

DumbGameMaker commented 3 years ago

You do not set shardCount on the process spawned by a ShardingManager, as it does that for you.

Although it being NaN is mildly concerning. Half-bug found!

line 437 (where the error comes from) is a comment. the line is this:

* Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval} on a script

PyroTechniac commented 3 years ago

Are you modifying process.env anywhere? the only thing I could see that would break it is if process.env.SHARD_COUNT is set to something that wouldn't be a valid number

DumbGameMaker commented 3 years ago

i'm trying it now and it is broken.

// index.js
const { ShardingManager } = require("../discord.js/");
const managerOptions = {
  token: "token here",
  respawn: false,
  shardCount: 1,
};
const manager = new ShardingManager("./bot.js", managerOptions);
const shard = manager.createShard(0);
shard.spawn();
//bot.js
const discord = require("../discord.js/");
const client = new discord.Client({
  intents: ["GUILDS", "GUILD_MESSAGES"],
});
client.login();
DumbGameMaker commented 3 years ago

so far I have fixed it by replacing the error message in the /src/client/Client.js with a line saying options.shardCount = 1;

SliceCraft commented 3 years ago

Are you modifying process.env anywhere? the only thing I could see that would break it is if process.env.SHARD_COUNT is set to something that wouldn't be a valid number

I am not modifying process.env. All the code I was using is shared.

SpaceEEC commented 3 years ago

Is there some reason you are not using ShardingManager#spawn? From a not too deep dive into the source in relation to this error, it seems like Shard#spawn does not resolve ShardingManager#totalShards from 'auto' to the relevant shard count, which is then passed along to the shard process which then results in this rather confusing error.

SliceCraft commented 3 years ago

I run shards in multiple docker containers and would like to be able to separate the instances by giving them their own hardcoded shard id.

Shard#Spawn not resolving the shard count would be weird since the issue only occurs when I specify the shardCount as 1. If I specify the shardCount as 2 (but still spawning through the exact same way I am doing now) the issue dissapear. This is also why I specified the priority to be low, when sharding you will most likely use multiple shards instead of just one.

kyranet commented 3 years ago

I'm unable to reproduce this issue with 374c779 using the following code:

// src/shard.js
import { ShardingManager } from 'discord.js';

const manager = new ShardingManager('./src/index.js', {
    respawn: false,
    totalShards: 1,
});
const shard = manager.createShard(0);
shard.spawn();
// src/index.js
import { Client } from 'discord.js';

const client = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES'], shardCount: 1 });

void client.login()
    .then(() => console.log(`${client.user.tag} is now ready!`))
    .catch(console.error);

Running both node src/index.js and node src/shard.js work just fine.

Regarding https://github.com/discordjs/discord.js/issues/5611#issuecomment-846645163, @DumbGameMaker, shardCount is not a valid option.


Please upgrade to v13 (current master or discord.js@dev on npm) and let me know if this problem still persists.

SliceCraft commented 3 years ago

Thanks for helping, I realised that I forgot to define totalShards in the ShardingManager options. Stupid mistake of me and thanks everyone for helping.