diff --git a/src/commands/misc/archive.ts b/src/commands/misc/archive.ts index 5830b45..6f6c148 100644 --- a/src/commands/misc/archive.ts +++ b/src/commands/misc/archive.ts @@ -1,8 +1,8 @@ import { SlashCommandBuilder } from '@discordjs/builders'; -import { ChannelType, Client, CommandInteraction, EmbedBuilder } from 'discord.js'; +import { CategoryChannelResolvable, ChannelType, Client, CommandInteraction, EmbedBuilder, NonThreadGuildBasedChannel } from 'discord.js'; +import '../../modules/string'; import { getLocale, getLocalizations } from '../../utils/locales'; import { getFilename } from '../../utils/misc'; -import '../../modules/string'; export default { data: (client: Client) => { @@ -32,13 +32,13 @@ export default { interaction: async (interaction: CommandInteraction, client: Client) => { const loc = getLocale(client, interaction.locale); - const desired_cat = interaction.options.get(client + const desiredCat = 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) { + if (!desiredCat) { // Sends a list of commands sorted into categories return interaction.reply({ @@ -52,58 +52,71 @@ export default { } // If a category is specified - const clean_cat = ['L1', 'L2', 'L3', 'M1', 'M2']; - const channel = clean_cat.includes(desired_cat); + const cleanCat = ['L1', 'L2', 'L3', 'M1', 'M2']; + const channel = cleanCat.includes(desiredCat); if (!channel) { // Category doesn't exist or is not included return interaction.reply({ - content: `${loc.get('c_archive3')} \`${desired_cat}\``, + content: `${loc.get('c_archive3')} \`${desiredCat}\``, 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); + allChannel?.then(async channelGuild => { + // Retrieve category to archive + const catToArchive = channelGuild + .filter(chan => chan?.type == ChannelType.GuildCategory) + .filter(chan => chan?.name == desiredCat); - 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); + // Create/Retrieve the archive category + const catArchivedName = 'archive - ' + desiredCat; + const getCatArchived = () => channelGuild + .filter(chan => chan?.type == ChannelType.GuildCategory) + .filter(chan => chan?.name == catArchivedName); - 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) { + if (getCatArchived().size == 0) { + await interaction.guild?.channels + .create({ name: catArchivedName, type: ChannelType.GuildCategory }); + } + const catArchived = getCatArchived().at(0); + + const allChannelDesired = channelGuild + .filter(chan => chan?.type == 0) + .filter(chan => chan?.parentId == catToArchive.map(cat => cat?.id)[0]); + + // If no channels in the source category + if (allChannelDesired.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 loc.get('c_archive7') ), ], }); } - 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'); + // Move channels to the archived category + allChannelDesired.forEach(elem => elem?.setParent(catArchived?.id as string)); + return interaction.reply({ embeds: [ new EmbedBuilder() .setColor('Blurple') - .setTitle(loc.get('c_archive4') + cat_to_archive_name + loc.get('c_archive5') + cat_archived_name + '`') + .setTitle(loc.get('c_archive4') + + '`' + + catToArchive.map(cat => cat?.name) + + '`' + + loc.get('c_archive5') + + '`' + + catArchivedName + + '`') .setDescription( - // Loads the description - // according to the user's locals - list_cg_moved + allChannelDesired + .map(cgD => cgD?.name).toString().replaceAll(',', '\n') ), ], });