From 5eb80a2280fc704fa120fca89de792f511268163 Mon Sep 17 00:00:00 2001 From: OrigamingWasTaken Date: Tue, 28 Nov 2023 21:20:12 +0100 Subject: [PATCH] Fixed undefined serverInfo and modif. help command --- config/commands/help.yml | 26 +++++++++++++++ package.json | 2 +- src/commands/alerts/alerts.ts | 11 ------- src/commands/alerts/functions.ts | 38 ++++------------------ src/commands/help/folder/subfolder/help.ts | 29 ----------------- src/commands/help/help.ts | 24 ++++++++++++++ src/types/index.ts | 20 +++++++++++- tsconfig.json | 1 + 8 files changed, 78 insertions(+), 73 deletions(-) create mode 100644 config/commands/help.yml delete mode 100644 src/commands/help/folder/subfolder/help.ts create mode 100644 src/commands/help/help.ts diff --git a/config/commands/help.yml b/config/commands/help.yml new file mode 100644 index 0000000..e2483a2 --- /dev/null +++ b/config/commands/help.yml @@ -0,0 +1,26 @@ +Commands: + - name: "Aide" + command: "help" + description: "Affiche ce menu" + - name: "Latence" + command: "ping" + description: "Répond avec la latence en ms du bot" + +Admin Commands: + - name: "Alertes" + command: "alert" + category: true + description: "Gère les alertes d'évents du serveur" + subcommands: + - command: "info" + description: "Affiche des informations à propos de la configuration des alertes" + - command: "enable" + description: "Active les alertes d'évents" + - command: "disable" + description: "Désactive les alertes d'évents" + - command: "setchannel" + description: "Défini le channel d'alerte pour les évents" + - command: "pick" + description: "Active/Désactive certain serveurs d'évents" + - command: "roles bind" + description: "Permet de définir le rôle à ping pour un certain event" \ No newline at end of file diff --git a/package.json b/package.json index d7fc1af..88a9c50 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "node scripts/start.js && npm run build && cd dist && node src/index.js", "build": "rimraf dist/* && tsc && tsc-alias && tscp", - "build:watch": "tsc && tscp && (concurrently \"tsc -w\" \"tsc-alias -w\")", + "build:watch": "tsc -c && tscp && (concurrently \"tsc -w\" \"tsc-alias -w\")", "watch": "npm run build:watch", "url": "node scripts/url.js" }, diff --git a/src/commands/alerts/alerts.ts b/src/commands/alerts/alerts.ts index b2570df..780507f 100644 --- a/src/commands/alerts/alerts.ts +++ b/src/commands/alerts/alerts.ts @@ -36,17 +36,6 @@ export default { subcommand .setName("roles") .setDescription("Séléctionne les rôles à ping lors d'un event.") - // .addSubcommand(subcommand => - // subcommand - // .setName("add") - // .setDescription("Ajoute le rôle séléctionné") - // .addRoleOption(option => - // option - // .setRequired(true) - // .setName("role") - // .setDescription("Le rôle à ajouter") - // ) - // ) .addSubcommand(subcommand => subcommand .setName("unbind") diff --git a/src/commands/alerts/functions.ts b/src/commands/alerts/functions.ts index 019d7bc..95e375e 100644 --- a/src/commands/alerts/functions.ts +++ b/src/commands/alerts/functions.ts @@ -137,50 +137,26 @@ export async function pickSource(interaction: ChatInputCommandInteraction) { } } -// export async function roles(interaction: ChatInputCommandInteraction, operation: boolean) { -// const roleId = interaction.options.getRole("role").id -// try { -// await mongo.connect() -// const db: Db = mongo.db("discord") -// const collection: Collection = db.collection("servers") - -// let roles: string[] = (await collection.findOne({ id: interaction.guild.id })).roles || [] -// if (operation) { -// roles.push(roleId) -// } else { -// roles = roles.filter(role => role !== roleId) -// } -// await collection.updateOne( -// { id: interaction.guild.id }, -// { $set: { roles: [...new Set(roles)] } }, -// { upsert: true } -// ) -// interaction.reply({ content: operation ? `<@&${roleId}> a été **ajouté** à la liste des rôles.` : `<@&${roleId}> a été **retiré** à la liste des rôles.`, ephemeral: true }) -// } catch (error) { -// logging(`Error occured while updating guild ping roles:\n${error}`, "error") -// } finally { -// if (mongo) { -// await mongo.close() -// } -// } -// } - export async function info(interaction: ChatInputCommandInteraction) { try { await mongo.connect() const db: Db = mongo.db("discord") const collection: Collection = db.collection("servers") const serverInfo: DiscordServerInfo = await collection.findOne({ id: interaction.guild.id }) + if (!serverInfo) { + interaction.reply({content: "Aucune configuration n'a été trouvée pour le serveur.",ephemeral: true}) + return + } const sources: EventSource[] = await getSources() - const formattedString: string = Object.entries(serverInfo.roles) + const formattedString: string = serverInfo.roles ? Object.entries(serverInfo.roles) .map(([id, roleId]) => { const user = sources.find((src) => src.guildId === id); const userName = user ? user.name : id; return `\n> \`${userName}\`: <@&${roleId}>`; }) - .join(', '); + .join(', ') : '@everyone'; const embed = new EmbedBuilder() .setTitle("Paramètres des Alertes") @@ -189,7 +165,7 @@ export async function info(interaction: ChatInputCommandInteraction) { **Actif**: ${serverInfo.enabled ? '✅' : '🚫'} **Channel**: ${serverInfo.channel ? '<#' + serverInfo.channel + '>' : '`Non défini`'} - **Role(s)**: ${serverInfo.roles ? formattedString : "@everyone"} + **Role(s)**: ${formattedString} **Serveurs Évents Actifs**: ${serverInfo.sources ? "\n*" + serverInfo.sources .map((src) => sources.find((s) => s.guildId === src)?.name) .join("*\n*") + "*" : '`Aucun`'}` diff --git a/src/commands/help/folder/subfolder/help.ts b/src/commands/help/folder/subfolder/help.ts deleted file mode 100644 index ae46802..0000000 --- a/src/commands/help/folder/subfolder/help.ts +++ /dev/null @@ -1,29 +0,0 @@ -// @command -import { SlashCommandBuilder, EmbedBuilder, Embed, AttachmentBuilder } from 'discord.js' -const Theme = loadYaml("bot/info.yml") - -export default { - data: new SlashCommandBuilder() - .setName('help') - .setDescription('Shows the help menu'), - async execute(interaction) { - const embed = new EmbedBuilder() - .setTitle("Menu d'aide") - .setColor(Theme.Color) - .setThumbnail("attachment://question_mark.png") - .setDescription("Voici une liste des actions proposées par **Event Helper**.") - .setFields( - { name: "Commandes", value: - `**/help**: Affiche ce menu - **/ping**: Répond avec la latence (en ms) du bot` }, - { name: "Commandes Admin", value: - `**/alerts enable**: Active les alertes - **/alerts disable**: Désactive les alertes - **/alerts setchannel**: Défini le channel dans lequel envoyer les alertes - **/alerts pick**: Menu pour choisir quels serveurs d'events doivent être annoncé - **/alerts roles add**: Ajoute un rôle à ping - **/alerts roles remove**: Retire un rôle à ping` } - ) - await interaction.reply({embeds: [embed], files: [new AttachmentBuilder("src/images/question_mark.png")]}); - } -}; \ No newline at end of file diff --git a/src/commands/help/help.ts b/src/commands/help/help.ts new file mode 100644 index 0000000..c0fe5bd --- /dev/null +++ b/src/commands/help/help.ts @@ -0,0 +1,24 @@ +import { HelpCommandConfig } from '@src/types'; +import { SlashCommandBuilder, EmbedBuilder, AttachmentBuilder } from 'discord.js' +const Theme = loadYaml("bot/info.yml") +const HelpConfig: HelpCommandConfig = loadYaml("commands/help.yml") + +// @command +// here cuz otherwise tsc removes it +export default { + data: new SlashCommandBuilder() + .setName('help') + .setDescription('Shows the help menu'), + async execute(interaction) { + const embed = new EmbedBuilder() + .setTitle("Menu d'aide") + .setColor(Theme.Color) + .setThumbnail("attachment://question_mark.png") + .setDescription("Voici une liste des actions proposées par **Event Helper**.") + .setFields( + { name: "Commandes", value: HelpConfig.Commands.map(cmd => `** **\n**${cmd.name}**\n${cmd.category ? cmd.subcommands.map(sub => "`/" + cmd.command + sub.command + "` : " + sub.description + "\n").join('') + "\n" : "`/" + cmd.command + "` : " + cmd.description + "\n"}`).join('')}, + { name: "Commandes Admins", value: HelpConfig['Admin Commands'].map(cmd => `** **\n**${cmd.name}**\n${cmd.category ? cmd.subcommands.map(sub => "`/" + cmd.command + sub.command + "` : " + sub.description + "\n").join('') + "\n" : "`/" + cmd.command + "` : " + cmd.description + "\n"}`).join('')}, + ) + await interaction.reply({embeds: [embed], files: [new AttachmentBuilder("src/images/question_mark.png")]}); + } +}; \ No newline at end of file diff --git a/src/types/index.ts b/src/types/index.ts index a85f0a1..29406c9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -9,16 +9,34 @@ export interface EventSource { emoji: string } +// Config + export interface BotConfig { 'App Name': string Color: `#${string}` } +export interface HelpCommandCategory { + name: string, + command: string, + category: boolean, + description: string, + subcommands?: { + command: string, + description: string + }[] +} +export interface HelpCommandConfig { + Commands: HelpCommandCategory[], + 'Admin Commands': HelpCommandCategory[] +} + +// Mongo export interface DiscordServerInfo { _id: ObjectId id?: string, channel?: string, enabled?: boolean, sources?: string[], - roles?: { [id: string]: string} + roles?: { [id: string]: string } } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 1a63930..2e9dee5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "files": true }, "compilerOptions": { + "removeComments": false, "resolveJsonModule": true, "esModuleInterop": true, "rootDir": "./",