Botanique/src/commands/misc/archive.ts

113 lines
3.8 KiB
TypeScript
Raw Normal View History

import { SlashCommandBuilder } from '@discordjs/builders';
import { ChannelType, Client, CommandInteraction, EmbedBuilder } 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
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');
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
),
],
});
});
},
};