-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathModerationManagerEntry.ts
362 lines (322 loc) · 9.36 KB
/
ModerationManagerEntry.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import type { ModerationData } from '#lib/database';
import { LanguageKeys } from '#lib/i18n/languageKeys';
import type { ScheduleEntry } from '#lib/schedule';
import { minutes } from '#utils/common';
import { TypeMetadata, type TypeVariation } from '#utils/moderationConstants';
import { UserError, container } from '@sapphire/framework';
import { isNullishOrZero } from '@sapphire/utilities';
import type { Guild, Snowflake, User } from 'discord.js';
/**
* Represents a moderation manager entry.
*/
export class ModerationManagerEntry<Type extends TypeVariation = TypeVariation> {
/**
* The ID of this moderation entry.
*/
public readonly id: number;
/**
* The timestamp when the moderation entry was created.
*/
public readonly createdAt: number;
/**
* The duration of the moderation entry since the creation.
*
* @remarks
*
* The value can be updated to add or remove the duration.
*/
public duration!: number | null;
/**
* The extra data of the moderation entry.
*/
public readonly extraData: ExtraDataTypes[Type];
/**
* The guild where the moderation entry was created.
*/
public readonly guild: Guild;
/**
* The ID of the moderator who created the moderation entry.
*/
public readonly moderatorId: Snowflake;
/**
* The ID of the user who is the target of the moderation entry.
*/
public readonly userId: Snowflake;
/**
* The reason of the action in the moderation entry.
*
* @remarks
*
* The value can be updated to add or remove the reason.
*/
public reason: string | null;
/**
* The image URL of the moderation entry.
*
* @remarks
*
* The value can be updated to add or remove the image URL.
*/
public imageURL: string | null;
/**
* The type of the moderation entry.
*/
public readonly type: Type;
/**
* The metadata of the moderation entry.
*
* @remarks
*
* The metadata is a bitfield that contains the following information:
* - `1 << 0`: The moderation entry is an undo action.
* - `1 << 1`: The moderation entry is temporary.
* - `1 << 3`: The moderation entry is archived.
*
* The value can be updated adding or removing any of the aforementioned
* flags.
*/
public metadata: TypeMetadata;
#moderator: User | null;
#user: User | null;
#cacheExpiresTimeout = Date.now() + minutes(15);
/**
* Constructs a new `ModerationManagerEntry` instance.
*
* @param data - The data to initialize the entry.
*/
public constructor(data: ModerationManagerEntry.Data<Type>) {
this.id = data.id;
this.createdAt = data.createdAt;
this.extraData = data.extraData;
this.guild = data.guild;
this.reason = data.reason;
this.imageURL = data.imageURL;
this.type = data.type;
this.metadata = data.metadata;
this.#setDuration(data.duration);
if (typeof data.moderator === 'string') {
this.#moderator = null;
this.moderatorId = data.moderator;
} else {
this.#moderator = data.moderator;
this.moderatorId = data.moderator.id;
}
if (typeof data.user === 'string') {
this.#user = null;
this.userId = data.user;
} else {
this.#user = data.user;
this.userId = data.user.id;
}
}
/**
* Creates a new instance of `ModerationManagerEntry` with the same property values as the current instance.
*/
public clone() {
return new ModerationManagerEntry(this.toData());
}
/**
* Updates the moderation entry with the given data.
*
* @remarks
*
* This method does not update the database, it only updates the instance
* with the given data, and updates the cache expiration time.
*
* @param data - The data to update the entry.
*/
public patch(data: ModerationManagerEntry.UpdateData) {
if (data.duration !== undefined) this.#setDuration(data.duration);
if (data.reason !== undefined) this.reason = data.reason;
if (data.imageURL !== undefined) this.imageURL = data.imageURL;
if (data.metadata !== undefined) this.metadata = data.metadata;
this.#cacheExpiresTimeout = Date.now() + minutes(15);
}
/**
* The scheduled task for this moderation entry.
*/
public get task() {
return container.client.schedules.queue.find((task) => this.#isMatchingTask(task)) ?? null;
}
/**
* The timestamp when the moderation entry expires, if any.
*
* @remarks
*
* If {@linkcode duration} is `null` or `0`, this property will be `null`.
*/
public get expiresTimestamp() {
return isNullishOrZero(this.duration) ? null : this.createdAt + this.duration;
}
/**
* Whether the moderation entry is expired.
*
* @remarks
*
* If {@linkcode expiresTimestamp} is `null`, this property will always be
* `false`.
*/
public get expired() {
const { expiresTimestamp } = this;
return expiresTimestamp !== null && expiresTimestamp < Date.now();
}
/**
* Whether the moderation entry is cache expired, after 15 minutes.
*
* @remarks
*
* This property is used to determine if the entry should be removed from
* the cache, and will be updated to extend the cache life when
* {@linkcode patch} is called.
*/
public get cacheExpired() {
return this.#cacheExpiresTimeout < Date.now();
}
/**
* Checks if the entry is an undo action.
*/
public isUndo() {
return (this.metadata & TypeMetadata.Undo) === TypeMetadata.Undo;
}
/**
* Checks if the entry is temporary.
*/
public isTemporary() {
return (this.metadata & TypeMetadata.Temporary) === TypeMetadata.Temporary;
}
/**
* Checks if the entry is archived.
*/
public isArchived() {
return (this.metadata & TypeMetadata.Archived) === TypeMetadata.Archived;
}
/**
* Checks if the entry is completed.
*/
public isCompleted() {
return (this.metadata & TypeMetadata.Completed) === TypeMetadata.Completed;
}
/**
* Fetches the moderator who created the moderation entry.
*/
public async fetchModerator() {
return (this.#moderator ??= await container.client.users.fetch(this.moderatorId));
}
/**
* Fetches the target user of the moderation entry.
*/
public async fetchUser() {
return (this.#user ??= await container.client.users.fetch(this.userId));
}
/**
* Returns a clone of the data for this moderation manager entry.
*/
public toData(): ModerationManagerEntry.Data<Type> {
return {
id: this.id,
createdAt: this.createdAt,
duration: this.duration,
extraData: this.extraData,
guild: this.guild,
moderator: this.moderatorId,
user: this.userId,
reason: this.reason,
imageURL: this.imageURL,
type: this.type,
metadata: this.metadata
};
}
public toJSON() {
return {
id: this.id,
createdAt: this.createdAt,
duration: this.duration,
extraData: this.extraData,
guildId: this.guild.id,
moderatorId: this.moderatorId,
userId: this.userId,
reason: this.reason,
imageURL: this.imageURL,
type: this.type,
metadata: this.metadata
};
}
#isMatchingTask(task: ScheduleEntry) {
return (
task.data !== null && //
'caseID' in task.data &&
task.data.caseID === this.id &&
task.data.guildID === this.guild.id
);
}
#setDuration(duration: bigint | number | null) {
if (typeof duration === 'bigint') duration = Number(duration);
if (isNullishOrZero(duration)) {
this.duration = null;
this.metadata &= ~TypeMetadata.Temporary;
} else {
this.duration = duration;
this.metadata |= TypeMetadata.Temporary;
}
}
public static from(guild: Guild, entity: ModerationData) {
if (guild.id !== entity.guildId) {
throw new UserError({ identifier: LanguageKeys.Arguments.CaseNotInThisGuild, context: { parameter: entity.caseId } });
}
return new this({
id: entity.caseId,
createdAt: entity.createdAt ? entity.createdAt.getTime() : Date.now(),
duration: entity.duration,
extraData: entity.extraData as any,
guild,
moderator: entity.moderatorId,
user: entity.userId!,
reason: entity.reason,
imageURL: entity.imageURL,
type: entity.type,
metadata: entity.metadata
});
}
}
export namespace ModerationManagerEntry {
export interface Data<Type extends TypeVariation = TypeVariation> {
id: number;
createdAt: number;
duration: bigint | number | null;
extraData: ExtraData<Type>;
guild: Guild;
moderator: User | Snowflake;
user: User | Snowflake;
reason: string | null;
imageURL: string | null;
type: Type;
metadata: TypeMetadata;
}
export type CreateData<Type extends TypeVariation = TypeVariation> = MakeOptional<
Omit<Data<Type>, 'id' | 'guild' | 'createdAt'>,
'duration' | 'imageURL' | 'extraData' | 'metadata' | 'moderator' | 'reason'
>;
export type UpdateData<Type extends TypeVariation = TypeVariation> = Partial<
Omit<Data<Type>, 'id' | 'createdAt' | 'extraData' | 'moderator' | 'user' | 'type' | 'guild'>
>;
export type ExtraData<Type extends TypeVariation = TypeVariation> = ExtraDataTypes[Type];
}
type MakeOptional<Type, OptionalKeys extends keyof Type> = Omit<Type, OptionalKeys> & Partial<Pick<Type, OptionalKeys>>;
interface ExtraDataTypes {
[TypeVariation.Ban]: null;
[TypeVariation.Kick]: null;
[TypeVariation.Mute]: Snowflake[];
[TypeVariation.Softban]: null;
[TypeVariation.VoiceKick]: null;
[TypeVariation.VoiceMute]: null;
[TypeVariation.Warning]: null;
[TypeVariation.RestrictedReaction]: null;
[TypeVariation.RestrictedEmbed]: null;
[TypeVariation.RestrictedAttachment]: null;
[TypeVariation.RestrictedVoice]: null;
[TypeVariation.SetNickname]: { oldName: string | null };
[TypeVariation.RoleAdd]: { role: Snowflake };
[TypeVariation.RoleRemove]: { role: Snowflake };
[TypeVariation.RestrictedEmoji]: null;
[TypeVariation.Timeout]: null;
}