-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
132 lines (111 loc) · 5.12 KB
/
plugin.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var format = require("util").format;
var inspect = require("util").inspect;
module.exports = {
name: "repeat",
requiresRoles: ["admin"],
init: function (client, deps) {
// struct repeatState {
// interval: i32,
// channel: channel
// message: Option<String>
// timeout: Option<Timeout>
// }
var interval;
var channel;
var message;
var timeout;
function stopRepeat () {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
client.debug("RepeatPlugin", "Repeat cleared.");
} else {
client.debug("RepeatPlugin", "Tried to clear repeat, but no repeat set.");
}
}
function startRepeat () {
// TODO(Havvy): Is this one necessary?
stopRepeat();
function repeat () {
console.log(format("REPEATING at %s", Math.floor(Date.now() / 1000) % 600));
// ASSUME: Client is in specified channel.
client.say(channel, message);
timeout = setTimeout(repeat, interval * 1e3);
}
timeout = setTimeout(repeat, interval * 1e3);
}
// These values are in seconds.
interval = (Math.ceil(Number(client.config("repeat-interval"))) || 600);
if (interval < 0 || Number.isNaN(interval) || interval === Infinity) {
throw new Error("Configuration error: repeat-interval must be a positive finite number of seconds, or not defined (defaulting to 10 minutes");
}
message = client.config("repeat-message");
if (typeof message !== "string" || message === "") {
if (isRepeating) {
throw new Error("Configuration error: repeat-autostart is true while repeat-message is not set.");
}
message = undefined;
}
channel = client.config("repeat-channel");
if (typeof channel !== "string" || channel[0] !== "#") {
throw new Error("Configuration error: repeat-channel must be a channel.");
}
if (client.config("repeat-autostart")) {
startRepeat();
}
var requiresAdmin = deps.admin.requiresAdmin;
return {
handlers: {
"!repeat": requiresAdmin(function (command) {
var subcommands = {
"message": function (command) {
var newMessage = command.args.slice(1).join(" ");
if (message === "") {
return "This subcommand is `repeat message <new-message>` and you did not supply a new message.";
}
message = newMessage;
return "Message changed. This message is only changed until I am restarted.";
},
"interval": function (command) {
var newInterval = Math.ceil(Number(command.args[1]));
if (newInterval <= 0 || Number.isNaN(newInterval) || newInterval === Infinity) {
return "Cannot set interval to that value. Give a positive number of seconds.";
}
interval = newInterval;
return "Interval changed. This interval is only changed until I am restarted.";
},
"start": function (command) {
if (timeout) {
return "Message repeat functionality is already running.";
} else if (!message) {
return "No message to repeat. Use !repeat message <new-message> to set the repeat message first.";
} else {
startRepeat();
return format("Starting repeating message in %s. Interval = %s seconds. Message = %s.", channel, interval, message);
}
},
"stop": function (command) {
if (!timeout) {
return "Message repeat functionality is already stopped.";
} else {
stopRepeat();
return "Message repeat functionality has been stopped.";
}
}
};
var subcommand = command.args[0];
if (subcommands[subcommand]) {
return subcommands[subcommand](command);
} else {
return "Unknown subcommand. Available options are 'message', 'interval', 'start', 'stop'.";
}
}),
"error": function (quitMessage) {
stopRepeat();
}
},
"help": require("help.json"),
"commands": ["repeat"]
};
}
};