-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugins-parser.js
76 lines (63 loc) · 2.7 KB
/
plugins-parser.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
'use strict';
class _Plugins {
constructor() {
// idk
}
random(base, start, decimals) {
decimals = parseInt(decimals) || 0;
start = parseInt(start) || 0;
base = parseInt(base) || 0;
let diff = Math.abs(base - start);
let res = Math.random() * diff + (start > base ? base : start);
return res.toFixed(decimals);
}
runCustomCommand(target, room, user, customCommand, pm, levelsDeep) {
let parts = target.split(",").map(p => p.trim());
this.canBroadcast = user.hasRank(room, customCommand.rank);
customCommand.text.forEach(text => {
let reply = text
// custom arguments
// the entire thing
.replace(/{arg}/g, target)
.replace(/{arg\[(?:(?!\]\}).)+\]}/g, m => {
if (m.includes(",")) {
let subparts = m.match(/\[(?:(?!\]\}).)+\]/)[0].slice(1, -1).split(",");
let [min, max, joiner] = subparts;
min = parseInt(min);
max = parseInt(max);
if (!min) min = 0;
if (isNaN(max)) max = parts.length;
joiner = joiner.replace(/^\s(?!$)/, "");
if (!joiner) joiner = ", ";
return parts.slice(min, max).join(joiner);
} else {
let index = m.replace(/[^0-9]/g, "");
return parts[index] || "";
}
})
// preset randoms
.replace(/{choose\[(?:(?!\]\}).)+\]}/g, m => {
let subparts = m.match(/\[(?:(?!\]\}).)+\]/)[0].slice(1, -1).split(",").map(p => p.trim());
return subparts[Math.floor(Math.random() * subparts.length)] || "";
})
.replace(/{rand\[(?:(?!\]\}).)+\]}/g, m => {
let subparts = m.match(/\[(?:(?!\]\}).)+\]/)[0].slice(1, -1).split(",").map(p => p.trim());
return this.random(...subparts);
})
// preset randoms
.replace(/{pick}/g, m => {
return parts[Math.floor(Math.random() * parts.length)];
})
// users
.replace(/{by}/g, user.name)
.replace(/{arg\/by}/g, target || user.name);
if (/^{parse}/i.test(reply)) {
let cmd = reply
.replace(/^{parse}[\s]?/i, "");
return this.parse(cmd);
}
this.send(reply);
});
}
}
exports.Plugins = new _Plugins();