blob: f89a899c9c26e8396c10ed002f3051ad4177e812 [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
9const command = new SlashCommandBuilder()
10 .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
47 console.log(c)
48 if (typeof c === "string") channel = interaction.guild.channels.cache.get(channel).id
49 // @ts-ignore
50 else channel = c[0].id
51 console.log(channel)
52 if (!predicted[channel]) predicted[channel] = []
53 m = await interaction.editReply({embeds: [new EmojiEmbed()
54 .setTitle("Categorise")
55 .setDescription(`Select all types that apply to <#${channel}>.\n\n` +
56 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`)
57 .setEmoji("CHANNEL.CATEGORY.CREATE")
58 .setStatus("Success")
59 ], components: [
60 new MessageActionRow().addComponents([new MessageSelectMenu()
61 .setCustomId("selected")
62 .setMaxValues(Object.keys(types).length)
63 .setMinValues(1)
64 .setPlaceholder("Select all types that apply to this channel")
65 .setOptions(Object.keys(types).map(type => ({label: type, value: type})))
66 ]),
67 new MessageActionRow().addComponents([
68 new MessageButton().setLabel("Accept Suggestion").setCustomId("accept").setStyle("SUCCESS").setDisabled(predicted[channel].length === 0)
69 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id"))),
70 new MessageButton().setLabel("Use \"Other\"").setCustomId("reject").setStyle("SECONDARY")
71 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id")))
72 ])
73 ]})
74 let i;
75 try {
76 i = await m.awaitMessageComponent({ time: 300000 });
77 } catch (e) {
78 return await interaction.editReply({embeds: [new EmojiEmbed()
79 .setTitle("Categorise")
80 .setEmoji("CHANNEL.CATEGORY.DELETE")
81 .setStatus("Danger")
82 .setDescription(`Select all types that apply to <#${channel}>.\n\n` +
83 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`)
84 .setFooter({text: "Message timed out"})
85 ]})
86 }
87 i.deferUpdate()
88 let selected;
89 if (i.customId === "select") { selected = i.values; }
90 if (i.customId === "accept") { selected = predicted[channel]; }
91 if (i.customId === "reject") { selected = ["other"]; }
92 categorised[channel] = selected
93 }
94 console.log(categorised)
pineafanad54d752022-04-18 19:01:43 +010095}
96
97const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
98 return true;
99}
100
101export { command };
102export { callback };
103export { check };