/
PresenceTemplater.ts
211 lines (187 loc) · 7.83 KB
/
PresenceTemplater.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { Collection, Snowflake, Guild } from 'discord.js';
import humanizeDuration from 'humanize-duration';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import { ExtendedClient } from '../ExtendedClient';
import { AsyncTemplater } from '../abstract/AsyncTemplater';
import { PresenceTemplaterGetters } from '../../types';
dayjs.extend(utc);
dayjs.extend(timezone);
/**
* An async templater class to help with the application of templates for presence statuses.
*
* Supported templates:
*
* | Template | Replaced by |
* |------------------|-----------------------------------------------------------------------------------------------------------------------------|
* | `{num_guild}` | Get the number of guilds the client is connected to. |
* | `{prefix}` | Get the prefix used by the client. |
* | `{cur_time}` | Get the current time in the client's locale. |
* | `{owner_name}` | Get the owner's name. If no owner is specified then it returns `undefined`. |
* | `{client_name}` | Get the client's name. If the client's user is not ready yet, then it returns `undefined`. |
* | `{uptime}` | Get the client's uptime since last ready event emitted in a human-readable shape. Returns `null` if no uptime is available. |
* | `{ready_time}` | Get the client's time at which the last ready event was emitted. Returns `null` if no `readyAt` timestamp is available. |
* | `{num_members}` | Get the total number of members across all the guilds that the client is connected to. |
* | `{num_commands}` | Get the number of commands registered to this client. |
*/
export class PresenceTemplater extends AsyncTemplater {
/**
* The client that this presence async templater will use as a data source.
* @type {ExtendedClient}
* @memberof PresenceTemplater
*/
public readonly client: ExtendedClient;
/**
* An object to map a templater key to a custom getter. The getter function
* should return a Promise that resolves to the correct string that the
* templater should replace the key with.
* @type {PresenceTemplaterGetters}
* @memberof PresenceTemplater
*/
public readonly customGetters: PresenceTemplaterGetters;
/**
* @param client The client that this presence async templater will use as a data source.
* @param customGetters The custom getters object to define custom templates.
*/
public constructor(client: ExtendedClient, customGetters: PresenceTemplaterGetters) {
super([
'num_guilds',
'prefix',
'cur_time',
'owner_name',
'client_name',
'uptime',
'ready_time',
'num_members',
'num_commands',
...Object.keys(customGetters)
]);
this.client = client;
this.customGetters = customGetters;
}
public get(key: string): Promise<string> {
let getter: (() => Promise<string>) | undefined;
switch (key) {
case 'num_guilds':
return this.getNumberOfGuilds();
case 'prefix':
return this.getPrefix();
case 'cur_time':
return this.getCurrentTime();
case 'owner_name':
return this.getOwnerName();
case 'client_name':
return this.getClientName();
case 'uptime':
return this.getUptime();
case 'ready_time':
return this.getReadyTime();
case 'num_members':
return this.getNumberOfMembers();
case 'num_commands':
return this.getNumberOfCommands();
default:
getter = this.customGetters[key];
if (getter) {
return getter();
}
return Promise.reject(new Error('Unknown key inserted in PresenceTemplater.'));
}
}
/**
* Get the number of guilds the client is connected to.
* @returns A promise that resolves to the number of [guilds](https://old.discordjs.dev/#/docs/discord.js/main/class/Guild).
*/
private getNumberOfGuilds(): Promise<string> {
if (!this.client.shard) {
return Promise.resolve(this.client.guilds.cache.size.toString());
}
return this.client.shard.fetchClientValues('guilds.cache.size')
.then((results) => {
const castedResults = results as number[];
return castedResults.reduce((sum, size) => sum + size, 0).toString();
});
}
/**
* Get the prefix used by the client.
* @returns A promise that resolves to the client's prefix.
*/
private getPrefix(): Promise<string> {
return Promise.resolve(this.client.prefix);
}
/**
* Get the current time in the client's locale.
* @returns A promise that resolves to the current time.
*/
private getCurrentTime(): Promise<string> {
return Promise.resolve(dayjs(new Date().getTime()).tz().format('hh:mm:ss A'));
}
/**
* Get the owner's name. If no owner is specified then it returns `undefined`.
* @returns A promise that resolves to the owner's name.
*/
private getOwnerName(): Promise<string> {
return Promise.resolve(this.client.owner?.username || 'undefined');
}
/**
* Get the client's name. If the client's user is not ready yet, then it returns `undefined`.
* @returns A promise that resolves to the client's name.
*/
private getClientName(): Promise<string> {
return Promise.resolve(this.client.user?.username || 'undefined');
}
/**
* Get the client's uptime since last ready event emitted in a human-readable shape.
* Returns `null` if no uptime is available.
* @returns A promise that resolves to the client's uptime.
*/
private getUptime(): Promise<string> {
if (!this.client.uptime) {
return Promise.resolve('null');
}
const uptime = humanizeDuration(this.client.uptime, {
largest: 3,
units: ['d', 'h', 'm'],
round: true,
conjunction: ' and ',
serialComma: false
});
return Promise.resolve(uptime);
}
/**
* Get the client's time at which the last ready event was emitted. Returns `null`
* if no `readyAt` timestamp is available.
* @returns A promise that resolves to the time the client has gone ready.
*/
private getReadyTime(): Promise<string> {
if (!this.client.readyTimestamp) {
return Promise.resolve('null');
}
return Promise.resolve(dayjs(this.client.readyTimestamp).tz().format('ddd, DD/MM/YY @HH:MM:A'));
}
/**
* Get the total number of members across all the guilds that the client is connected to.
* @returns A promise that resolves to the number of [members](https://old.discordjs.dev/#/docs/discord.js/main/class/GuildMember).
*/
private getNumberOfMembers(): Promise<string> {
if (!this.client.shard) {
return Promise.resolve(this.client.guilds.cache.reduce((sum, guild) => sum + guild.memberCount, 0).toString());
}
return this.client.shard.fetchClientValues('guilds.cache')
.then((results) => {
const castedResults = results as Collection<Snowflake, Guild>[];
return castedResults.reduce((sum, guildCache) => {
const memberCounts = guildCache.reduce((sum: number, guild: Guild) => sum + guild.memberCount, 0);
return sum + memberCounts;
}, 0).toString();
});
}
/**
* Get the number of commands registered to this client.
* @returns A promise that resolves to the number of commands.
*/
private getNumberOfCommands(): Promise<string> {
return Promise.resolve(this.client.registry.commands.size.toString());
}
}