From 51e2d61358359c6ca82a680d6221327b5440da58 Mon Sep 17 00:00:00 2001 From: Tutur33 Date: Fri, 15 Mar 2024 20:35:35 +0100 Subject: [PATCH] add anti crash + command --- commands/botcontrol/invite.ts | 5 +- commands/botcontrol/servers.ts | 2 + commands/botcontrol/set.ts | 78 +++++++++++++------------- commands/botcontrol/setactivity.ts | 2 + commands/{ => botcontrol}/setprefix.ts | 2 +- commands/botcontrol/setstatus.ts | 2 + commands/utils/addbtnurl.ts | 35 ++++++++++++ commands/utils/allbot.ts | 28 +++++++++ commands/utils/alldmin.ts | 28 +++++++++ commands/utils/banner.ts | 38 +++++++++++++ commands/{ => utils}/help.ts | 2 +- commands/{ => utils}/ping.ts | 0 events/ready.ts | 29 +++++++++- fonctions/loadCommands.ts | 3 +- 14 files changed, 208 insertions(+), 46 deletions(-) rename commands/{ => botcontrol}/setprefix.ts (94%) create mode 100644 commands/utils/addbtnurl.ts create mode 100644 commands/utils/allbot.ts create mode 100644 commands/utils/alldmin.ts create mode 100644 commands/utils/banner.ts rename commands/{ => utils}/help.ts (98%) rename commands/{ => utils}/ping.ts (100%) diff --git a/commands/botcontrol/invite.ts b/commands/botcontrol/invite.ts index 8c577b1..6705224 100644 --- a/commands/botcontrol/invite.ts +++ b/commands/botcontrol/invite.ts @@ -1,12 +1,11 @@ import { PermissionsBitField, Message, Client } from 'discord.js'; module.exports = { - name: 'invite', aliases: ['inv'], description: 'Crée un lien d\'invitation pour un serveur spécifique', emote: '🔗', - utilisation: 'invite [guildId]', - category: 'botcontrol', + utilisation: '[guildId]', + permission: '10', async execute(message: Message, args: string[], client: Client) { if (!args[0]) return message.reply('Veuillez fournir l\'ID du serveur.'); diff --git a/commands/botcontrol/servers.ts b/commands/botcontrol/servers.ts index 7e92130..4d6dfea 100644 --- a/commands/botcontrol/servers.ts +++ b/commands/botcontrol/servers.ts @@ -5,6 +5,8 @@ module.exports = { description: 'Liste les serveur du bot', emote: '🌐', utilisation: '', + permission: '10', + async execute(message: Message, args: string[], client: Client) { if (!client.user) return; const guilds = Array.from(client.guilds.cache.values()).map((guild, index) => `${index + 1}. ${guild.name} : ${guild.id}`).join("\n\n"); diff --git a/commands/botcontrol/set.ts b/commands/botcontrol/set.ts index 49eafee..6533298 100644 --- a/commands/botcontrol/set.ts +++ b/commands/botcontrol/set.ts @@ -5,46 +5,46 @@ module.exports = { description: 'Modifier divers paramètres du bot.', emote: '⚙️', utilisation: 'set ', + permissions: 10, - async execute(message: Message, args: string[], client: Client) { - if (args.length < 2) { - return message.reply('Veuillez fournir un paramètre parmis `name` ou `pic` pour sa valeur.'); - } - - const setting = args[0]; - - if (setting === 'name') { - const value = args.slice(1).join(' '); - if (value) { - try { - if (!message.guild || !client.user) return; - const botMember = await message.guild.members.fetch(client.user.id); - await botMember.setNickname(value); - return message.reply(`Mon nouveau pseudo est maintenant **${value}**.`); - } catch (error) { - console.error(error); - return message.reply('Une erreur est survenue lors de la tentative de changement de mon pseudo.'); - } - } else { - return message.reply('Veuillez fournir un nom valide.'); - } - } else if (setting === 'pic') { - const value = args[1]; - if (value && value.startsWith('http')) { - try { - if (!client.user) return; - await client.user.edit({avatar: value}); - return message.reply('Mon avatar a été mis à jour avec succès.'); - } catch (error) { - console.error(error); - return message.reply('Une erreur est survenue lors de la tentative de mise à jour de mon avatar.'); - } - } else { - return message.reply('Veuillez fournir un URL d\'image valide.'); + async execute(message: Message, args: string[], client: Client) { + if (args.length < 2) { + return message.reply('Veuillez fournir un paramètre parmis `name` ou `pic` pour sa valeur.'); + } + + const setting = args[0]; + + if (setting === 'name') { + const value = args.slice(1).join(' '); + if (value) { + try { + if (!message.guild || !client.user) return; + const botMember = await message.guild.members.fetch(client.user.id); + await botMember.setNickname(value); + return message.reply(`Mon nouveau pseudo est maintenant **${value}**.`); + } catch (error) { + console.error(error); + return message.reply('Une erreur est survenue lors de la tentative de changement de mon pseudo.'); } } else { - return message.reply('Paramètre invalide. Veuillez utiliser "name" ou "pic".'); + return message.reply('Veuillez fournir un nom valide.'); } - }, - }; - \ No newline at end of file + } else if (setting === 'pic') { + const value = args[1]; + if (value && value.startsWith('http')) { + try { + if (!client.user) return; + await client.user.edit({avatar: value}); + return message.reply('Mon avatar a été mis à jour avec succès.'); + } catch (error) { + console.error(error); + return message.reply('Une erreur est survenue lors de la tentative de mise à jour de mon avatar.'); + } + } else { + return message.reply('Veuillez fournir un URL d\'image valide.'); + } + } else { + return message.reply('Paramètre invalide. Veuillez utiliser "name" ou "pic".'); + } + }, +}; \ No newline at end of file diff --git a/commands/botcontrol/setactivity.ts b/commands/botcontrol/setactivity.ts index df13c1a..b9f9903 100644 --- a/commands/botcontrol/setactivity.ts +++ b/commands/botcontrol/setactivity.ts @@ -1,10 +1,12 @@ import { Message, Client, EmbedBuilder, StringSelectMenuBuilder, ActionRowBuilder, ActivityType, Activity } from "discord.js"; module.exports = { + aliases: ['setact'], description: 'Changer la présence du bot', emote: '🎮', utilisation: '', permission: '8', + async execute(message: Message, args:string[], client: Client) { if (!client.user) return diff --git a/commands/setprefix.ts b/commands/botcontrol/setprefix.ts similarity index 94% rename from commands/setprefix.ts rename to commands/botcontrol/setprefix.ts index 3e991f9..a7133da 100644 --- a/commands/setprefix.ts +++ b/commands/botcontrol/setprefix.ts @@ -1,6 +1,6 @@ import sqlite3 from 'sqlite3'; import { Message } from 'discord.js'; -const getServerPrefix = require("../fonctions/getServerPrefix"); +const getServerPrefix = require("../../fonctions/getServerPrefix"); module.exports = { aliases: ['prefixset'], diff --git a/commands/botcontrol/setstatus.ts b/commands/botcontrol/setstatus.ts index 6f4611e..f1e5c27 100644 --- a/commands/botcontrol/setstatus.ts +++ b/commands/botcontrol/setstatus.ts @@ -1,10 +1,12 @@ import { Message, Client, EmbedBuilder, StringSelectMenuBuilder, ActionRowBuilder } from "discord.js"; module.exports = { + aliases: ['setpres'], description: 'Changer le status du bot', emote: '🟢', utilisation: '', permission: '8', + async execute(message: Message, args:string[], client: Client) { if (!client.user) return diff --git a/commands/utils/addbtnurl.ts b/commands/utils/addbtnurl.ts new file mode 100644 index 0000000..531a01b --- /dev/null +++ b/commands/utils/addbtnurl.ts @@ -0,0 +1,35 @@ +import { Message, Client, ButtonStyle, ButtonBuilder, ActionRowBuilder } from "discord.js"; + +module.exports = { + aliases: ['addbuttonurl', 'addurlbtn', 'addurlbutton'], + description: 'Ajoute un bouton url', + emote: '🔗', + utilisation: '[url] [message ID] [label]', + permission: 10, + + async execute(message: Message, args: string[], client: Client) { + if (!args[0]) return message.reply('Veuillez fournir une url'); + if (!args[1]) return message.reply('Veuillez fournir un ID de message'); + if (!args[2]) return message.reply('Veuillez fournir un label'); + const url = args[0]; + const messageId = args[1]; + const label = args.slice(2).join(' '); + + if (url && !/^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/.test(url)) { + return message.reply('Veuillez fournir une URL valide'); + } + + const button = new ButtonBuilder() + .setLabel(label) + .setURL(url) + .setStyle(ButtonStyle.Link); + + const row: any = new ActionRowBuilder() + .addComponents(button); + + const msg = await message.channel.messages.fetch(messageId); + if (!msg) return message.reply('Message introuvable'); + if (msg.author.id !== client.user?.id) return message.reply('Ce message n\'a pas été envoyé par moi'); + msg.edit({ components: [row] }); + }, +}; \ No newline at end of file diff --git a/commands/utils/allbot.ts b/commands/utils/allbot.ts new file mode 100644 index 0000000..3f53e2a --- /dev/null +++ b/commands/utils/allbot.ts @@ -0,0 +1,28 @@ +import { Message, EmbedBuilder } from "discord.js"; + +module.exports = { + aliases: ['allbots'], + description: 'Affiche tous les bots sur le serveur', + emote: '🤖', + utilisation: '', + permission: 0, + + async execute(message: Message) { + if (!message.guild) return message.reply('Cette commande ne peut être utilisée que dans un serveur'); + const bots = message.guild.members.cache.filter(member => member.user.bot); + + const embed = new EmbedBuilder() + .setTitle('Liste des bots présents') + .setColor('#0099ff'); + + if (bots.size > 0) { + const botList = Array.from(bots.values()).map((bot, index) => `${index + 1} - <@${bot.id}>`).join('\n'); + embed.setDescription(botList); + embed.setFooter({ text: `Total : ${bots.size}`}); + } else { + embed.setDescription('Il n\'y a pas de bot sur le serveur'); + } + + message.channel.send({ embeds: [embed] }); + }, +}; \ No newline at end of file diff --git a/commands/utils/alldmin.ts b/commands/utils/alldmin.ts new file mode 100644 index 0000000..5079b0a --- /dev/null +++ b/commands/utils/alldmin.ts @@ -0,0 +1,28 @@ +import { Message, EmbedBuilder, PermissionsBitField } from "discord.js"; + +module.exports = { + aliases: ['alladmins'], + description: 'Affiche tous les membres avec la permission d\'administrateur sur le serveur', + emote: '🔧', + utilisation: '', + permission: 0, + + async execute(message: Message) { + if (!message.guild) return message.reply('Cette commande ne peut être utilisée que dans un serveur'); + const admins = message.guild.members.cache.filter(member => member.permissions.has(PermissionsBitField.Flags.Administrator)); + + const embed = new EmbedBuilder() + .setTitle('Administrateurs sur le serveur') + .setColor('#0099ff'); + + if (admins.size > 0) { + const adminList = Array.from(admins.values()).map((admin, index) => `${index + 1} - <@${admin.id}>`).join('\n'); + embed.setDescription(adminList); + embed.setFooter({ text :`Total : ${admins.size}`}); + } else { + embed.setDescription('Il n\'y a pas d\'administrateur sur le serveur'); + } + + message.channel.send({ embeds: [embed] }); + }, +}; diff --git a/commands/utils/banner.ts b/commands/utils/banner.ts new file mode 100644 index 0000000..5395f3f --- /dev/null +++ b/commands/utils/banner.ts @@ -0,0 +1,38 @@ +const fetch = require('node-fetch'); +import { Message, Client, EmbedBuilder } from "discord.js"; + +module.exports = { + aliases: ['bannière', 'banniere'], + description: 'Affiche la bannière d\'un utilisateur', + emote: '🔍', + utilisation: '<@user>', + permission: 0, + + async execute(message: Message, args: string[], client: Client) { + const user = message.mentions.users.first() || message.author; + + const response = await fetch(`https://discord.com/api/users/${user.id}`, { + method: 'GET', + headers: { + Authorization: `Bot ${message.client.token}` + } + }); + const data = await response.json(); + + if (!data.banner) { + return message.reply('Cet utilisateur n\'a pas de bannière.'); + } + + const format = data.banner.startsWith('a_') ? 'gif' : 'png'; + + const bannerURL = `https://cdn.discordapp.com/banners/${user.id}/${data.banner}.${format}?size=2048`; + + const embed = new EmbedBuilder() + .setTitle(`Bannière de ${user.tag}`) + .setImage(bannerURL) + .setFooter({ text: `Demandé par ${message.author.tag}`, iconURL: message.author.displayAvatarURL() }) + .setTimestamp(); + + message.reply({ embeds: [embed] }); + }, +}; \ No newline at end of file diff --git a/commands/help.ts b/commands/utils/help.ts similarity index 98% rename from commands/help.ts rename to commands/utils/help.ts index e050b62..7fbc9fe 100644 --- a/commands/help.ts +++ b/commands/utils/help.ts @@ -1,5 +1,5 @@ import { Message, EmbedBuilder, StringSelectMenuBuilder, ActionRowBuilder } from "discord.js"; -const getServerPrefix = require("../fonctions/getServerPrefix"); +const getServerPrefix = require("../../fonctions/getServerPrefix"); interface Command { name: string; diff --git a/commands/ping.ts b/commands/utils/ping.ts similarity index 100% rename from commands/ping.ts rename to commands/utils/ping.ts diff --git a/events/ready.ts b/events/ready.ts index 44df983..6e90ce3 100644 --- a/events/ready.ts +++ b/events/ready.ts @@ -1,4 +1,4 @@ -const { Events, ActivityType } = require("discord.js"); +import { Events } from "discord.js"; const addBaseInDB = require("../fonctions/addBaseInDB"); module.exports = { @@ -6,5 +6,32 @@ module.exports = { async execute(client: any) { addBaseInDB(client); console.log(`le bot ${client.user.tag} est en ligne`) + + process.on('unhandledRejection', (error) => { + console.log(' [antiCrash] :: Unhandled Rejection/Catch'); + console.log(error); + }); + + process.on("uncaughtException", (error, origin) => { + console.log(' [antiCrash] :: Uncaught Exception/Catch'); + console.log(error); + console.log('Information supplémentaire:', origin); + }); + + process.on('uncaughtExceptionMonitor', (error, origin) => { + console.log(' [antiCrash] :: Uncaught Exception Monitor/Catch'); + console.log(error); + console.log('Information supplémentaire:', origin); + }); + + process.on('beforeExit', (code) => { + console.log(' [antiCrash] :: Before Exit'); + console.log('Code de sortie:', code); + }); + + process.on('exit', (code) => { + console.log(' [antiCrash] :: Exit'); + console.log('Code de sortie:', code); + }); } }; \ No newline at end of file diff --git a/fonctions/loadCommands.ts b/fonctions/loadCommands.ts index 129eef3..c18f1e1 100644 --- a/fonctions/loadCommands.ts +++ b/fonctions/loadCommands.ts @@ -12,11 +12,12 @@ module.exports = function loadCommands(client: any, dir: string) { delete require.cache[require.resolve(filePath)]; const command = require(filePath); const fileName = file.replace(/\.js|\.ts/g, ''); - command.name = fileName; + if (!command.name) command.name = fileName; if (!command.category) { const parentDir = path.basename(path.dirname(filePath)); command.category = parentDir === 'commands' ? 'other' : parentDir; } + if (!command.permission) command.permission = 0; client.commands.set(fileName, command); if (command.aliases) { command.aliases.forEach((alias: string) => {