Skip to content

Commit 7a43b33

Browse files
authored
Merge 014fca0 into d7cd162
2 parents d7cd162 + 014fca0 commit 7a43b33

File tree

9 files changed

+68
-16
lines changed

9 files changed

+68
-16
lines changed

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"promise.allsettled": "^1.0.2"
4848
},
4949
"devDependencies": {
50+
"@discordjs/collection": "^0.1.6",
5051
"@types/jest": "^26.0.15",
5152
"@types/node": "^14.0.5",
5253
"@types/promise.allsettled": "^1.0.3",

src/Interface/ClientFiller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class ClientFiller {
1919
this.client = client
2020
}
2121

22-
/** Gets the amount of users the bot has cached. */
22+
/** Gets the amount of users the bot can reach. */
2323
get userCount(): number | undefined {
2424
return 0
2525
}

src/Interface/Clients/DiscordIO.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AnyObject } from '../../Utils/Util'
12
import { Shard } from '../../Utils/Constants'
23
import { ClientFiller } from '../ClientFiller'
34

@@ -7,8 +8,13 @@ import { ClientFiller } from '../ClientFiller'
78
*/
89
export default class DiscordIO extends ClientFiller {
910
get userCount() {
10-
if (!this.client.users) return undefined
11-
return Object.keys(this.client.users).length
11+
if (!this.client.servers) return undefined
12+
return Object.keys(this.client.servers)
13+
.map((id) => this.client.servers[id])
14+
.reduce(
15+
(count: number, guild: AnyObject) => count + guild.member_count,
16+
0
17+
)
1218
}
1319

1420
get serverCount() {

src/Interface/Clients/DiscordJS.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AnyObject } from '../../Utils/Util'
12
import { Shard } from '../../Utils/Constants'
23
import { ClientFiller } from '../ClientFiller'
34

@@ -7,9 +8,16 @@ import { ClientFiller } from '../ClientFiller'
78
*/
89
export default class DiscordJS extends ClientFiller {
910
get userCount(): number {
10-
if (this.client.users?.constructor?.name === 'UserManager')
11-
return this.client.users?.cache?.size
12-
else return this.client.users?.size
11+
if (this.client.guilds?.constructor?.name === 'GuildManager')
12+
return this.client.guilds?.cache?.reduce(
13+
(count: number, guild: AnyObject) => count + guild.memberCount,
14+
0
15+
)
16+
else
17+
return this.client.guilds?.reduce(
18+
(count: number, guild: AnyObject) => count + guild.memberCount,
19+
0
20+
)
1321
}
1422

1523
get serverCount(): number {

src/Interface/Clients/Discordie.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AnyObject } from '../../Utils/Util'
12
import { Shard } from '../../Utils/Constants'
23
import { ClientFiller } from '../ClientFiller'
34

@@ -7,11 +8,14 @@ import { ClientFiller } from '../ClientFiller'
78
*/
89
export default class Discordie extends ClientFiller {
910
get userCount(): number {
10-
return this.client.Guilds?.size
11+
return this.client.Guilds?.toArray().reduce(
12+
(count: number, guild: AnyObject) => count + guild.member_count,
13+
0
14+
)
1115
}
1216

1317
get serverCount(): number {
14-
return this.client.Users?.size
18+
return this.client.Guilds?.size
1519
}
1620

1721
get voiceConnections(): number {

src/Interface/Clients/Eris.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AnyObject } from '../../Utils/Util'
12
import { ClientFiller } from '../ClientFiller'
23

34
/**
@@ -6,7 +7,10 @@ import { ClientFiller } from '../ClientFiller'
67
*/
78
export default class Eris extends ClientFiller {
89
get userCount(): number {
9-
return this.client.users?.size
10+
return this.client.guilds?.reduce(
11+
(count: number, guild: AnyObject) => count + guild.memberCount,
12+
0
13+
)
1014
}
1115

1216
get serverCount(): number {

src/Interface/Clients/Paracord.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
import { AnyObject } from '../../Utils/Util'
12
import { ClientFiller } from '../ClientFiller'
23

34
/**
45
* Represents the client filler for Paracord clients.
56
* @private
67
*/
78
export default class Paracord extends ClientFiller {
8-
get userCount(): number {
9-
return this.client.users?.size
9+
get userCount() {
10+
if (!this.client.guilds) return undefined
11+
return Array.from(this.client.guilds.values as ArrayLike<AnyObject>).reduce(
12+
(count: number, guild: AnyObject) => count + guild.member_count,
13+
0
14+
)
1015
}
1116

1217
get serverCount(): number {

tests/Interface/Poster.test.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Collection from '@discordjs/collection'
12
import * as PosterModule from '../../src/Interface/Poster'
23
import { Service } from '../../src/Interface/Service'
34
import TopGG from '../../src/Interface/Lists/TopGG'
@@ -6,6 +7,11 @@ import { PosterOptions } from '../../src/Utils/Constants'
67
describe('Poster module', () => {
78
describe('Poster class', () => {
89
const { Poster } = PosterModule
10+
const fakeClientGuilds = new Collection(
11+
Array(123)
12+
.fill('a')
13+
.map((_value, index) => [index, { memberCount: 2 }]) as [number, any][]
14+
)
915

1016
describe('constructor', () => {
1117
it('should throw when instatiated without options', () => {
@@ -109,12 +115,17 @@ describe('Poster module', () => {
109115
expect(fn).toHaveBeenCalled()
110116
})
111117

112-
it('should use the clientFiller', () => {
118+
it('should use the clientFiller', (done) => {
113119
const p = new Poster({
114-
client: { users: { size: 123 } },
120+
client: { guilds: fakeClientGuilds },
115121
clientLibrary: 'discord.js'
116122
})
117-
expect(p.getUserCount()).resolves.toBe(123)
123+
p.getUserCount()
124+
.then((res) => {
125+
expect(res).toBe(123 * 2)
126+
done()
127+
})
128+
.catch(() => done.fail())
118129
})
119130

120131
it('should default to 0', () => {
@@ -219,7 +230,7 @@ describe('Poster module', () => {
219230
describe('post method', () => {
220231
const p = new Poster({
221232
client: {
222-
guilds: { size: 123 },
233+
guilds: fakeClientGuilds,
223234
users: { size: 456 },
224235
broadcasts: { size: 789 }
225236
},
@@ -242,7 +253,7 @@ describe('Poster module', () => {
242253
await p.post('service')
243254
expect(p.postManual).toHaveBeenLastCalledWith('service', {
244255
serverCount: 123,
245-
userCount: 456,
256+
userCount: 123 * 2,
246257
voiceConnections: 789
247258
})
248259
})

0 commit comments

Comments
 (0)