diff --git a/src/Interface/Clients/DiscordJS.ts b/src/Interface/Clients/DiscordJS.ts index 142d9d2f..4bbd14a4 100644 --- a/src/Interface/Clients/DiscordJS.ts +++ b/src/Interface/Clients/DiscordJS.ts @@ -9,10 +9,12 @@ import { ClientFiller } from '../ClientFiller' export default class DiscordJS extends ClientFiller { get userCount(): number { if (this.client.guilds?.constructor?.name === 'GuildManager') + // v12 return this.client.guilds?.cache?.reduce( (count: number, guild: AnyObject) => count + guild.memberCount, 0 ) + // v11 else return this.client.guilds?.reduce( (count: number, guild: AnyObject) => count + guild.memberCount, @@ -22,13 +24,16 @@ export default class DiscordJS extends ClientFiller { get serverCount(): number { if (this.client.guilds?.constructor?.name === 'GuildManager') + // v12 return this.client.guilds?.cache?.size - else return this.client.guilds?.size + else return this.client.guilds?.size // v11 } get voiceConnections(): number { - if (this.client.voice) return this.client.voice.broadcasts?.length || 0 - else return this.client.broadcasts?.size + if (this.client.voice) + // v12 + return this.client.voice.broadcasts?.length || 0 + else return this.client.broadcasts?.size // v11 } get clientID(): string | undefined { @@ -36,11 +41,24 @@ export default class DiscordJS extends ClientFiller { } get shard(): Shard | undefined { - return this.client.shard - ? { - id: this.client.shard.id, - count: this.client.shard.count - } - : undefined + if (this.client.shard?.ids && this.client.shard?.ids.length !== 1) + // v12 unsupported + return undefined + + if (this.client.shard?.id) + // v11 + return { + id: this.client.shard.id, + count: this.client.shard.count + } + + if (this.client.shard?.ids) + // v12 supported, using a ShardingManager + return { + id: this.client.shard.ids[0], + count: this.client.shard.count + } + + return undefined } } diff --git a/tests/Interface/Clients/common.test.ts b/tests/Interface/Clients/common.test.ts index 4bc47c7e..7584628c 100644 --- a/tests/Interface/Clients/common.test.ts +++ b/tests/Interface/Clients/common.test.ts @@ -6,51 +6,80 @@ import Collection from '@discordjs/collection' const clientsDir = path.join(__dirname, '../../../src/Interface/Clients') const fakeClients = { - Discordie: { - Guilds: { - size: 123, - toArray: () => [1, 2, 3].map((n) => ({ member_count: n })) - }, - VoiceConnections: [1, 2, 3], - User: { id: 'abc' }, - options: { - shardId: 'abc', - shardCount: 123 + Discordie: [ + { + Guilds: { + size: 123, + toArray: () => [1, 2, 3].map((n) => ({ member_count: n })) + }, + VoiceConnections: [1, 2, 3], + User: { id: 'abc' }, + options: { + shardId: 'abc', + shardCount: 123 + } } - }, - DiscordIO: { - servers: [1, 2, 3].map((n) => ({ member_count: n })), - _vChannels: [1, 2, 3], - id: 'abc', - _shard: ['abc', 123] - }, - DiscordJS: { - guilds: { - constructor: { name: 'GuildManager' }, - cache: new Collection([1, 2, 3].map((n) => [n, { memberCount: n }])) - }, - voice: { broadcasts: [1, 2, 3] }, - user: { id: 'abc' }, - shard: { + ], + DiscordIO: [ + { + servers: [1, 2, 3].map((n) => ({ member_count: n })), + _vChannels: [1, 2, 3], id: 'abc', - count: 123 + _shard: ['abc', 123] } - }, - Eris: { - guilds: new Collection([1, 2, 3].map((n) => [n, { memberCount: n }])), - voiceConnections: { - constructor: { name: 'VoiceConnectionManager' }, - size: 123 + ], + DiscordJS: [ + { + // v11 + guilds: new Collection([1, 2, 3].map((n) => [n, { memberCount: n }])), + broadcasts: { size: 123 }, + user: { id: 'abc' }, + shard: { + id: 'abc', + count: 123 + } }, - user: { id: 'abc' } - }, - Paracord: { - guilds: { - values: [1, 2, 3].map((n) => [n, { member_count: n }]), - size: 123 + { + // v12 + guilds: { + constructor: { name: 'GuildManager' }, + cache: new Collection([1, 2, 3].map((n) => [n, { memberCount: n }])) + }, + voice: { broadcasts: [1, 2, 3] }, + user: { id: 'abc' }, + shard: { + ids: [123], + count: 123 + } }, - user: { id: 'abc' } - } + { + // v12, unsupported sharding + testAsEmpty: true, + shard: { + ids: [1, 2, 3], + count: 123 + } + } + ], + Eris: [ + { + guilds: new Collection([1, 2, 3].map((n) => [n, { memberCount: n }])), + voiceConnections: { + constructor: { name: 'VoiceConnectionManager' }, + size: 123 + }, + user: { id: 'abc' } + } + ], + Paracord: [ + { + guilds: { + values: [1, 2, 3].map((n) => [n, { member_count: n }]), + size: 123 + }, + user: { id: 'abc' } + } + ] } const noVoiceFillers: (keyof typeof fakeClients)[] = ['Paracord'] const noShardFillers: (keyof typeof fakeClients)[] = ['Eris', 'Paracord'] @@ -64,7 +93,6 @@ fs.readdirSync(clientsDir).forEach((file) => { describe(`${fileName} class`, () => { const emptyClient = new Client({}) - const fakeClient = fakeClients[fileName] const testClient = (c: ClientFiller, name: string, empty = false) => { const eType = empty ? 'undefined' : undefined @@ -108,7 +136,13 @@ fs.readdirSync(clientsDir).forEach((file) => { } testClient(emptyClient, 'empty client', true) - if (fakeClient) testClient(new Client(fakeClient), 'fake client') + fakeClients[fileName]?.forEach((client: any, index: number) => + testClient( + new Client(client), + `fake client [${index}]`, + client.testAsEmpty + ) + ) }) }) })