-
Notifications
You must be signed in to change notification settings - Fork 13
/
interactionCreate.js
148 lines (132 loc) · 4.28 KB
/
interactionCreate.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
import { ApplicationCommandType, GuildMember } from "discord.js";
import warn from "../common/moderation/warns.js";
import { censor, badWordsAllowed } from "../common/moderation/automod.js";
import { getWarns } from "../commands/view-warns.js";
import CONSTANTS from "../common/CONSTANTS.js";
import logError from "../util/logError.js";
import { importScripts } from "../util/files.js";
import path from "path";
import url from "url";
import { guessAddon } from "../commands/guess-addon.js";
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
const commands =
await /** @type {typeof importScripts<import("../common/types/command").default>} */
(importScripts)(path.resolve(dirname, "../commands"));
/** @type {import("../common/types/event").default<"interactionCreate">} */
export default async function event(interaction) {
if (!interaction.isRepliable()) return;
try {
if (interaction.isButton()) {
if (interaction.customId.endsWith("_strike")) {
if (!(interaction.member instanceof GuildMember))
throw new TypeError("interaction.member is not a GuildMember");
await getWarns(
(data) => interaction.reply(data),
interaction.member,
interaction.customId.split("_strike")[0],
);
return;
}
if (interaction.customId.endsWith("_reaction_role")) {
if (!(interaction.member instanceof GuildMember))
throw new TypeError("interaction.member is not a GuildMember");
const roleId = interaction.customId.split("_reaction_role")[0];
if (!roleId)
throw new SyntaxError(
"Button customId ends in _reaction_role but no role ID was given",
);
const role = interaction.member.roles.resolve(roleId);
if (role) {
await interaction.member.roles.remove(role);
await interaction.reply({
ephemeral: true,
content: `${
CONSTANTS.emojis.statuses.yes
} Removed ${role.toString()} from you!`,
});
} else {
await interaction.member.roles.add(roleId);
await interaction.reply({
ephemeral: true,
content: `${CONSTANTS.emojis.statuses.yes} Gave you <@&${roleId}>!`,
});
}
return;
}
}
if (interaction.isModalSubmit()) {
if (interaction.customId.startsWith("guessModal.")) {
return await guessAddon(interaction);
}
}
if (!interaction.isCommand()) return;
const commandPromise = commands.get(interaction.commandName);
const command = await commandPromise?.();
if (!command) throw new ReferenceError(`Command \`${interaction.commandName}\` not found`);
const commandType = command.data.toJSON().type;
if (
commandType === ApplicationCommandType.ChatInput &&
("censored" in command && command.censored === "channel"
? !badWordsAllowed(interaction.channel)
: ("censored" in command && command.censored) ?? true)
) {
const censored = censorOptions(interaction.options.data);
if (censored.isBad) {
await Promise.all([
interaction.reply({
ephemeral: true,
content: `${CONSTANTS.emojis.statuses.no} Language!`,
}),
warn(
interaction.member instanceof GuildMember
? interaction.member
: interaction.user,
`Watch your language!`,
censored.strikes,
`Used command:\n${interaction.toString()}`,
),
]);
return;
}
}
// @ts-expect-error -- No concrete fix to this
await command.interaction(interaction);
} catch (error) {
logError(error, interaction.toString());
await (interaction.replied
? interaction.followUp
: interaction[interaction.deferred ? "editReply" : "reply"])({
ephemeral: true,
content: `${CONSTANTS.emojis.statuses.no} An error occurred.`,
embeds: [],
components: [],
files: [],
});
}
}
/** @param {readonly import("discord.js").CommandInteractionOption[]} options */
function censorOptions(options) {
let strikes = 0,
isBad = false,
/** @type {string[]} */
words = [];
options.forEach((option) => {
if (typeof option.value === "string") {
const censored = censor(option.value);
if (censored) {
isBad = true;
strikes += censored.strikes;
words.push(option.value);
}
}
if (option.options) {
const censored = censorOptions(option.options);
if (censored.isBad) {
isBad = true;
strikes += censored.strikes;
words.push(...censored.words);
}
}
});
return { isBad, strikes, words };
}