feat: remove shards

This commit is contained in:
Elysia 2024-01-23 21:14:25 +07:00
parent 2472642184
commit 0386b5b1d7
3 changed files with 3 additions and 49 deletions

View File

@ -8,7 +8,7 @@ const BaseClient = require('./BaseClient');
const ActionsManager = require('./actions/ActionsManager'); const ActionsManager = require('./actions/ActionsManager');
const ClientVoiceManager = require('./voice/ClientVoiceManager'); const ClientVoiceManager = require('./voice/ClientVoiceManager');
const WebSocketManager = require('./websocket/WebSocketManager'); const WebSocketManager = require('./websocket/WebSocketManager');
const { Error, TypeError, RangeError } = require('../errors'); const { Error, TypeError } = require('../errors');
const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager'); const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager');
const BillingManager = require('../managers/BillingManager'); const BillingManager = require('../managers/BillingManager');
const ChannelManager = require('../managers/ChannelManager'); const ChannelManager = require('../managers/ChannelManager');
@ -31,8 +31,6 @@ const Webhook = require('../structures/Webhook');
const Widget = require('../structures/Widget'); const Widget = require('../structures/Widget');
const { Events, Status } = require('../util/Constants'); const { Events, Status } = require('../util/Constants');
const DataResolver = require('../util/DataResolver'); const DataResolver = require('../util/DataResolver');
const Intents = require('../util/Intents');
const Options = require('../util/Options');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const DiscordAuthWebsocket = require('../util/RemoteAuth'); const DiscordAuthWebsocket = require('../util/RemoteAuth');
const Sweepers = require('../util/Sweepers'); const Sweepers = require('../util/Sweepers');
@ -48,39 +46,6 @@ class Client extends BaseClient {
constructor(options) { constructor(options) {
super(options); super(options);
const data = require('node:worker_threads').workerData ?? process.env;
const defaults = Options.createDefault();
if (this.options.shards === defaults.shards) {
if ('SHARDS' in data) {
this.options.shards = JSON.parse(data.SHARDS);
}
}
if (this.options.shardCount === defaults.shardCount) {
if ('SHARD_COUNT' in data) {
this.options.shardCount = Number(data.SHARD_COUNT);
} else if (Array.isArray(this.options.shards)) {
this.options.shardCount = this.options.shards.length;
}
}
const typeofShards = typeof this.options.shards;
if (typeofShards === 'undefined' && typeof this.options.shardCount === 'number') {
this.options.shards = Array.from({ length: this.options.shardCount }, (_, i) => i);
}
if (typeofShards === 'number') this.options.shards = [this.options.shards];
if (Array.isArray(this.options.shards)) {
this.options.shards = [
...new Set(
this.options.shards.filter(item => !isNaN(item) && item >= 0 && item < Infinity && item === (item | 0)),
),
];
}
this._validateOptions(); this._validateOptions();
/** /**
@ -647,18 +612,6 @@ class Client extends BaseClient {
* @private * @private
*/ */
_validateOptions(options = this.options) { _validateOptions(options = this.options) {
if (typeof options.intents === 'undefined') {
throw new TypeError('CLIENT_MISSING_INTENTS');
} else {
options.intents = Intents.resolve(options.intents);
}
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount) || options.shardCount !== 1) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shardCount', 'a number equal to 1');
}
if (options.shards && !(options.shards === 'auto' || Array.isArray(options.shards))) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shards', "'auto', a number or array of numbers");
}
if (options.shards && !options.shards.length) throw new RangeError('CLIENT_INVALID_PROVIDED_SHARDS');
if (typeof options.makeCache !== 'function') { if (typeof options.makeCache !== 'function') {
throw new TypeError('CLIENT_INVALID_OPTION', 'makeCache', 'a function'); throw new TypeError('CLIENT_INVALID_OPTION', 'makeCache', 'a function');
} }

View File

@ -718,7 +718,7 @@ class WebSocketShard extends EventEmitter {
delete d.version; delete d.version;
delete d.agent; delete d.agent;
this.debug(`[IDENTIFY] Shard ${this.id}/${client.options.shardCount} with intents: ${d.intents}`); this.debug(`[IDENTIFY] Shard ${this.id}`);
this.send({ op: Opcodes.IDENTIFY, d }, true); this.send({ op: Opcodes.IDENTIFY, d }, true);
} }

View File

@ -159,6 +159,7 @@ class Options extends null {
closeTimeout: 5_000, closeTimeout: 5_000,
waitGuildTimeout: 15_000, waitGuildTimeout: 15_000,
shardCount: 1, shardCount: 1,
shards: [0],
makeCache: this.cacheWithLimits(this.defaultMakeCacheSettings), makeCache: this.cacheWithLimits(this.defaultMakeCacheSettings),
messageCacheLifetime: 0, messageCacheLifetime: 0,
messageSweepInterval: 0, messageSweepInterval: 0,