-
Notifications
You must be signed in to change notification settings - Fork 13
/
CommandLock.ts
104 lines (97 loc) · 3.72 KB
/
CommandLock.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
import { Lang } from '../localization/Lang';
import { Message } from 'discord.js';
import { BaseStrings as s } from '../localization/BaseStrings';
/**
* Class to be used by a command that should prevent others from using
* it when in use by anyone else. CommandLocks can define sibling commands
* by name that will also be locked when the command is locked.
*
* >To use this (or your own extension class) on a Command, create a new
* instance and assign it to your Command's `lock` field in your Command
* constructor.
*
* By default a command will be locked for a maximum of 30 seconds if
* something goes wrong and a command fails to finish. This can be
* changed by setting `lockTimeout` in your CommandInfo to a desired
* time in ms, or `0` to disable the lock timeout entirely.
*
* **NOTE:** Command locks only apply within a guild context. You must
* declare set `guildOnly` to `true` to be able to utilize a CommandLock
* for that command.
*
* >By extending this class and overriding `lock()`, `isLocked()`, `free()`,
* and `getError()` with your own definitions you can effectively define your own
* lock behavior. These methods will be passed the same Message context and
* arguments as your Command so you can define any behavior you desire, like
* locking multiple commands that operate on a given user while one of the
* commands is in use.
* @param {...string} siblings Associated commands to lock
*/
export class CommandLock
{
private _locks: { [guild: string]: boolean } = {};
/**
* Associated commands that will also be locked when the
* command is locked
* @type {string[]}
*/
public siblings: string[];
public constructor(...siblings: string[])
{
this.siblings = siblings;
}
/**
* Sets the lock
* @param {Message} message Message that triggered the command
* @param {any[]} args Arguments passed to the command
* @returns {void}
*/
// @ts-ignore - Args will be passed by the framework regardless
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public lock(message: Message, args: any[]): void
{
this._locks[message.guild!.id] = true;
}
/**
* Returns whether or not this lock is active
* @param {Message} message Message that triggered the command
* @param {any[]} args Arguments passed to the command
* @returns {boolean}
*/
// @ts-ignore - Args will be passed by the framework regardless
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public isLocked(message: Message, args: any[]): boolean
{
return this._locks[message.guild!.id] || false;
}
/**
* Frees this lock. Called automatically when the command finishes,
* or when the lockTimeout fires
* @param {Message} message Message that triggered the command
* @param {any[]} args Arguments passed to the command
* @returns {void}
*/
// @ts-ignore - Args will be passed by the framework regardless
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public free(message: Message, args: any[]): void
{
delete this._locks[message.guild!.id];
}
/**
* Get an error string describing the effects of this lock. Defaults
* to `'This command is currently in use.` for en_us.
*
* >When defining your own lock behavior, consider using details
* related to the conditions under which your commands are locked.
* @param {string} lang The language to use for the error string
* @param {Message} message Message that triggered the command
* @param {any[]} args Arguments passed to the command
* @returns {string}
*/
// @ts-ignore - Message and args will be passed by the framework regardless
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public getError(lang: string, message: Message, args: any[]): string
{
return Lang.res(lang, s.DISPATCHER_ERR_COMMAND_LOCKED);
}
}