blob: f5660fe679818dd9e4e260a5af02a0d6e99892da [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "../utils/defaultEmbeds.js";
TheCodedProf21c08592022-09-13 14:14:43 -04002import { CommandInteraction, GuildChannel, ActionRowBuilder, ButtonBuilder, SelectMenuBuilder, ButtonStyle } from "discord.js";
pineafane23c4ec2022-07-27 21:56:27 +01003import { SlashCommandBuilder } from "@discordjs/builders";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +01005import client from "../utils/client.js";
pineafan73a7c4a2022-07-24 10:38:04 +01006import addPlural from "../utils/plurals.js";
7import getEmojiByName from "../utils/getEmojiByName.js";
pineafanad54d752022-04-18 19:01:43 +01008
Skyler Grey11236ba2022-08-08 21:13:33 +01009const command = new SlashCommandBuilder().setName("categorise").setDescription("Categorises your servers channels");
pineafanad54d752022-04-18 19:01:43 +010010
Skyler Greyc634e2b2022-08-06 17:50:48 +010011const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey11236ba2022-08-08 21:13:33 +010012 const channels = interaction.guild.channels.cache.filter((c) => c.type !== "GUILD_CATEGORY");
pineafan63fc5e22022-08-04 22:04:10 +010013 const categorised = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010014 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan63fc5e22022-08-04 22:04:10 +010015 const predicted = {};
16 const types = {
pineafan73a7c4a2022-07-24 10:38:04 +010017 general: ["general", "muted", "main", "topic", "discuss"],
18 commands: ["bot", "command", "music"],
19 images: ["pic", "selfies", "image"],
20 nsfw: ["porn", "nsfw", "sex"],
21 links: ["links"],
22 advertising: ["ads", "advert", "server", "partner"],
23 staff: ["staff", "mod", "admin"],
24 spam: ["spam"],
25 other: ["random"]
pineafan63fc5e22022-08-04 22:04:10 +010026 };
27 for (const c of channels.values()) {
28 for (const type in types) {
29 for (const word of types[type]) {
pineafanad54d752022-04-18 19:01:43 +010030 if (c.name.toLowerCase().includes(word)) {
pineafan63fc5e22022-08-04 22:04:10 +010031 predicted[c.id] = predicted[c.id] ?? [];
32 predicted[c.id].push(type);
pineafanad54d752022-04-18 19:01:43 +010033 }
34 }
35 }
pineafanad54d752022-04-18 19:01:43 +010036 }
pineafan73a7c4a2022-07-24 10:38:04 +010037 let m;
pineafan63fc5e22022-08-04 22:04:10 +010038 for (const c of channels) {
pineafan73a7c4a2022-07-24 10:38:04 +010039 // convert channel to a channel if its a string
pineafanbd02b4a2022-08-05 22:01:38 +010040 let channel: string | GuildChannel;
Skyler Grey11236ba2022-08-08 21:13:33 +010041 if (typeof c === "string") channel = interaction.guild.channels.cache.get(channel as string).id;
pineafan63fc5e22022-08-04 22:04:10 +010042 else channel = (c[0] as unknown as GuildChannel).id;
43 console.log(channel);
44 if (!predicted[channel]) predicted[channel] = [];
Skyler Grey75ea9172022-08-06 10:22:23 +010045 m = await interaction.editReply({
46 embeds: [
47 new EmojiEmbed()
48 .setTitle("Categorise")
49 .setDescription(
50 `Select all types that apply to <#${channel}>.\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010051 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`
Skyler Grey75ea9172022-08-06 10:22:23 +010052 )
53 .setEmoji("CHANNEL.CATEGORY.CREATE")
54 .setStatus("Success")
55 ],
56 components: [
TheCodedProf21c08592022-09-13 14:14:43 -040057 new ActionRowBuilder().addComponents([
58 new SelectMenuBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010059 .setCustomId("selected")
60 .setMaxValues(Object.keys(types).length)
61 .setMinValues(1)
Skyler Grey11236ba2022-08-08 21:13:33 +010062 .setPlaceholder("Select all types that apply to this channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010063 .setOptions(
64 Object.keys(types).map((type) => ({
65 label: type,
66 value: type
67 }))
68 )
69 ]),
TheCodedProf21c08592022-09-13 14:14:43 -040070 new ActionRowBuilder().addComponents([
71 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010072 .setLabel("Accept Suggestion")
73 .setCustomId("accept")
TheCodedProf21c08592022-09-13 14:14:43 -040074 .setStyle(ButtonStyle.Success)
Skyler Grey75ea9172022-08-06 10:22:23 +010075 .setDisabled(predicted[channel].length === 0)
Skyler Grey11236ba2022-08-08 21:13:33 +010076 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id"))),
TheCodedProf21c08592022-09-13 14:14:43 -040077 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010078 .setLabel('Use "Other"')
79 .setCustomId("reject")
TheCodedProf21c08592022-09-13 14:14:43 -040080 .setStyle(ButtonStyle.Secondary)
Skyler Grey11236ba2022-08-08 21:13:33 +010081 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id")))
Skyler Grey75ea9172022-08-06 10:22:23 +010082 ])
83 ]
84 });
pineafan73a7c4a2022-07-24 10:38:04 +010085 let i;
86 try {
87 i = await m.awaitMessageComponent({ time: 300000 });
88 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010089 return await interaction.editReply({
90 embeds: [
91 new EmojiEmbed()
92 .setTitle("Categorise")
93 .setEmoji("CHANNEL.CATEGORY.DELETE")
94 .setStatus("Danger")
95 .setDescription(
96 `Select all types that apply to <#${channel}>.\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010097 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(
98 ", "
99 )}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 )
101 .setFooter({ text: "Message timed out" })
102 ]
103 });
pineafan73a7c4a2022-07-24 10:38:04 +0100104 }
pineafan63fc5e22022-08-04 22:04:10 +0100105 i.deferUpdate();
pineafan73a7c4a2022-07-24 10:38:04 +0100106 let selected;
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 if (i.customId === "select") {
108 selected = i.values;
109 }
110 if (i.customId === "accept") {
111 selected = predicted[channel];
112 }
113 if (i.customId === "reject") {
114 selected = ["other"];
115 }
pineafan63fc5e22022-08-04 22:04:10 +0100116 categorised[channel] = selected;
pineafan73a7c4a2022-07-24 10:38:04 +0100117 }
pineafan63fc5e22022-08-04 22:04:10 +0100118 console.log(categorised);
119};
pineafanad54d752022-04-18 19:01:43 +0100120
PineaFan64486c42022-12-28 09:21:04 +0000121const check = (_interaction: CommandInteraction) => {
pineafanad54d752022-04-18 19:01:43 +0100122 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100123};
pineafanad54d752022-04-18 19:01:43 +0100124
125export { command };
126export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100127export { check };