blob: 8c25eb2815a854c3880e65d58820b93fe3862481 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "../utils/defaultEmbeds.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01002import {
3 CommandInteraction,
4 GuildChannel,
5 MessageActionRow,
6 MessageButton,
7 MessageSelectMenu
8} from "discord.js";
pineafane23c4ec2022-07-27 21:56:27 +01009import { SlashCommandBuilder } from "@discordjs/builders";
Skyler Greyc634e2b2022-08-06 17:50:48 +010010// @ts-expect-error
11import type { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +010012import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +010013import client from "../utils/client.js";
pineafan73a7c4a2022-07-24 10:38:04 +010014import addPlural from "../utils/plurals.js";
15import getEmojiByName from "../utils/getEmojiByName.js";
pineafanad54d752022-04-18 19:01:43 +010016
pineafane23c4ec2022-07-27 21:56:27 +010017const command = new SlashCommandBuilder()
pineafanad54d752022-04-18 19:01:43 +010018 .setName("categorise")
pineafan63fc5e22022-08-04 22:04:10 +010019 .setDescription("Categorises your servers channels");
pineafanad54d752022-04-18 19:01:43 +010020
Skyler Greyc634e2b2022-08-06 17:50:48 +010021const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey75ea9172022-08-06 10:22:23 +010022 const channels = interaction.guild.channels.cache.filter(
23 (c) => c.type !== "GUILD_CATEGORY"
24 );
pineafan63fc5e22022-08-04 22:04:10 +010025 const categorised = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010026 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan63fc5e22022-08-04 22:04:10 +010027 const predicted = {};
28 const types = {
pineafan73a7c4a2022-07-24 10:38:04 +010029 general: ["general", "muted", "main", "topic", "discuss"],
30 commands: ["bot", "command", "music"],
31 images: ["pic", "selfies", "image"],
32 nsfw: ["porn", "nsfw", "sex"],
33 links: ["links"],
34 advertising: ["ads", "advert", "server", "partner"],
35 staff: ["staff", "mod", "admin"],
36 spam: ["spam"],
37 other: ["random"]
pineafan63fc5e22022-08-04 22:04:10 +010038 };
39 for (const c of channels.values()) {
40 for (const type in types) {
41 for (const word of types[type]) {
pineafanad54d752022-04-18 19:01:43 +010042 if (c.name.toLowerCase().includes(word)) {
pineafan63fc5e22022-08-04 22:04:10 +010043 predicted[c.id] = predicted[c.id] ?? [];
44 predicted[c.id].push(type);
pineafanad54d752022-04-18 19:01:43 +010045 }
46 }
47 }
pineafanad54d752022-04-18 19:01:43 +010048 }
pineafan73a7c4a2022-07-24 10:38:04 +010049 let m;
pineafan63fc5e22022-08-04 22:04:10 +010050 for (const c of channels) {
pineafan73a7c4a2022-07-24 10:38:04 +010051 // convert channel to a channel if its a string
pineafanbd02b4a2022-08-05 22:01:38 +010052 let channel: string | GuildChannel;
Skyler Grey75ea9172022-08-06 10:22:23 +010053 if (typeof c === "string")
54 channel = interaction.guild.channels.cache.get(
55 channel as string
56 ).id;
pineafan63fc5e22022-08-04 22:04:10 +010057 else channel = (c[0] as unknown as GuildChannel).id;
58 console.log(channel);
59 if (!predicted[channel]) predicted[channel] = [];
Skyler Grey75ea9172022-08-06 10:22:23 +010060 m = await interaction.editReply({
61 embeds: [
62 new EmojiEmbed()
63 .setTitle("Categorise")
64 .setDescription(
65 `Select all types that apply to <#${channel}>.\n\n` +
66 `${addPlural(
67 predicted[channel].length,
68 "Suggestion"
69 )}: ${predicted[channel].join(", ")}`
70 )
71 .setEmoji("CHANNEL.CATEGORY.CREATE")
72 .setStatus("Success")
73 ],
74 components: [
75 new MessageActionRow().addComponents([
76 new MessageSelectMenu()
77 .setCustomId("selected")
78 .setMaxValues(Object.keys(types).length)
79 .setMinValues(1)
80 .setPlaceholder(
81 "Select all types that apply to this channel"
82 )
83 .setOptions(
84 Object.keys(types).map((type) => ({
85 label: type,
86 value: type
87 }))
88 )
89 ]),
90 new MessageActionRow().addComponents([
91 new MessageButton()
92 .setLabel("Accept Suggestion")
93 .setCustomId("accept")
94 .setStyle("SUCCESS")
95 .setDisabled(predicted[channel].length === 0)
96 .setEmoji(
97 client.emojis.cache.get(
98 getEmojiByName("ICONS.TICK", "id")
99 )
100 ),
101 new MessageButton()
102 .setLabel('Use "Other"')
103 .setCustomId("reject")
104 .setStyle("SECONDARY")
105 .setEmoji(
106 client.emojis.cache.get(
107 getEmojiByName("ICONS.CROSS", "id")
108 )
109 )
110 ])
111 ]
112 });
pineafan73a7c4a2022-07-24 10:38:04 +0100113 let i;
114 try {
115 i = await m.awaitMessageComponent({ time: 300000 });
116 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 return await interaction.editReply({
118 embeds: [
119 new EmojiEmbed()
120 .setTitle("Categorise")
121 .setEmoji("CHANNEL.CATEGORY.DELETE")
122 .setStatus("Danger")
123 .setDescription(
124 `Select all types that apply to <#${channel}>.\n\n` +
125 `${addPlural(
126 predicted[channel].length,
127 "Suggestion"
128 )}: ${predicted[channel].join(", ")}`
129 )
130 .setFooter({ text: "Message timed out" })
131 ]
132 });
pineafan73a7c4a2022-07-24 10:38:04 +0100133 }
pineafan63fc5e22022-08-04 22:04:10 +0100134 i.deferUpdate();
pineafan73a7c4a2022-07-24 10:38:04 +0100135 let selected;
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 if (i.customId === "select") {
137 selected = i.values;
138 }
139 if (i.customId === "accept") {
140 selected = predicted[channel];
141 }
142 if (i.customId === "reject") {
143 selected = ["other"];
144 }
pineafan63fc5e22022-08-04 22:04:10 +0100145 categorised[channel] = selected;
pineafan73a7c4a2022-07-24 10:38:04 +0100146 }
pineafan63fc5e22022-08-04 22:04:10 +0100147 console.log(categorised);
148};
pineafanad54d752022-04-18 19:01:43 +0100149
Skyler Grey75ea9172022-08-06 10:22:23 +0100150const check = (
151 _interaction: CommandInteraction,
152 _defaultCheck: WrappedCheck
153) => {
pineafanad54d752022-04-18 19:01:43 +0100154 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100155};
pineafanad54d752022-04-18 19:01:43 +0100156
157export { command };
158export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100159export { check };