Skip to content

Commit 755d7c1

Browse files
committed
Fix bot
1 parent c1882a8 commit 755d7c1

File tree

1 file changed

+69
-45
lines changed

1 file changed

+69
-45
lines changed

src/discordBot/services/command.js

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,76 @@ const { Routes } = require("discord-api-types/v9");
66
const clientId = process.env.BOT_ID;
77
const guildId = process.env.GUILD_ID;
88
const token = process.env.DISCORD_BOT_TOKEN;
9-
const { findPrivateCoursesFromDb, findPublicCoursesFromDb, findLockedCoursesFromDb, findUnlockedCoursesFromDb, findCoursesFromDb } = require("../../db/services/courseService");
9+
const {
10+
findPrivateCoursesFromDb,
11+
findPublicCoursesFromDb,
12+
findLockedCoursesFromDb,
13+
findUnlockedCoursesFromDb,
14+
findCoursesFromDb,
15+
} = require("../../db/services/courseService");
1016
const { logError } = require("./logger");
1117

1218
const parseCourseData = (courseData) => {
13-
const choices = courseData
14-
.map((c) => {
15-
const regExp = /[^0-9]*/;
16-
const fullname = c.fullName.charAt(0).toUpperCase() + c.fullName.slice(1);
17-
const matches = regExp.exec(c.code)?.[0];
18-
const code = matches ? matches.toUpperCase() + c.code.slice(matches.length) : c.code;
19-
return (
20-
{
21-
name: `${code} - ${fullname} - ${c.name}`,
22-
value: c.name,
23-
}
24-
);
25-
});
19+
const choices = courseData.map((c) => {
20+
const regExp = /[^0-9]*/;
21+
const fullname = c.fullName.charAt(0).toUpperCase() + c.fullName.slice(1);
22+
const matches = regExp.exec(c.code)?.[0];
23+
const code = matches
24+
? matches.toUpperCase() + c.code.slice(matches.length)
25+
: c.code;
26+
return {
27+
name: `${code} - ${fullname} - ${c.name}`,
28+
value: c.name,
29+
};
30+
});
2631
return choices;
2732
};
2833

2934
const addOptions = async (command, obj, courseData) => {
3035
const parsedChoices = parseCourseData(courseData);
31-
parsedChoices.forEach((ch) => obj.data.options[0].addChoice(ch.name, ch.value));
36+
parsedChoices.forEach((ch) => {
37+
try {
38+
obj.data.options[0].addChoice(ch.name, ch.value);
39+
} catch (e) {}
40+
});
41+
3242
const options = obj.data.options;
33-
await command.edit({
34-
options: options,
35-
})
43+
await command
44+
.edit({
45+
options: options,
46+
})
3647
.catch(console.error);
3748
};
3849

3950
const updateDynamicChoices = async (client, commandNames, Course) => {
40-
const loadedCommands = await client.guilds.cache.get(guildId)?.commands.fetch();
41-
const filteredCommands = await loadedCommands.filter((command) => commandNames.includes(command.name));
51+
const loadedCommands = await client.guilds.cache
52+
.get(guildId)
53+
?.commands.fetch();
54+
const filteredCommands = await loadedCommands.filter((command) =>
55+
commandNames.includes(command.name)
56+
);
4257
filteredCommands.map(async (c) => {
4358
const obj = {
4459
data: new SlashCommandBuilder()
4560
.setName(c.name)
4661
.setDescription(c.description)
4762
.setDefaultPermission(!c.role)
48-
.addStringOption(option =>
49-
option.setName(c.options[0].name)
63+
.addStringOption((option) =>
64+
option
65+
.setName(c.options[0].name)
5066
.setDescription(c.options[0].description)
51-
.setRequired(true)),
67+
.setRequired(true)
68+
),
5269
};
5370
if (obj.data.name === "join" || obj.data.name === "hide_course") {
5471
await addOptions(c, obj, await findPublicCoursesFromDb("code", Course));
55-
}
56-
else if (obj.data.name === "leave") {
72+
} else if (obj.data.name === "leave") {
5773
await addOptions(c, obj, await findCoursesFromDb("code", Course));
58-
}
59-
else if (obj.data.name === "unhide_course") {
74+
} else if (obj.data.name === "unhide_course") {
6075
await addOptions(c, obj, await findPrivateCoursesFromDb("code", Course));
61-
}
62-
else if (obj.data.name === "lock_chat") {
76+
} else if (obj.data.name === "lock_chat") {
6377
await addOptions(c, obj, await findUnlockedCoursesFromDb("code", Course));
64-
}
65-
else if (obj.data.name === "unlock_chat") {
78+
} else if (obj.data.name === "unlock_chat") {
6679
await addOptions(c, obj, await findLockedCoursesFromDb("code", Course));
6780
}
6881
});
@@ -73,13 +86,11 @@ const deployCommands = async (commands) => {
7386

7487
(async () => {
7588
try {
76-
await rest.put(
77-
Routes.applicationGuildCommands(clientId, guildId),
78-
{ body: commands },
79-
);
89+
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
90+
body: commands,
91+
});
8092
console.log("Successfully registered application commands.");
81-
}
82-
catch (error) {
93+
} catch (error) {
8394
logError(error);
8495
console.error(error);
8596
}
@@ -90,18 +101,20 @@ const loadCommands = (client) => {
90101
const commands = [];
91102
client.commands = new Collection();
92103
const slashCommands = new Collection();
93-
const commandFolders = fs.readdirSync("./src/discordBot/commands/", { withFileTypes: true })
94-
.filter(dirent => dirent.isDirectory())
95-
.map(dirent => dirent.name);
104+
const commandFolders = fs
105+
.readdirSync("./src/discordBot/commands/", { withFileTypes: true })
106+
.filter((dirent) => dirent.isDirectory())
107+
.map((dirent) => dirent.name);
96108

97109
for (const folder of commandFolders) {
98-
const slashCommandFiles = fs.readdirSync(`./src/discordBot/commands/${folder}`).filter(file => file.endsWith(".js"));
110+
const slashCommandFiles = fs
111+
.readdirSync(`./src/discordBot/commands/${folder}`)
112+
.filter((file) => file.endsWith(".js"));
99113
for (const file of slashCommandFiles) {
100114
const command = require(`../commands/${folder}/${file}`);
101115
if (command.prefix) {
102116
client.commands.set(command.name, command);
103-
}
104-
else {
117+
} else {
105118
slashCommands.set(command.data.name, command);
106119
commands.push(command.data.toJSON());
107120
}
@@ -115,10 +128,21 @@ const loadCommands = (client) => {
115128
const setUpCommands = async (client, Course) => {
116129
const commands = loadCommands(client);
117130
if (process.env.NODE_ENV === "production") await deployCommands(commands);
118-
await updateDynamicChoices(client, ["join", "leave", "hide_course", "unhide_course", "lock_chat", "unlock_chat"], Course);
131+
await updateDynamicChoices(
132+
client,
133+
[
134+
"join",
135+
"leave",
136+
"hide_course",
137+
"unhide_course",
138+
"lock_chat",
139+
"unlock_chat",
140+
],
141+
Course
142+
);
119143
};
120144

121145
module.exports = {
122146
setUpCommands,
123147
updateDynamicChoices,
124-
};
148+
};

0 commit comments

Comments
 (0)