forked from ar1ocker/SquadJS-Timer-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.js
More file actions
71 lines (62 loc) · 2.21 KB
/
timer.js
File metadata and controls
71 lines (62 loc) · 2.21 KB
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
import BasePlugin from "./base-plugin.js";
export default class Timer extends BasePlugin {
static get description() {
return "Time plugin";
}
static get defaultEnabled() {
return true;
}
static get optionsSpecification() {
return {
max_time: {
required: false,
description: "maximum timer time in minutes",
default: 30,
},
commands: {
required: false,
description: "list of commands",
default: ["timer", "time", "timer", "set", "set timer", "settimer"],
},
};
}
constructor(server, options, connectors) {
super(server, options, connectors);
this.warn = this.warn.bind(this);
}
async mount() {
for (const index in this.options.commands) {
this.server.on(`CHAT_COMMAND:${this.options.commands[index]}`, async (data) => {
if (data.player) {
let isTimerSet = false;
if (data.message) {
const time = parseInt(data.message.trim().split(" ").slice(-1)[0]);
if (time && time > 0 && time <= this.options.max_time) {
this.warn(data.player.steamID, `In ${time} minutes, we will remind you about: ${data.message.slice(0, -2)}`);
setTimeout(
() => this.warn(data.player.steamID, `You asked to be reminded: ${data.message.slice(0, -2)}`, 2),
time * 60 * 1000
);
isTimerSet = true;
}
}
if (!isTimerSet) {
this.warn(
data.player.steamID,
`How many minutes should we set the timer for (from 1 to ${this.options.max_time})?\n\nWrite the time at the end of the command\nFor example: !timer mbt 30`
);
}
}
});
}
}
async warn(playerID, message, repeat = 1, frequency = 5) {
for (let i = 0; i < repeat; i++) {
// 'repeat' is used so that the squad outputs all messages, not hiding them because they are identical
await this.server.rcon.warn(playerID, message + "\u{00A0}".repeat(i));
if (i !== repeat - 1) {
await new Promise((resolve) => setTimeout(resolve, frequency * 1000));
}
}
}
}