Repository navigation
Expand file tree
/
Copy pathlisteners.js
More file actions
211 lines (179 loc) · 6.14 KB
/
Copy pathlisteners.js
File metadata and controls
211 lines (179 loc) · 6.14 KB
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
import { ChannelType } from "discord.js";
import { interactionReply, messageReply } from "ewilib";
import { roleAdd, roleRemove } from "./admin/role.js";
import {
buttonHandler,
modalHandler,
selectMenuHandler,
} from "./commands/utils.js";
import {
addEmojiData,
addServerEmojiCount,
addServerStatsData,
clearURL,
isReleasedCommand,
removeReminder,
} from "./helpers/index.js";
import { COMMONS } from "./classes/commons.js";
import { readContentAndReact } from "./fun.js";
import { emojiInContentHandler, statsGifCount } from "./stats.js";
import { PERSONALITY } from "./classes/personality.js";
//#region Listeners
export const onInteractionCreate = (interaction) => {
if (interaction.isButton()) {
buttonHandler(interaction);
return;
}
if (interaction.isStringSelectMenu()) {
console.log("selectMenu interaction detected");
selectMenuHandler(interaction);
return;
}
if (interaction.isModalSubmit()) {
console.log("modal interaction received");
modalHandler(interaction);
return;
}
const client = interaction.client; //get client
if (interaction.isContextMenuCommand()) {
//context commands
const contextCommands = client.contextCommands; //get commands
const foundCommand = contextCommands.find(
(cmd) => cmd.command.name === interaction.commandName,
);
if (foundCommand) foundCommand.action(interaction); //if found command, execute its action
return;
}
const slashCommands = client.slashCommands;
if (interaction.isAutocomplete()) {
//interaction with autocomplete activated
const autoCompleteCommands = slashCommands.filter(
(cmd) => cmd.autocomplete,
); //get commands with autocomplete action
const foundCommand = autoCompleteCommands
? autoCompleteCommands.find(
(cmd) => cmd.command.name === interaction.commandName,
)
: null; //find command that fired onInteractionCreate
if (foundCommand && isReleasedCommand(foundCommand))
foundCommand.autocomplete(interaction);
else interaction.respond([]); //if not found, return no choices
} else if (interaction.isCommand()) {
//slash commands
const client = interaction.client; //get client
const slashCommands = client.slashCommands; //get commands
const foundCommand = slashCommands.find(
(cmd) => cmd.command.name === interaction.commandName,
);
if (foundCommand && isReleasedCommand(foundCommand))
foundCommand.action(interaction, "/");
//if found command, execute its action
else interactionReply(interaction, "La commande n'est pas disponible.");
}
};
export const onMessageCreate = async (message) => {
// Function triggered for each message sent
const { channel } = message;
if (channel.type === ChannelType.DM) return;
else {
const currentServer = COMMONS.fetchFromGuildId(channel.guildId);
onPublicMessage(message, currentServer);
}
};
export const onReactionAdd = async (messageReaction, user) => {
// Function triggered for each reaction added
const currentServer = COMMONS.fetchFromGuildId(
messageReaction.message.channel.guild.id,
);
const cmnShared = COMMONS.getShared();
//stats
const emote = messageReaction.emoji; //get emote
const emoteGuild = emote.guild ? emote.guild : null; //get emote guild if any
if (emoteGuild && currentServer.guildId === emoteGuild.id) {
//if is a guildEmote and belongs to current server, count
const db = messageReaction.client.db;
addEmojiData(db, user.id, emote.id); //user stat
addServerEmojiCount(db, emote.id); //server stat
return;
}
if (
currentServer.cosmeticRoleHandle.messageId === messageReaction.message.id
) {
roleAdd(messageReaction, currentServer, user);
return;
}
onRemoveReminderReaction(messageReaction, user, cmnShared);
};
export const onReactionRemove = async (messageReaction, user) => {
const currentServer = COMMONS.fetchFromGuildId(
messageReaction.message.channel.guild.id,
);
if (currentServer.cosmeticRoleHandle.messageId === messageReaction.message.id)
await roleRemove(messageReaction, currentServer, user);
};
export const onThreadCreate = (thread, newlyCreated) => {
if (!newlyCreated) return;
if (thread.joinable && !thread.joined) thread.join();
};
//#endregion
//#region Listeners helpers
const onPublicMessage = (message, currentServer) => {
const { author } = message;
if (
author.id === process.env.CLIENTID || // ignoring message from himself
!currentServer || // ignoring if wrong guild
(process.env.DEBUG === "yes" && currentServer.name === "prod") // ignoring if debug && prod
)
return;
if (
message.attachments.size &&
message.channel.id === currentServer.catsThreadId
)
addServerStatsData(message.client.db, "cats");
readContentAndReact(message, currentServer);
statsGifCount(message);
emojiInContentHandler(message);
setTimeout(() => clearURL(message), 1500);
};
export const onRemoveReminderReaction = (
messageReaction,
reactionUser,
cmnShared,
) => {
const { removeEmoji } = cmnShared;
const { message, emoji, users, client } = messageReaction;
const foundReminder = client.remindme.find(
// found corresponding reminder message
({ botMessage }) => botMessage.id === message.id,
);
if (
foundReminder &&
emoji.name === removeEmoji &&
(message.interaction
? reactionUser.id === message.interaction.user.id
: users.cache
.map((user) => user.id)
.includes(message.mentions.users.first().id)) // if user reacting is the owner of reminder
) {
try {
client.remindme = client.remindme.filter(({ botMessage, timeout }) => {
if (botMessage.id === message.id) {
// if it is the right message
clearTimeout(timeout); //cancel timeout
removeReminder(client.db, botMessage.id);
const payload = {
content: PERSONALITY.getPersonality().reminder.delete,
};
messageReply(botMessage, payload);
console.log("reminder deleted");
return false;
}
return true;
});
return;
} catch (err) {
console.log("reminderError", err);
}
}
};
//#endregion