From 5d3b2638cc557ab32a38d902033a8a961de19d44 Mon Sep 17 00:00:00 2001 From: FRANCOIS tanya Date: Mon, 29 Aug 2022 18:41:32 +0200 Subject: [PATCH] archivage + prep --- src/commands/misc/archive.ts | 107 ++++++++++++++++++++++++++++++++ src/commands/misc/prep.ts | 117 +++++++++++++++++++++++++++++++++++ src/index.ts | 3 +- src/locales/fr.json | 21 +++++++ tsconfig.json | 4 +- 5 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 src/commands/misc/archive.ts create mode 100644 src/commands/misc/prep.ts diff --git a/src/commands/misc/archive.ts b/src/commands/misc/archive.ts new file mode 100644 index 0000000..f9585ac --- /dev/null +++ b/src/commands/misc/archive.ts @@ -0,0 +1,107 @@ +import { SlashCommandBuilder } from '@discordjs/builders'; +import { Locale } from 'discord-api-types/v9'; +import { CategoryChannel, ChannelType, Client, CommandInteraction, EmbedBuilder, Guild } from 'discord.js'; +import { getLocale, getLocalizations } from '../../utils/locales'; +import { getFilename } from '../../utils/misc'; +import '../../modules/string'; + +export default { + data: (client: Client) => { + const filename = getFilename(__filename); + return new SlashCommandBuilder() + .setName( + filename.toLowerCase()) + .setDescription(client.locales.get(client.config.default_lang) + ?.get(`c_${filename}_desc`) ?? '') + .setNameLocalizations( + getLocalizations(client, `c_${filename}_name`, true)) + .setDescriptionLocalizations( + getLocalizations(client, `c_${filename}_desc`)) + + // Command option + .addStringOption(option => option + .setName(client.locales.get(client.config.default_lang) + ?.get(`c_${filename}_opt1_name`) ?? '') + .setDescription(client.locales.get(client.config.default_lang) + ?.get(`c_${filename}_opt1_desc`) ?? '') + .setNameLocalizations( + getLocalizations(client, `c_${filename}_opt1_name`, true)) + .setDescriptionLocalizations( + getLocalizations(client, `c_${filename}_opt1_desc`)) + ); + }, + + interaction: async (interaction: CommandInteraction, client: Client) => { + const loc = getLocale(client, interaction.locale); + const desired_cat = interaction.options.get(client + .locales + .get(client.config.default_lang) + ?.get(`c_${getFilename(__filename)}_opt1_name`) ?? '')?.value as string; + + // If a category isn't specified + if (!desired_cat) { + + // Sends a list of commands sorted into categories + return interaction.reply({ embeds: [ + new EmbedBuilder() + .setColor('Blurple') + .setTitle(loc.get('c_archive1')) + .setDescription(loc.get('c_archive2')), + ] }); + } + + // If a category is specified + const clean_cat = ['L1', 'L2', 'L3', 'M1', 'M2']; + const channel = clean_cat.includes(desired_cat); + if (!channel) { + // Category doesn't exist or is not included + return interaction.reply({ + content: `${loc.get('c_archive3')} \`${desired_cat}\``, + ephemeral: true, + }); + } + + // Send information about the command + const allChannel = interaction.guild?.channels.fetch(); + allChannel?.then(channel_guild => { + const cat_to_archive = channel_guild.filter(chan => chan.type == ChannelType.GuildCategory).filter(chan => chan.name == desired_cat); + const cat_to_archive_id = cat_to_archive.map(cat => cat.id); + const cat_to_archive_name = cat_to_archive.map(cat => cat.name); + + const cat_archived = channel_guild.filter(chan => chan.type == ChannelType.GuildCategory).filter(chan => chan.name == 'archive - ' + desired_cat); + const cat_archived_id = cat_archived.map(cat => cat.id); + const cat_archived_name = cat_archived.map(cat => cat.name); + + const all_channel_desired = channel_guild.filter(chan => chan.type == 0).filter(chan => chan.parentId == cat_to_archive_id[0]); + if (all_channel_desired.size == 0) { + return interaction.reply({ embeds: [ + new EmbedBuilder() + .setColor('Blurple') + .setTitle(loc.get('c_archive6')) + .setDescription( + // Loads the description + // according to the user's locals + 'Catégorie déjà nettoyée' + ), + ] }); + } + const all_channel_desired_name = all_channel_desired.map(cg_d => cg_d.name); + console.log(all_channel_desired_name); + all_channel_desired.forEach(elem => + elem.setParent(cat_archived_id[0]) + ); + + const list_cg_moved = all_channel_desired_name.toString().replaceAll(',', '\n'); + return interaction.reply({ embeds: [ + new EmbedBuilder() + .setColor('Blurple') + .setTitle(loc.get('c_archive4') + cat_to_archive_name + loc.get('c_archive5') + cat_archived_name + '`') + .setDescription( + // Loads the description + // according to the user's locals + list_cg_moved + ), + ] }); + }); + }, +}; diff --git a/src/commands/misc/prep.ts b/src/commands/misc/prep.ts new file mode 100644 index 0000000..5894d50 --- /dev/null +++ b/src/commands/misc/prep.ts @@ -0,0 +1,117 @@ +import { SlashCommandBuilder } from '@discordjs/builders'; +import { Locale } from 'discord-api-types/v9'; +import { CategoryChannel, ChannelType, Client, CommandInteraction, EmbedBuilder, Guild } from 'discord.js'; +import { getLocale, getLocalizations } from '../../utils/locales'; +import { getFilename } from '../../utils/misc'; +import '../../modules/string'; + +export default { + data: (client: Client) => { + const filename = getFilename(__filename); + return new SlashCommandBuilder() + .setName( + filename.toLowerCase()) + .setDescription(client.locales.get(client.config.default_lang) + ?.get(`c_${filename}_desc`) ?? '') + .setNameLocalizations( + getLocalizations(client, `c_${filename}_name`, true)) + .setDescriptionLocalizations( + getLocalizations(client, `c_${filename}_desc`)) + + // Command option + .addStringOption(option => option + .setName(client.locales.get(client.config.default_lang) + ?.get(`c_${filename}_opt1_name`) ?? '') + .setDescription(client.locales.get(client.config.default_lang) + ?.get(`c_${filename}_opt1_desc`) ?? '') + .setNameLocalizations( + getLocalizations(client, `c_${filename}_opt1_name`, true)) + .setDescriptionLocalizations( + getLocalizations(client, `c_${filename}_opt1_desc`)) + ); + }, + + interaction: async (interaction: CommandInteraction, client: Client) => { + const loc = getLocale(client, interaction.locale); + const desired_cat = interaction.options.get(client + .locales + .get(client.config.default_lang) + ?.get(`c_${getFilename(__filename)}_opt1_name`) ?? '')?.value as string; + + // If a category isn't specified + if (!desired_cat) { + + // Sends a list of commands sorted into categories + return interaction.reply({ embeds: [ + new EmbedBuilder() + .setColor('Blurple') + .setTitle(loc.get('c_prep1')) + .setDescription(loc.get('c_prep2')), + ] }); + } + + // If a category is specified + const clean_cat = ['L1', 'L2', 'L3', 'M1', 'M2']; + const channel = clean_cat.includes(desired_cat); + if (!channel) { + // Category doesn't exist or is not included + return interaction.reply({ + content: `${loc.get('c_prep3')} \`${desired_cat}\``, + ephemeral: true, + }); + } + + // Send information about the command + const allChannel = interaction.guild?.channels.fetch(); + allChannel?.then(channel_guild => { + const cat_to_prep = channel_guild.filter(chan => chan.type == ChannelType.GuildCategory).filter(chan => chan.name == desired_cat); + const cat_to_prep_id = cat_to_prep.map(cat => cat.id); + const cat_to_prep_name = cat_to_prep.map(cat => cat.name); + + const cat_archived = channel_guild.filter(chan => chan.type == ChannelType.GuildCategory).filter(chan => chan.name == 'archive - ' + desired_cat); + const cat_archived_id = cat_archived.map(cat => cat.id); + const cat_archived_name = cat_archived.map(cat => cat.name); + + // console.log(cat_to_prep); + const all_channel_desired = channel_guild.filter(chan => chan.type == 0).filter(chan => chan.parentId == cat_to_prep_id[0]); + const all_channel_desired_name = all_channel_desired.map(c_d => c_d.name); + + let desc = ''; + + if (all_channel_desired_name.filter(cdn => cdn == 'général').length == 0) { + interaction.guild?.channels.create({ + name: 'général', + type: 0, + parent:cat_to_prep_id[0], + }); + desc = 'général créé \n'; + } + + + if (all_channel_desired_name.filter(cdn => cdn == 'informations').length == 0) { + interaction.guild?.channels.create({ + name: 'informations', + type: 0, + parent:cat_to_prep_id[0], + }); + + desc += 'informations créé \n'; + } + + if (desc == '') { + desc = 'Pas besoin de préparation'; + } + + return interaction.reply({ embeds: [ + new EmbedBuilder() + .setColor('Blurple') + .setTitle(loc.get('c_prep4') + cat_to_prep_name) + .setDescription( + // Loads the description + // according to the user's locals + desc, + ), + ] }); + }); + }, +}; diff --git a/src/index.ts b/src/index.ts index 7b85203..9bca0fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,7 +36,8 @@ const run = async () => { const commands_name = 'Commands'; await loadCommands(client) .then(() => console.log(logStart(commands_name, true))) - .catch(() => { + .catch(err => { + console.log(err); throw logStart(commands_name, false); }); diff --git a/src/locales/fr.json b/src/locales/fr.json index 90fddc6..7aed0b9 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -14,5 +14,26 @@ "c_help2": "`/help ` pour obtenir plus d'informations sur une commande.", "c_help3": "Impossible de trouver :", + "c_archive_name": "Nettoyer", + "c_archive_desc": "Nettoyage pour le passage à niveau", + "c_archive_opt1_name": "catégorie", + "c_archive_opt1_desc": "Nom de la catégorie à nettoyer", + "c_archive1": "Liste des catégories soumis au nettoyage", + "c_archive2": "`L1`, `L2`, `L3`, `M1`, `M2`", + "c_archive3": "Impossible de trouver/nettoyer le salon :", + "c_archive4": "Listes des Salons archivés de la catégorie `", + "c_archive5": "` vers `", + "c_archive6": "Nettoyage", + + "c_prep_name": "Préparation", + "c_prep_desc": "Préparation des salons généraux pour la nouvelle année", + "c_prep_opt1_name": "année", + "c_prep_opt1_desc": "Nom de l'année à préparer'", + "c_prep1": "Liste des catégories soumis à la préparation", + "c_prep2": "`L1`, `L2`, `L3`, `M1`, `M2`", + "c_prep3": "Impossible de trouver/nettoyer le salon :", + "c_prep4": "Listes des Salons préparés `", + + "u_time_at": "à" } diff --git a/tsconfig.json b/tsconfig.json index cb551e2..5d0dfd6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,9 @@ /* Language and Environment */ "target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ - // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + "lib": [ + "ES2021.String" + ], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */