forked from PSWiFi/PipBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessagehandler.js
340 lines (312 loc) · 11.9 KB
/
messagehandler.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
const dotenv = require("dotenv");
dotenv.config();
const { BOT_USERNAME: username } = process.env;
const FORMATTING_CHARS = ["*", "_", "`", "~", "^", "\\"];
const DEFAULT_MESSAGE = `Hi, I'm ${username}! I'm a Bot for the WiFi room - my prefix is \`\`${config.prefix}\`\`. For support, please contact a staff member.`;
const CANNOT_BE_USED_IN_PM = "This command can only be used in a room.";
async function handleMessage(message, client, DB) {
if (message.author?.name) {
if (message.isIntro || message.author?.name === client.status.username)
return;
if (message.content === "%...%")
console.log(message, message.author, message.target);
if (!message.content.startsWith(config.prefix)) {
if (message.type === "pm" && message.author?.name)
message.reply(DEFAULT_MESSAGE);
return;
}
if (
FORMATTING_CHARS.includes(config.prefix) &&
message.content.startsWith(config.prefix.repeat(2))
)
return; // Don't try and interpret formatting as a command
if (
message.command === "/raw" &&
message.content?.includes("</span> sent you a friend request!")
)
return;
const checkPerms = getCheckPerms(message);
const args = message.content.substr(config.prefix.length).split(" ");
const command = args.shift().toLowerCase().trim();
try {
switch (command) {
// Make sure to run a checkPerms on everything!
// Also would recommend using checkPerms('chatvoice') for broadcasting stuff
// since it uses the displayed rank (higher of room and global rank)
// Also remember to add a break after every command
// Yes I could've used modular functions but I'm lazy okay
case "kill":
case "restart": // Technically this command ends the process, but PipBot's VPS has a cron job set up to instantly restart the process
checkPerms("roommod");
process.exit(0);
case "hangman":
checkPerms("chatvoice");
if (message.type !== "chat")
throw new ChatError(CANNOT_BE_USED_IN_PM);
message.reply("/hangman random");
break;
// We're using both addwp and addhwp as the same command; the line
// with useHelperPoints is what makes them slightly different
case "addwp":
case "addhwp":
case "addpp":
case "addrizz":
case "removepp":
case "removewp":
case "removehwp":
case "removerizz":
checkPerms("chatvoice");
// Remove the next line if you want to let staff use this in DMs
if (message.type !== "chat")
throw new ChatError(CANNOT_BE_USED_IN_PM);
let rizz = command.includes("rizz");
const params = args
.join(" ")
.split(",")
.map((param) => param.trim());
let [amt, ...extra] = params.filter((param) => /^-?\d+$/.test(param));
if (extra.length)
throw new ChatError(
`Please provide only 1 number (received: ${extra + 1})`
);
// You can also make this '1' or something instead
if (!amt || parseInt(amt) === 0)
throw new ChatError("Please provide the number of points to add.");
const users = params.filter((param) => /[a-z]/i.test(param));
const useHelperPoints = command.includes("hwp");
const remove = command.includes("remove") || amt < 0;
if (remove) amt = Math.abs(amt) * -1;
await Promise.all(
users.map((user) =>
DB.addPoints(
user,
config.mainRoom,
parseInt(amt),
useHelperPoints ? 1 : 0,
useHelperPoints ? 150 : 10_000
)
)
);
// TODO: Probably make this a Promise.allSettled and display results instead
// await DB.bulkAddPoints(users, config.mainRoom, parseInt(amt));
message.reply(
`${Math.abs(amt)} ${rizz ? "rizz" : "point"}${
Math.abs(amt) === 1 && !rizz ? "" : "s"
} ${remove ? "removed from" : "awarded to"} ${users.join(", ")}.`
);
break;
case "wp":
case "viewwp":
case "rizz":
case "viewrizz":
if (message.type === "chat") checkPerms("chatvoice");
let rz = command.includes("rizz");
const user = args.length
? toId(args.join(""))
: message.author.userid;
try {
const {
name,
points: [wp, hp = 0],
} = await DB.getPoints(user);
if (rz) {
message.reply(`${name} has ${wp + hp} rizz.`);
} else {
message.reply(
`${name} has ${wp + hp} point${
Math.abs(wp + hp) === 1 ? "" : "s"
}${hp ? ` - ${wp}WP and ${hp}HWP` : ""}.`
);
}
} catch (err) {
throw new ChatError(
`That user doesn't have any ${rz ? "rizz" : "points"}...`
);
}
break;
case "reset":
case "resetwp":
case "fanumtax":
checkPerms("roomdriver"); // Maybe make this roommod? Perms are up to you
// Remove the next line if you want to let staff use this in DMs
if (message.type !== "chat")
throw new ChatError(CANNOT_BE_USED_IN_PM);
message.reply(
"Are you sure you want to reset the leaderboard? Type 'confirm' to confirm within the next 10 seconds."
);
try {
await message.target.waitFor((msg) => {
return (
msg.author.userid === message.author.userid &&
toId(msg.content) === "confirm"
);
}, 10_000);
message.reply("Resetting points, please wait...");
await DB.resetPoints(config.mainRoom, [15, 0]);
message.reply("Points have been reset!");
} catch {
message.reply("Time expired.");
}
break;
case "monthly":
if (message.type === "pm") {
// Set the value of the monthly
checkPerms("roomowner");
const tourDetails = args.join(" ").trim();
if (!tourDetails)
throw new ChatError("Please provide a format for the tour.");
let tmp = tourDetails.split(",");
const format = tmp.shift();
const rules = tmp.join(", ");
CACHE.tourDetails = tourDetails;
DB.setTourDetails(tourDetails);
message.reply(
`Set monthly tour to: \`\`${format}\`\`. Please ensure there are no typos in the format string or creating the tour will fail!`
);
if (rules?.length > 0) message.reply(`Added rules: ${rules}`);
} else if (message.type === "chat") {
// Creating a monthly tour
checkPerms("roomvoice");
if (!CACHE?.tourDetails?.value)
CACHE.tourDetails = await DB.getTourDetails();
if (!CACHE?.tourDetails?.value) break;
let tmp = CACHE.tourDetails.value.split(",");
const format = tmp.shift();
const rules = tmp.join(", ");
message.reply(
`/modnote Attempting to create a ${format} tour. If it is unsuccessful, please verify the format is valid and get a Room Owner or higher to re-set it by using ${config.prefix}monthly FormatName in PMs with the bot.`
);
message.reply(`/tour create ${format}, elimination`);
if (rules?.length > 0) message.reply(`/tour rules ${rules}`);
message.reply("/tour autostart 5");
message.reply("/tour autodq 2");
message.reply("/tour scouting disallow");
}
break;
case "say":
if (!config.developers.includes(message.author.userid))
checkPerms("roomowner");
message.reply(args.join(" "));
break;
case "sayroom":
if (!config.developers.includes(message.author.userid))
throw new ChatError("You lack permission to use this command.");
let room = toId(args.shift());
if (!room || !args.length)
throw new ChatError(
`\`\`${config.prefix}sayroom room, message or command\`\``
);
client.send(`${room}|${args.join(" ").trim()}`);
break;
case "uptime":
const time = Math.floor(process.uptime());
let hours = Math.floor(time / 3600);
const mins = Math.floor((time - hours * 3600) / 60);
const secs = Math.floor(time - hours * 3600 - mins * 60);
const days = Math.floor(time / (60 * 60 * 24));
hours = hours % 24;
let str;
if (days > 0) {
str = [
`${days} day${days === 1 ? "" : "s"}`,
`${hours} hour${hours === 1 ? "" : "s"}`,
`${mins} minute${mins === 1 ? "" : "s"}`,
`and ${secs} second${secs === 1 ? "" : "s"}`,
];
} else if (hours > 0) {
str = [
`${hours} hour${hours === 1 ? "" : "s"}`,
`${mins} minute${mins === 1 ? "" : "s"}`,
`and ${secs} second${secs === 1 ? "" : "s"}`,
];
} else if (mins > 0) {
str = [
`${mins} minute${mins === 1 ? "" : "s"} and ${secs} second${
secs === 1 ? "" : "s"
}`,
];
} else {
str = [`${secs} second${secs === 1 ? "" : "s"}`];
}
message.reply(`${username} uptime: ${str.join(", ")}`);
break;
case "rejoin":
case "rj":
for (const room of config.rooms) {
client.send("|/j " + room);
}
break;
default:
throw new ChatError(DEFAULT_MESSAGE);
}
} catch (err) {
message.reply(err.message);
if (err.name !== "ChatError") console.log(err, err.name);
}
}
}
// You shouldn't need to touch the stuff below this
function getCheckPerms(message) {
const rankMap = {
"‽": -2,
"!": -1,
" ": 0,
"^": 0.5,
"+": 1,
"§": 1.5,
"*": 3,
"%": 2,
"@": 3,
"&": 4,
"#": 5,
"⛵": 1,
};
const aliases = {
voice: "+",
driver: "%",
mod: "@",
moderator: "@",
bot: "*",
owner: "#",
ro: "#",
admin: "&",
};
function aliasRank(rank) {
if (aliases[rank]) return aliases[rank];
else return rank;
}
function getRank(rank) {
return rankMap[aliasRank(rank)] ?? 0;
}
return function checkPerms(rankString, throwErr = true) {
if (config.developers.includes(message.author.id)) return true; // devs bypass permission checks
if (!rankString) throw new Error("Must pass a rank to checkPerms");
rankString = rankString.toLowerCase().replace(/ /g, "");
const rankRegex = /^(?:room|chat|global)/;
const level = rankString.match(rankRegex)?.toString();
if (!level) throw new Error("Rank must start with room/chat");
// 'room' checks for roomauth, 'chat' uses the rank shown in chat, 'global' uses the global rank
const rank = rankString.replace(rankRegex, "");
const requiredRank = getRank(rank);
const room = config.mainRoom; // You can use message.target.roomid if you want to use this elsewhere
const actualRank = getRank(
level === "room"
? Object.entries(message.parent.rooms.get(room)?.auth ?? {}).find(
([sym, list]) => {
return list.includes(message.author.userid);
}
)?.[0]
: level === "chat"
? message.msgRank
: level === "global"
? message.author.group
: null
);
if (actualRank >= requiredRank) return true;
if (throwErr) throw new ChatError("Insufficient permissions");
return false;
};
}
module.exports = {
handleMessage,
};