-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathLockdownManager.ts
73 lines (57 loc) · 1.83 KB
/
LockdownManager.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
import type { AccurateTimeout } from '#utils/Timers';
import type { GuildTextBasedChannelTypes } from '@sapphire/discord.js-utilities';
import { Collection, type Role } from 'discord.js';
export class LockdownManager extends Collection<string, Collection<string, LockdownManager.Entry>> {
public add(role: Role, channel: LockdownManager.Channel, value: LockdownManager.Entry) {
const roles = this.acquire(channel);
roles.get(role.id)?.timeout?.stop();
roles.set(role.id, value);
}
public remove(role: Role): this;
public remove(role: Role, channel: LockdownManager.Channel): boolean;
public remove(role: Role, channel?: LockdownManager.Channel) {
if (channel === undefined) return this.removeRole(role);
const channels = this.get(channel.id);
if (channels === undefined) return false;
const entry = channels.get(role.id);
if (entry === undefined) return false;
entry.timeout?.stop();
channels.delete(role.id);
if (channels.size === 0) {
super.delete(channel.id);
}
return true;
}
public override delete(id: string) {
const roles = this.get(id);
if (roles === undefined) return false;
for (const role of roles.values()) {
role.timeout?.stop();
}
return super.delete(id);
}
public acquire(channel: LockdownManager.Channel) {
let collection = this.get(channel.id);
if (collection === undefined) {
collection = new Collection();
this.set(channel.id, collection);
}
return collection;
}
private removeRole(role: Role) {
for (const channel of this.values()) {
const entry = channel.get(role.id);
if (entry === undefined) continue;
entry.timeout?.stop();
channel.delete(role.id);
}
return this;
}
}
export namespace LockdownManager {
export type Channel = GuildTextBasedChannelTypes;
export interface Entry {
allowed: boolean | null;
timeout: AccurateTimeout | null;
}
}