From 91a82232bf7951fb57272f16ade745db9162a84a Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Fri, 2 Sep 2022 13:34:24 +0200 Subject: [PATCH] fix: try catch all moderation commands --- Dockerfile | 2 +- commands/misc/sugerencias.ts | 4 ++-- commands/moderation/ban.ts | 16 ++++++++++------ commands/moderation/deleteMessage.ts | 18 +++++++++++------- commands/moderation/kick.ts | 22 +++++++++++++--------- commands/moderation/prune.ts | 21 ++++++++++++--------- commands/moderation/slowmode.ts | 16 ++++++++++------ commands/moderation/timeoutUser.ts | 22 ++++++++++++---------- index.ts | 1 + tsconfig.json | 2 +- 10 files changed, 73 insertions(+), 51 deletions(-) diff --git a/Dockerfile b/Dockerfile index d81fe43..4ee0a52 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,4 @@ RUN npm install -g ts-node typescript COPY . . -CMD ts-node index.ts \ No newline at end of file +CMD ts-node --transpile-only index.ts \ No newline at end of file diff --git a/commands/misc/sugerencias.ts b/commands/misc/sugerencias.ts index bfc3bc4..523b24e 100644 --- a/commands/misc/sugerencias.ts +++ b/commands/misc/sugerencias.ts @@ -22,7 +22,7 @@ export default commandModule({ } else if (value.indexOf('sugerencia') >= 0 || value.indexOf('sugerencias') >= 0) { modal.reply({content: 'No puedes enviar una sugerencia con la palabra sugerencia(s).'}) } else { - // then we create the embed which will be sent when the thing is sent + // we create the embed which will be sent when the thing is sent const modalEmbed = new EmbedBuilder() .setColor("Random") .setTitle('Sugerencia') @@ -30,7 +30,7 @@ export default commandModule({ .setDescription(value); // finally send the message to the text channel const message1 = modal.client.guilds.cache.get('928018226330337280').channels.cache.get('1007269448140476436') as TextChannel - const message2 = await (await message1.send({embeds: [modalEmbed]})) + const message2 = (await message1.send({embeds: [modalEmbed]})) message2.startThread({name: `Sugerencia de ${modal.user.username}`, autoArchiveDuration: ThreadAutoArchiveDuration.ThreeDays, reason: 'AUTOMATIZADO: Hilo para discutir sobre la sugerencia.'}) message2.react("✅") message2.react("❎") diff --git a/commands/moderation/ban.ts b/commands/moderation/ban.ts index d7f0cf1..8b65787 100644 --- a/commands/moderation/ban.ts +++ b/commands/moderation/ban.ts @@ -22,11 +22,15 @@ export default commandModule({ }], //alias : [], execute: async (ctx, options, message) => { - const userToBan = options[1].getMember('usuario', true); - const reason = options[1].get('razon', true).value; - userToBan.ban(reason) - const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') - await sendToMods.send({content: `Se ha baneado a ${userToBan}.\nBan efectuado por ${ctx.user} con razón "${reason}."`}) - await ctx.reply({content: 'Baneado correctamente!', ephemeral: true}) + try { + const userToBan = options[1].getMember('usuario', true); + const reason = options[1].get('razon', true).value; + userToBan.ban(reason) + const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') + await sendToMods.send({content: `Se ha baneado a ${userToBan}.\nBan efectuado por ${ctx.user} con razón "${reason}."`}) + await ctx.reply({content: 'Baneado correctamente!', ephemeral: true}) + } catch (e) { + await ctx.reply({content: `ERROR: No puedo hacer este comando porque a lo mejor soy inferior que el rol de esa persona o estoy usándolo contra admins.`}) + } }, }); \ No newline at end of file diff --git a/commands/moderation/deleteMessage.ts b/commands/moderation/deleteMessage.ts index 1dc4fb1..c4d5b06 100644 --- a/commands/moderation/deleteMessage.ts +++ b/commands/moderation/deleteMessage.ts @@ -28,12 +28,16 @@ export default commandModule({ } ], execute: async (ctx, options) => { - const idMensaje = options[1].getString('id', true); - const channelID = options[1].getChannel('canal', true).id || ctx.channel.id - const guildId = ctx.guild.id - const guild = await ctx.client.guilds.fetch(guildId); - const channel = await guild.channels.fetch(channelID); - (await channel.messages.fetch(idMensaje)).delete(); - await ctx.reply({content: 'Mensaje eliminado correctamente.', ephemeral: true}); + try { + const idMensaje = options[1].getString('id', true); + const channelID = options[1].getChannel('canal', true).id || ctx.channel.id + const guildId = ctx.guild.id + const guild = await ctx.client.guilds.fetch(guildId); + const channel = await guild.channels.fetch(channelID); + (await channel.messages.fetch(idMensaje)).delete(); + await ctx.reply({content: 'Mensaje eliminado correctamente.', ephemeral: true}); + } catch (e) { + await ctx.reply({content: `ERROR: No se ha podido eliminar el mensaje, asegúrate que estás usando el ID y el canal correcto.`}) + } }, }); \ No newline at end of file diff --git a/commands/moderation/kick.ts b/commands/moderation/kick.ts index 6ca27a8..2b45d96 100644 --- a/commands/moderation/kick.ts +++ b/commands/moderation/kick.ts @@ -24,14 +24,18 @@ export default commandModule({ ], //alias : [], execute: async (ctx, options, message) => { - const userToBan = options[1].getMember('usuario', true); - const reason = options[1].get('razon', true).value; - const kickEmbed = new EmbedBuilder() - .setTitle('Nuevo kick.') - .setDescription(`Kick efectuado por <@${ctx.author}>.\nRazón: ${reason}.`); - userToBan.kick(reason) - const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') - await sendToMods.send({content: `Se ha expulsado a ${userToBan}.\nKick efectuado por ${ctx.user} con razón "${reason}."`}) - await ctx.reply({content: 'Expulsado correctamente!'}) + try { + const userToBan = options[1].getMember('usuario', true); + const reason = options[1].get('razon', true).value; + const kickEmbed = new EmbedBuilder() + .setTitle('Nuevo kick.') + .setDescription(`Kick efectuado por <@${ctx.author}>.\nRazón: ${reason}.`); + userToBan.kick(reason) + const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') + await sendToMods.send({content: `Se ha expulsado a ${userToBan}.\nKick efectuado por ${ctx.user} con razón "${reason}."`}) + await ctx.reply({content: 'Expulsado correctamente!'}) + } catch (e) { + await ctx.reply({content: `ERROR: No puedo hacer este comando porque a lo mejor soy inferior que el rol de esa persona o estoy usándolo contra admins.`}) + } }, }); \ No newline at end of file diff --git a/commands/moderation/prune.ts b/commands/moderation/prune.ts index 6b4c4e0..b6bdaf0 100644 --- a/commands/moderation/prune.ts +++ b/commands/moderation/prune.ts @@ -13,17 +13,20 @@ export default commandModule({ description: 'Escribe un número', type: ApplicationCommandOptionType.Number, required: true, - min_value: 2, + min_value: 1, max_value: 100 }], //alias : [], execute: async (ctx, options) => { - const amount = options[1].getNumber('numero', true); - ctx.channel.bulkDelete(amount, true).catch(err => { - console.error(err); - ctx.reply({content: 'Ha habido un error eliminando mensajes! (mira la consola, Sr Izan)', ephemeral: true});}); - await ctx.reply({content: `Se han eliminado ${amount} mensajes.`}) - const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') - await sendToMods.send({content: `Se han eliminado ${amount} mensajes en ${ctx.channel}\nEfectuado por ${ctx.user}.`}) - }, + try { + const amount = options[1].getNumber('numero', true); + ctx.channel.bulkDelete(amount, true).catch(err => { + console.error(err); + ctx.reply({content: 'Ha habido un error eliminando mensajes! (mira la consola, Sr Izan)', ephemeral: true});}); + await ctx.reply({content: `Se han eliminado ${amount} mensajes.`}) + const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') + await sendToMods.send({content: `Se han eliminado ${amount} mensajes en ${ctx.channel}\nEfectuado por ${ctx.user}.`}) + } catch (e){ + ctx.reply({content: 'Ha habido un error eliminando mensajes! Error reportado automáticamente.', ephemeral: true})}; + } }); \ No newline at end of file diff --git a/commands/moderation/slowmode.ts b/commands/moderation/slowmode.ts index a0753c6..69edd1e 100644 --- a/commands/moderation/slowmode.ts +++ b/commands/moderation/slowmode.ts @@ -24,13 +24,17 @@ export default commandModule({ ], //alias : [], execute: async (ctx, options) => { - const seconds = options[1].getNumber("segundos", true); - const reason = options[1].getString("razon", true); + try { + const seconds = options[1].getNumber("segundos", true); + const reason = options[1].getString("razon", true); - ctx.channel.setRateLimitPerUser(seconds, reason) + ctx.channel.setRateLimitPerUser(seconds, reason) - ctx.reply({content: `Se han añadido ${seconds} segundos de modo lento al canal de voz actual`}) - const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') - await sendToMods.send({content: `Se ha aplicado modo lento al canal ${ctx.channel}.\nEfectuado por ${ctx.user} con ${seconds} segundos de retardo.\nRazón: ${reason}`}) + ctx.reply({content: `Se han añadido ${seconds} segundos de modo lento al canal de voz actual`}) + const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') + await sendToMods.send({content: `Se ha aplicado modo lento al canal ${ctx.channel}.\nEfectuado por ${ctx.user} con ${seconds} segundos de retardo.\nRazón: ${reason}`}) + } catch (e) { + ctx.reply({content: `No se ha podido aplicar modo lento al canal.`}) + } }, }); \ No newline at end of file diff --git a/commands/moderation/timeoutUser.ts b/commands/moderation/timeoutUser.ts index 9818cf7..06690f0 100644 --- a/commands/moderation/timeoutUser.ts +++ b/commands/moderation/timeoutUser.ts @@ -35,14 +35,16 @@ export default commandModule({ ], //alias : [], execute: async (ctx, options) => { - const usuario = options[1].getMember('usuario', true); - const minutos = options[1].getNumber('minutos', true); - const razon = options[1].getString('razon', true); - const minutosToMilisegundos = minutos * 60 * 1000 - - usuario.timeout(minutosToMilisegundos, razon).then(() => {ctx.reply({content: `Se ha silenciado a ${usuario} correctamente.`, ephemeral: true})}) - const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') - await sendToMods.send({content: `Se ha silenciado a ${usuario}.\nSlencio efectuado por ${ctx.user} con ${minutos} minutos de duración.\nRazón: ${razon}`}) - } + try { + const usuario = options[1].getMember('usuario', true); + const minutos = options[1].getNumber('minutos', true); + const razon = options[1].getString('razon', true); + const minutosToMilisegundos = minutos * 60 * 1000 + usuario.timeout(minutosToMilisegundos, razon).then(() => {ctx.reply({content: `Se ha silenciado a ${usuario} correctamente.`, ephemeral: true})}) + const sendToMods = ctx.client.guilds.cache.get('928018226330337280')!.channels.cache.get('1004118323258208257') + await sendToMods.send({content: `Se ha silenciado a ${usuario}.\nSlencio efectuado por ${ctx.user} con ${minutos} minutos de duración.\nRazón: ${razon}`}) + } catch (e) { + await ctx.reply({content: `ERROR: No puedo hacer este comando porque a lo mejor soy inferior que el rol de esa persona o estoy usándolo contra admins.`}) + } }, -); \ No newline at end of file +}); \ No newline at end of file diff --git a/index.ts b/index.ts index ba72941..e080dfa 100644 --- a/index.ts +++ b/index.ts @@ -8,6 +8,7 @@ require("dotenv").config(); const sernPrefix = process.env.PREFIX const mongoose = require('mongoose'); const youtube = require('discord-bot-youtube-notifications'); + const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildVoiceStates], restTimeOffset: 0 diff --git a/tsconfig.json b/tsconfig.json index dd73f73..37fe2e4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -39,7 +39,7 @@ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */ - // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */