blob: b0527835aa58ef0965b64c5ac65f79784c7992c4 [file] [log] [blame]
pineafan73a7c4a2022-07-24 10:38:04 +01001import { CommandInteraction, GuildChannel, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js";
2import { SelectMenuOption, SlashCommandBuilder } from "@discordjs/builders";
pineafanad54d752022-04-18 19:01:43 +01003import { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan4edb7762022-06-26 19:21:04 +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
pineafan02ba0232022-07-24 22:16:15 +01009const command = new SlashCommandBuilder() // TODO: remove for release
pineafanad54d752022-04-18 19:01:43 +010010 .setName("categorise")
11 .setDescription("Categorises your servers channels")
12
pineafan4edb7762022-06-26 19:21:04 +010013const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafanad54d752022-04-18 19:01:43 +010014 let channels = interaction.guild.channels.cache.filter(c => c.type !== "GUILD_CATEGORY");
15 let categorised = {}
pineafan4edb7762022-06-26 19:21:04 +010016 await interaction.reply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010017 .setTitle("Loading...")
18 .setEmoji("NUCLEUS.LOADING")
19 .setStatus("Success")
20 ], ephemeral: true});
pineafan73a7c4a2022-07-24 10:38:04 +010021 let predicted = {}
22 let types = {
23 general: ["general", "muted", "main", "topic", "discuss"],
24 commands: ["bot", "command", "music"],
25 images: ["pic", "selfies", "image"],
26 nsfw: ["porn", "nsfw", "sex"],
27 links: ["links"],
28 advertising: ["ads", "advert", "server", "partner"],
29 staff: ["staff", "mod", "admin"],
30 spam: ["spam"],
31 other: ["random"]
32 }
pineafanad54d752022-04-18 19:01:43 +010033 for (let c of channels.values()) {
pineafanad54d752022-04-18 19:01:43 +010034 for (let type in types) {
35 for (let word of types[type]) {
36 if (c.name.toLowerCase().includes(word)) {
pineafan73a7c4a2022-07-24 10:38:04 +010037 predicted[c.id] = predicted[c.id] ?? []
38 predicted[c.id].push(type)
pineafanad54d752022-04-18 19:01:43 +010039 }
40 }
41 }
pineafanad54d752022-04-18 19:01:43 +010042 }
pineafan73a7c4a2022-07-24 10:38:04 +010043 let m;
44 for (let c of channels) {
45 // convert channel to a channel if its a string
46 let channel: any
pineafan73a7c4a2022-07-24 10:38:04 +010047 if (typeof c === "string") channel = interaction.guild.channels.cache.get(channel).id
pineafan02ba0232022-07-24 22:16:15 +010048 else channel = (c[0] as unknown as GuildChannel).id
pineafan73a7c4a2022-07-24 10:38:04 +010049 console.log(channel)
50 if (!predicted[channel]) predicted[channel] = []
51 m = await interaction.editReply({embeds: [new EmojiEmbed()
52 .setTitle("Categorise")
53 .setDescription(`Select all types that apply to <#${channel}>.\n\n` +
54 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`)
55 .setEmoji("CHANNEL.CATEGORY.CREATE")
56 .setStatus("Success")
57 ], components: [
58 new MessageActionRow().addComponents([new MessageSelectMenu()
59 .setCustomId("selected")
60 .setMaxValues(Object.keys(types).length)
61 .setMinValues(1)
62 .setPlaceholder("Select all types that apply to this channel")
63 .setOptions(Object.keys(types).map(type => ({label: type, value: type})))
64 ]),
65 new MessageActionRow().addComponents([
66 new MessageButton().setLabel("Accept Suggestion").setCustomId("accept").setStyle("SUCCESS").setDisabled(predicted[channel].length === 0)
67 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id"))),
68 new MessageButton().setLabel("Use \"Other\"").setCustomId("reject").setStyle("SECONDARY")
69 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id")))
70 ])
71 ]})
72 let i;
73 try {
74 i = await m.awaitMessageComponent({ time: 300000 });
75 } catch (e) {
76 return await interaction.editReply({embeds: [new EmojiEmbed()
77 .setTitle("Categorise")
78 .setEmoji("CHANNEL.CATEGORY.DELETE")
79 .setStatus("Danger")
80 .setDescription(`Select all types that apply to <#${channel}>.\n\n` +
81 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`)
82 .setFooter({text: "Message timed out"})
83 ]})
84 }
85 i.deferUpdate()
86 let selected;
87 if (i.customId === "select") { selected = i.values; }
88 if (i.customId === "accept") { selected = predicted[channel]; }
89 if (i.customId === "reject") { selected = ["other"]; }
90 categorised[channel] = selected
91 }
92 console.log(categorised)
pineafanad54d752022-04-18 19:01:43 +010093}
94
95const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
96 return true;
97}
98
99export { command };
100export { callback };
101export { check };