blob: 2da2be3213d1504d994dd45258d46800613cc44e [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "../utils/defaultEmbeds.js";
Skyler Grey11236ba2022-08-08 21:13:33 +01002import { CommandInteraction, GuildChannel, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js";
pineafane23c4ec2022-07-27 21:56:27 +01003import { SlashCommandBuilder } from "@discordjs/builders";
Skyler Greyc634e2b2022-08-06 17:50:48 +01004// @ts-expect-error
5import type { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +01006import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +01007import client from "../utils/client.js";
pineafan73a7c4a2022-07-24 10:38:04 +01008import addPlural from "../utils/plurals.js";
9import getEmojiByName from "../utils/getEmojiByName.js";
pineafanad54d752022-04-18 19:01:43 +010010
Skyler Grey11236ba2022-08-08 21:13:33 +010011const command = new SlashCommandBuilder().setName("categorise").setDescription("Categorises your servers channels");
pineafanad54d752022-04-18 19:01:43 +010012
Skyler Greyc634e2b2022-08-06 17:50:48 +010013const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey11236ba2022-08-08 21:13:33 +010014 const channels = interaction.guild.channels.cache.filter((c) => c.type !== "GUILD_CATEGORY");
pineafan63fc5e22022-08-04 22:04:10 +010015 const categorised = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010016 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan63fc5e22022-08-04 22:04:10 +010017 const predicted = {};
18 const types = {
pineafan73a7c4a2022-07-24 10:38:04 +010019 general: ["general", "muted", "main", "topic", "discuss"],
20 commands: ["bot", "command", "music"],
21 images: ["pic", "selfies", "image"],
22 nsfw: ["porn", "nsfw", "sex"],
23 links: ["links"],
24 advertising: ["ads", "advert", "server", "partner"],
25 staff: ["staff", "mod", "admin"],
26 spam: ["spam"],
27 other: ["random"]
pineafan63fc5e22022-08-04 22:04:10 +010028 };
29 for (const c of channels.values()) {
30 for (const type in types) {
31 for (const word of types[type]) {
pineafanad54d752022-04-18 19:01:43 +010032 if (c.name.toLowerCase().includes(word)) {
pineafan63fc5e22022-08-04 22:04:10 +010033 predicted[c.id] = predicted[c.id] ?? [];
34 predicted[c.id].push(type);
pineafanad54d752022-04-18 19:01:43 +010035 }
36 }
37 }
pineafanad54d752022-04-18 19:01:43 +010038 }
pineafan73a7c4a2022-07-24 10:38:04 +010039 let m;
pineafan63fc5e22022-08-04 22:04:10 +010040 for (const c of channels) {
pineafan73a7c4a2022-07-24 10:38:04 +010041 // convert channel to a channel if its a string
pineafanbd02b4a2022-08-05 22:01:38 +010042 let channel: string | GuildChannel;
Skyler Grey11236ba2022-08-08 21:13:33 +010043 if (typeof c === "string") channel = interaction.guild.channels.cache.get(channel as string).id;
pineafan63fc5e22022-08-04 22:04:10 +010044 else channel = (c[0] as unknown as GuildChannel).id;
45 console.log(channel);
46 if (!predicted[channel]) predicted[channel] = [];
Skyler Grey75ea9172022-08-06 10:22:23 +010047 m = await interaction.editReply({
48 embeds: [
49 new EmojiEmbed()
50 .setTitle("Categorise")
51 .setDescription(
52 `Select all types that apply to <#${channel}>.\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010053 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`
Skyler Grey75ea9172022-08-06 10:22:23 +010054 )
55 .setEmoji("CHANNEL.CATEGORY.CREATE")
56 .setStatus("Success")
57 ],
58 components: [
59 new MessageActionRow().addComponents([
60 new MessageSelectMenu()
61 .setCustomId("selected")
62 .setMaxValues(Object.keys(types).length)
63 .setMinValues(1)
Skyler Grey11236ba2022-08-08 21:13:33 +010064 .setPlaceholder("Select all types that apply to this channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010065 .setOptions(
66 Object.keys(types).map((type) => ({
67 label: type,
68 value: type
69 }))
70 )
71 ]),
72 new MessageActionRow().addComponents([
73 new MessageButton()
74 .setLabel("Accept Suggestion")
75 .setCustomId("accept")
76 .setStyle("SUCCESS")
77 .setDisabled(predicted[channel].length === 0)
Skyler Grey11236ba2022-08-08 21:13:33 +010078 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id"))),
Skyler Grey75ea9172022-08-06 10:22:23 +010079 new MessageButton()
80 .setLabel('Use "Other"')
81 .setCustomId("reject")
82 .setStyle("SECONDARY")
Skyler Grey11236ba2022-08-08 21:13:33 +010083 .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id")))
Skyler Grey75ea9172022-08-06 10:22:23 +010084 ])
85 ]
86 });
pineafan73a7c4a2022-07-24 10:38:04 +010087 let i;
88 try {
89 i = await m.awaitMessageComponent({ time: 300000 });
90 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010091 return await interaction.editReply({
92 embeds: [
93 new EmojiEmbed()
94 .setTitle("Categorise")
95 .setEmoji("CHANNEL.CATEGORY.DELETE")
96 .setStatus("Danger")
97 .setDescription(
98 `Select all types that apply to <#${channel}>.\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010099 `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(
100 ", "
101 )}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 )
103 .setFooter({ text: "Message timed out" })
104 ]
105 });
pineafan73a7c4a2022-07-24 10:38:04 +0100106 }
pineafan63fc5e22022-08-04 22:04:10 +0100107 i.deferUpdate();
pineafan73a7c4a2022-07-24 10:38:04 +0100108 let selected;
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 if (i.customId === "select") {
110 selected = i.values;
111 }
112 if (i.customId === "accept") {
113 selected = predicted[channel];
114 }
115 if (i.customId === "reject") {
116 selected = ["other"];
117 }
pineafan63fc5e22022-08-04 22:04:10 +0100118 categorised[channel] = selected;
pineafan73a7c4a2022-07-24 10:38:04 +0100119 }
pineafan63fc5e22022-08-04 22:04:10 +0100120 console.log(categorised);
121};
pineafanad54d752022-04-18 19:01:43 +0100122
Skyler Grey11236ba2022-08-08 21:13:33 +0100123const check = (_interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
pineafanad54d752022-04-18 19:01:43 +0100124 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100125};
pineafanad54d752022-04-18 19:01:43 +0100126
127export { command };
128export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100129export { check };