Skip to content

Commit

Permalink
Merge pull request #21 from UniversityOfHelsinkiCS/fix-bot
Browse files Browse the repository at this point in the history
Fix bot
  • Loading branch information
LeoVaris authored Sep 21, 2024
2 parents c1882a8 + 755d7c1 commit 5bf421b
Showing 1 changed file with 69 additions and 45 deletions.
114 changes: 69 additions & 45 deletions src/discordBot/services/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,76 @@ const { Routes } = require("discord-api-types/v9");
const clientId = process.env.BOT_ID;
const guildId = process.env.GUILD_ID;
const token = process.env.DISCORD_BOT_TOKEN;
const { findPrivateCoursesFromDb, findPublicCoursesFromDb, findLockedCoursesFromDb, findUnlockedCoursesFromDb, findCoursesFromDb } = require("../../db/services/courseService");
const {
findPrivateCoursesFromDb,
findPublicCoursesFromDb,
findLockedCoursesFromDb,
findUnlockedCoursesFromDb,
findCoursesFromDb,
} = require("../../db/services/courseService");
const { logError } = require("./logger");

const parseCourseData = (courseData) => {
const choices = courseData
.map((c) => {
const regExp = /[^0-9]*/;
const fullname = c.fullName.charAt(0).toUpperCase() + c.fullName.slice(1);
const matches = regExp.exec(c.code)?.[0];
const code = matches ? matches.toUpperCase() + c.code.slice(matches.length) : c.code;
return (
{
name: `${code} - ${fullname} - ${c.name}`,
value: c.name,
}
);
});
const choices = courseData.map((c) => {
const regExp = /[^0-9]*/;
const fullname = c.fullName.charAt(0).toUpperCase() + c.fullName.slice(1);
const matches = regExp.exec(c.code)?.[0];
const code = matches
? matches.toUpperCase() + c.code.slice(matches.length)
: c.code;
return {
name: `${code} - ${fullname} - ${c.name}`,
value: c.name,
};
});
return choices;
};

const addOptions = async (command, obj, courseData) => {
const parsedChoices = parseCourseData(courseData);
parsedChoices.forEach((ch) => obj.data.options[0].addChoice(ch.name, ch.value));
parsedChoices.forEach((ch) => {
try {
obj.data.options[0].addChoice(ch.name, ch.value);
} catch (e) {}
});

const options = obj.data.options;
await command.edit({
options: options,
})
await command
.edit({
options: options,
})
.catch(console.error);
};

const updateDynamicChoices = async (client, commandNames, Course) => {
const loadedCommands = await client.guilds.cache.get(guildId)?.commands.fetch();
const filteredCommands = await loadedCommands.filter((command) => commandNames.includes(command.name));
const loadedCommands = await client.guilds.cache
.get(guildId)
?.commands.fetch();
const filteredCommands = await loadedCommands.filter((command) =>
commandNames.includes(command.name)
);
filteredCommands.map(async (c) => {
const obj = {
data: new SlashCommandBuilder()
.setName(c.name)
.setDescription(c.description)
.setDefaultPermission(!c.role)
.addStringOption(option =>
option.setName(c.options[0].name)
.addStringOption((option) =>
option
.setName(c.options[0].name)
.setDescription(c.options[0].description)
.setRequired(true)),
.setRequired(true)
),
};
if (obj.data.name === "join" || obj.data.name === "hide_course") {
await addOptions(c, obj, await findPublicCoursesFromDb("code", Course));
}
else if (obj.data.name === "leave") {
} else if (obj.data.name === "leave") {
await addOptions(c, obj, await findCoursesFromDb("code", Course));
}
else if (obj.data.name === "unhide_course") {
} else if (obj.data.name === "unhide_course") {
await addOptions(c, obj, await findPrivateCoursesFromDb("code", Course));
}
else if (obj.data.name === "lock_chat") {
} else if (obj.data.name === "lock_chat") {
await addOptions(c, obj, await findUnlockedCoursesFromDb("code", Course));
}
else if (obj.data.name === "unlock_chat") {
} else if (obj.data.name === "unlock_chat") {
await addOptions(c, obj, await findLockedCoursesFromDb("code", Course));
}
});
Expand All @@ -73,13 +86,11 @@ const deployCommands = async (commands) => {

(async () => {
try {
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: commands,
});
console.log("Successfully registered application commands.");
}
catch (error) {
} catch (error) {
logError(error);
console.error(error);
}
Expand All @@ -90,18 +101,20 @@ const loadCommands = (client) => {
const commands = [];
client.commands = new Collection();
const slashCommands = new Collection();
const commandFolders = fs.readdirSync("./src/discordBot/commands/", { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
const commandFolders = fs
.readdirSync("./src/discordBot/commands/", { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

for (const folder of commandFolders) {
const slashCommandFiles = fs.readdirSync(`./src/discordBot/commands/${folder}`).filter(file => file.endsWith(".js"));
const slashCommandFiles = fs
.readdirSync(`./src/discordBot/commands/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of slashCommandFiles) {
const command = require(`../commands/${folder}/${file}`);
if (command.prefix) {
client.commands.set(command.name, command);
}
else {
} else {
slashCommands.set(command.data.name, command);
commands.push(command.data.toJSON());
}
Expand All @@ -115,10 +128,21 @@ const loadCommands = (client) => {
const setUpCommands = async (client, Course) => {
const commands = loadCommands(client);
if (process.env.NODE_ENV === "production") await deployCommands(commands);
await updateDynamicChoices(client, ["join", "leave", "hide_course", "unhide_course", "lock_chat", "unlock_chat"], Course);
await updateDynamicChoices(
client,
[
"join",
"leave",
"hide_course",
"unhide_course",
"lock_chat",
"unlock_chat",
],
Course
);
};

module.exports = {
setUpCommands,
updateDynamicChoices,
};
};

0 comments on commit 5bf421b

Please sign in to comment.