mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-24 18:15:02 +00:00
initial - tempbans & mutes
This commit is contained in:
parent
c84d1a0be1
commit
744b9273bb
14 changed files with 322 additions and 109 deletions
40
backend/src/utils/timers.ts
Normal file
40
backend/src/utils/timers.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { Snowflake } from "discord-api-types";
|
||||
|
||||
type TimerCallback = (key: string, expiry: number) => void;
|
||||
|
||||
type TimerOptions = {
|
||||
key: Snowflake;
|
||||
guildId?: Snowflake;
|
||||
expiry: number;
|
||||
plugin?: string;
|
||||
callback: TimerCallback;
|
||||
};
|
||||
|
||||
export class ExpiringTimer {
|
||||
done: boolean = false;
|
||||
options: TimerOptions;
|
||||
timeout?: NodeJS.Timeout;
|
||||
data?: any; // idk how to make this take generic <T> typings data
|
||||
isValid() {
|
||||
return !this.done;
|
||||
}
|
||||
private execute() {
|
||||
if (!this.isValid()) return;
|
||||
this.options.callback(this.options.key, this.options.expiry);
|
||||
this.done = true;
|
||||
}
|
||||
init() {
|
||||
if (this.timeout) this.clear();
|
||||
this.timeout = setTimeout(() => this.execute(), this.options.expiry);
|
||||
}
|
||||
clear() {
|
||||
if (this.timeout) {
|
||||
clearTimeout(this.timeout);
|
||||
this.timeout = undefined;
|
||||
}
|
||||
}
|
||||
constructor(options: TimerOptions) {
|
||||
this.options = options;
|
||||
this.init();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue