/
helper.ts
110 lines (93 loc) · 2.87 KB
/
helper.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
import { BotCommand } from './BotCommand';
import { type BotCommandContext } from './BotCommandContext';
/**
* Options for a bot command.
*/
export interface CreateBotCommandOptions {
/**
* The cooldown of the command for everyone, in seconds.
*/
globalCooldown?: number;
/**
* The cooldown of the command per user, in seconds.
*/
userCooldown?: number;
/**
* The interval in which expired cooldown data should be cleared, in seconds. Defaults to 10 minutes.
*/
cooldownCleanupRate?: number;
/**
* Alternative names for the command that can be used to call it.
*/
aliases?: string[];
}
/**
* Creates a simple bot command.
*
* @meta category main
*
* @expandParams
*
* @param commandName The name of the command.
* @param handler The execution handler that should be called when the command is sent.
* @param options
*/
export function createBotCommand(
commandName: string,
handler: (params: string[], context: BotCommandContext) => void | Promise<void>,
options: CreateBotCommandOptions = {},
): BotCommand {
return new (class extends BotCommand {
name = commandName;
private readonly _allowedExecutionPerChannel = new Map<string, number>();
private readonly _allowedExecutionPerChannelUser = new Map<string, number>();
constructor(private readonly _options: CreateBotCommandOptions) {
super();
setInterval(() => {
const now = Date.now();
for (const [key, time] of this._allowedExecutionPerChannel) {
if (now > time) {
this._allowedExecutionPerChannel.delete(key);
}
}
for (const [key, time] of this._allowedExecutionPerChannelUser) {
if (now > time) {
this._allowedExecutionPerChannelUser.delete(key);
}
}
}, (this._options.cooldownCleanupRate ?? 600) * 1000).unref();
}
get aliases() {
return options.aliases ?? [];
}
canExecute(channelId: string, userId: string): boolean {
const now = Date.now();
if (options.globalCooldown) {
const globalAllowedExecutionTime = this._allowedExecutionPerChannel.get(channelId);
if (globalAllowedExecutionTime !== undefined && now < globalAllowedExecutionTime) {
return false;
}
}
if (options.userCooldown) {
const userAllowedExecutionTime = this._allowedExecutionPerChannelUser.get(`${channelId}:${userId}`);
if (userAllowedExecutionTime !== undefined && now < userAllowedExecutionTime) {
return false;
}
}
return true;
}
async execute(params: string[], context: BotCommandContext) {
const now = Date.now();
if (this._options.globalCooldown) {
this._allowedExecutionPerChannel.set(context.broadcasterId, now + this._options.globalCooldown * 1000);
}
if (this._options.userCooldown) {
this._allowedExecutionPerChannelUser.set(
`${context.broadcasterId}:${context.userId}`,
now + this._options.userCooldown * 1000,
);
}
await handler(params, context);
}
})(options);
}