-
Notifications
You must be signed in to change notification settings - Fork 8
/
spongebob.ts
55 lines (47 loc) · 1.94 KB
/
spongebob.ts
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
import {Listener} from "@ubccpsc310/bot-base";
import {Client, GuildMember, Message, MessageMentions} from "discord.js";
import {ConfigKey, getConfig} from "../util/Config";
import {isStudent} from "../util/Util";
const spongebob: Listener<"messageCreate"> = {
event: "messageCreate",
procedure: async (client: Client, message: Message) => {
if (message.author.bot) return;
if (!message.cleanContent.includes("?")) return;
if (!(await isStudent(message.author.id, message.guild))) return;
const {tas, mentionedCourseStaff} = getPunishableMentions(message.mentions);
if (!mentionedCourseStaff && tas.length === 0) return;
const withCourseStaffReplaced = replaceAllWith(
[getConfig(ConfigKey.courseStaffId)],
getConfig(ConfigKey.studentId),
spongebobbify(message.content),
);
const withTAsReplaced = replaceAllWith(
tas,
message.author.id,
withCourseStaffReplaced,
);
return message.channel.send(withTAsReplaced);
}
};
const replaceAllWith = (searches: string[], value: string, starter: string): string =>
searches.reduce<string>(
(accumulator, search): string => accumulator.replace(new RegExp(search, 'g'), value),
starter,
);
const getPunishableMentions = (mentions: MessageMentions) => {
const courseStaffId = getConfig(ConfigKey.courseStaffId);
const {members, roles} = mentions;
const tas = members
.filter((member: GuildMember) =>
member.roles.valueOf().has(courseStaffId))
.map((member) => member.id);
const mentionedCourseStaff = roles.has(courseStaffId);
return {tas, mentionedCourseStaff};
};
const spongebobbify = (message: string) =>
message.split("")
.map((char, index) =>
index % 2 === 0 ? char.toLowerCase() : char.toUpperCase())
.join("");
export {replaceAllWith};
export default spongebob;