blob: 4ba582d6ee678ab37db3142fee7e11a439399b04 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "../utils/defaultEmbeds.js";
pineafan73a7c4a2022-07-24 10:38:04 +01002import { CommandInteraction, GuildChannel, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js";
pineafane23c4ec2022-07-27 21:56:27 +01003import { SlashCommandBuilder } from "@discordjs/builders";
pineafanad54d752022-04-18 19:01:43 +01004import { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +01006import client from "../utils/client.js";
pineafan73a7c4a2022-07-24 10:38:04 +01007import addPlural from "../utils/plurals.js";
8import getEmojiByName from "../utils/getEmojiByName.js";
pineafanad54d752022-04-18 19:01:43 +01009
pineafane23c4ec2022-07-27 21:56:27 +010010const command = new SlashCommandBuilder()
pineafanad54d752022-04-18 19:01:43 +010011 .setName("categorise")
pineafan63fc5e22022-08-04 22:04:10 +010012 .setDescription("Categorises your servers channels");
pineafanad54d752022-04-18 19:01:43 +010013
pineafanbd02b4a2022-08-05 22:01:38 +010014const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010015 const channels = interaction.guild.channels.cache.filter(c => c.type !== "GUILD_CATEGORY");
16 const categorised = {};
pineafane23c4ec2022-07-27 21:56:27 +010017 await interaction.reply({embeds: LoadingEmbed, ephemeral: true});
pineafan63fc5e22022-08-04 22:04:10 +010018 const predicted = {};
19 const types = {
pineafan73a7c4a2022-07-24 10:38:04 +010020 general: ["general", "muted", "main", "topic", "discuss"],
21 commands: ["bot", "command", "music"],
22 images: ["pic", "selfies", "image"],
23 nsfw: ["porn", "nsfw", "sex"],
24 links: ["links"],
25 advertising: ["ads", "advert", "server", "partner"],
26 staff: ["staff", "mod", "admin"],
27 spam: ["spam"],
28 other: ["random"]
pineafan63fc5e22022-08-04 22:04:10 +010029 };
30 for (const c of channels.values()) {
31 for (const type in types) {
32 for (const word of types[type]) {
pineafanad54d752022-04-18 19:01:43 +010033 if (c.name.toLowerCase().includes(word)) {
pineafan63fc5e22022-08-04 22:04:10 +010034 predicted[c.id] = predicted[c.id] ?? [];
35 predicted[c.id].push(type);
pineafanad54d752022-04-18 19:01:43 +010036 }
37 }
38 }
pineafanad54d752022-04-18 19:01:43 +010039 }
pineafan73a7c4a2022-07-24 10:38:04 +010040 let m;
pineafan63fc5e22022-08-04 22:04:10 +010041 for (const c of channels) {
pineafan73a7c4a2022-07-24 10:38:04 +010042 // convert channel to a channel if its a string
pineafanbd02b4a2022-08-05 22:01:38 +010043 let channel: string | GuildChannel;
44 if (typeof c === "string") channel = interaction.guild.channels.cache.get(channel as string).id;
pineafan63fc5e22022-08-04 22:04:10 +010045 else channel = (c[0] as unknown as GuildChannel).id;
46 console.log(channel);
47 if (!predicted[channel]) predicted[channel] = [];
pineafan73a7c4a2022-07-24 10:38:04 +010048 m = await interaction.editReply({embeds: [new EmojiEmbed()
49 .setTitle("Categorise")
50 .setDescription(`Select all types that apply to <#${channel}>.\n\n` +
51 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`)
52 .setEmoji("CHANNEL.CATEGORY.CREATE")
53 .setStatus("Success")
54 ], components: [
55 new MessageActionRow().addComponents([new MessageSelectMenu()
56 .setCustomId("selected")
57 .setMaxValues(Object.keys(types).length)
58 .setMinValues(1)
59 .setPlaceholder("Select all types that apply to this channel")
60 .setOptions(Object.keys(types).map(type => ({label: type, value: type})))
61 ]),
62 new MessageActionRow().addComponents([
63 new MessageButton().setLabel("Accept Suggestion").setCustomId("accept").setStyle("SUCCESS").setDisabled(predicted[channel].length === 0)
64 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id"))),
65 new MessageButton().setLabel("Use \"Other\"").setCustomId("reject").setStyle("SECONDARY")
66 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id")))
67 ])
pineafan63fc5e22022-08-04 22:04:10 +010068 ]});
pineafan73a7c4a2022-07-24 10:38:04 +010069 let i;
70 try {
71 i = await m.awaitMessageComponent({ time: 300000 });
72 } catch (e) {
73 return await interaction.editReply({embeds: [new EmojiEmbed()
74 .setTitle("Categorise")
75 .setEmoji("CHANNEL.CATEGORY.DELETE")
76 .setStatus("Danger")
77 .setDescription(`Select all types that apply to <#${channel}>.\n\n` +
78 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`)
79 .setFooter({text: "Message timed out"})
pineafan63fc5e22022-08-04 22:04:10 +010080 ]});
pineafan73a7c4a2022-07-24 10:38:04 +010081 }
pineafan63fc5e22022-08-04 22:04:10 +010082 i.deferUpdate();
pineafan73a7c4a2022-07-24 10:38:04 +010083 let selected;
84 if (i.customId === "select") { selected = i.values; }
85 if (i.customId === "accept") { selected = predicted[channel]; }
86 if (i.customId === "reject") { selected = ["other"]; }
pineafan63fc5e22022-08-04 22:04:10 +010087 categorised[channel] = selected;
pineafan73a7c4a2022-07-24 10:38:04 +010088 }
pineafan63fc5e22022-08-04 22:04:10 +010089 console.log(categorised);
90};
pineafanad54d752022-04-18 19:01:43 +010091
pineafanbd02b4a2022-08-05 22:01:38 +010092const check = (_interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
pineafanad54d752022-04-18 19:01:43 +010093 return true;
pineafan63fc5e22022-08-04 22:04:10 +010094};
pineafanad54d752022-04-18 19:01:43 +010095
96export { command };
97export { callback };
98export { check };