blob: e4346d6110dc4a27ae49ccb9c240bf324381b776 [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";
pineafanad54d752022-04-18 19:01:43 +010010import { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +010011import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +010012import client from "../utils/client.js";
pineafan73a7c4a2022-07-24 10:38:04 +010013import addPlural from "../utils/plurals.js";
14import getEmojiByName from "../utils/getEmojiByName.js";
pineafanad54d752022-04-18 19:01:43 +010015
pineafane23c4ec2022-07-27 21:56:27 +010016const command = new SlashCommandBuilder()
pineafanad54d752022-04-18 19:01:43 +010017 .setName("categorise")
pineafan63fc5e22022-08-04 22:04:10 +010018 .setDescription("Categorises your servers channels");
pineafanad54d752022-04-18 19:01:43 +010019
Skyler Grey75ea9172022-08-06 10:22:23 +010020const callback = async (
21 interaction: CommandInteraction
22): Promise<void | unknown> => {
23 const channels = interaction.guild.channels.cache.filter(
24 (c) => c.type !== "GUILD_CATEGORY"
25 );
pineafan63fc5e22022-08-04 22:04:10 +010026 const categorised = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010027 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan63fc5e22022-08-04 22:04:10 +010028 const predicted = {};
29 const types = {
pineafan73a7c4a2022-07-24 10:38:04 +010030 general: ["general", "muted", "main", "topic", "discuss"],
31 commands: ["bot", "command", "music"],
32 images: ["pic", "selfies", "image"],
33 nsfw: ["porn", "nsfw", "sex"],
34 links: ["links"],
35 advertising: ["ads", "advert", "server", "partner"],
36 staff: ["staff", "mod", "admin"],
37 spam: ["spam"],
38 other: ["random"]
pineafan63fc5e22022-08-04 22:04:10 +010039 };
40 for (const c of channels.values()) {
41 for (const type in types) {
42 for (const word of types[type]) {
pineafanad54d752022-04-18 19:01:43 +010043 if (c.name.toLowerCase().includes(word)) {
pineafan63fc5e22022-08-04 22:04:10 +010044 predicted[c.id] = predicted[c.id] ?? [];
45 predicted[c.id].push(type);
pineafanad54d752022-04-18 19:01:43 +010046 }
47 }
48 }
pineafanad54d752022-04-18 19:01:43 +010049 }
pineafan73a7c4a2022-07-24 10:38:04 +010050 let m;
pineafan63fc5e22022-08-04 22:04:10 +010051 for (const c of channels) {
pineafan73a7c4a2022-07-24 10:38:04 +010052 // convert channel to a channel if its a string
pineafanbd02b4a2022-08-05 22:01:38 +010053 let channel: string | GuildChannel;
Skyler Grey75ea9172022-08-06 10:22:23 +010054 if (typeof c === "string")
55 channel = interaction.guild.channels.cache.get(
56 channel as string
57 ).id;
pineafan63fc5e22022-08-04 22:04:10 +010058 else channel = (c[0] as unknown as GuildChannel).id;
59 console.log(channel);
60 if (!predicted[channel]) predicted[channel] = [];
Skyler Grey75ea9172022-08-06 10:22:23 +010061 m = await interaction.editReply({
62 embeds: [
63 new EmojiEmbed()
64 .setTitle("Categorise")
65 .setDescription(
66 `Select all types that apply to <#${channel}>.\n\n` +
67 `${addPlural(
68 predicted[channel].length,
69 "Suggestion"
70 )}: ${predicted[channel].join(", ")}`
71 )
72 .setEmoji("CHANNEL.CATEGORY.CREATE")
73 .setStatus("Success")
74 ],
75 components: [
76 new MessageActionRow().addComponents([
77 new MessageSelectMenu()
78 .setCustomId("selected")
79 .setMaxValues(Object.keys(types).length)
80 .setMinValues(1)
81 .setPlaceholder(
82 "Select all types that apply to this channel"
83 )
84 .setOptions(
85 Object.keys(types).map((type) => ({
86 label: type,
87 value: type
88 }))
89 )
90 ]),
91 new MessageActionRow().addComponents([
92 new MessageButton()
93 .setLabel("Accept Suggestion")
94 .setCustomId("accept")
95 .setStyle("SUCCESS")
96 .setDisabled(predicted[channel].length === 0)
97 .setEmoji(
98 client.emojis.cache.get(
99 getEmojiByName("ICONS.TICK", "id")
100 )
101 ),
102 new MessageButton()
103 .setLabel('Use "Other"')
104 .setCustomId("reject")
105 .setStyle("SECONDARY")
106 .setEmoji(
107 client.emojis.cache.get(
108 getEmojiByName("ICONS.CROSS", "id")
109 )
110 )
111 ])
112 ]
113 });
pineafan73a7c4a2022-07-24 10:38:04 +0100114 let i;
115 try {
116 i = await m.awaitMessageComponent({ time: 300000 });
117 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 return await interaction.editReply({
119 embeds: [
120 new EmojiEmbed()
121 .setTitle("Categorise")
122 .setEmoji("CHANNEL.CATEGORY.DELETE")
123 .setStatus("Danger")
124 .setDescription(
125 `Select all types that apply to <#${channel}>.\n\n` +
126 `${addPlural(
127 predicted[channel].length,
128 "Suggestion"
129 )}: ${predicted[channel].join(", ")}`
130 )
131 .setFooter({ text: "Message timed out" })
132 ]
133 });
pineafan73a7c4a2022-07-24 10:38:04 +0100134 }
pineafan63fc5e22022-08-04 22:04:10 +0100135 i.deferUpdate();
pineafan73a7c4a2022-07-24 10:38:04 +0100136 let selected;
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 if (i.customId === "select") {
138 selected = i.values;
139 }
140 if (i.customId === "accept") {
141 selected = predicted[channel];
142 }
143 if (i.customId === "reject") {
144 selected = ["other"];
145 }
pineafan63fc5e22022-08-04 22:04:10 +0100146 categorised[channel] = selected;
pineafan73a7c4a2022-07-24 10:38:04 +0100147 }
pineafan63fc5e22022-08-04 22:04:10 +0100148 console.log(categorised);
149};
pineafanad54d752022-04-18 19:01:43 +0100150
Skyler Grey75ea9172022-08-06 10:22:23 +0100151const check = (
152 _interaction: CommandInteraction,
153 _defaultCheck: WrappedCheck
154) => {
pineafanad54d752022-04-18 19:01:43 +0100155 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100156};
pineafanad54d752022-04-18 19:01:43 +0100157
158export { command };
159export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100160export { check };