pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "../utils/defaultEmbeds.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | import { |
| 3 | CommandInteraction, |
| 4 | GuildChannel, |
| 5 | MessageActionRow, |
| 6 | MessageButton, |
| 7 | MessageSelectMenu |
| 8 | } from "discord.js"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 9 | import { SlashCommandBuilder } from "@discordjs/builders"; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 10 | // @ts-expect-error |
| 11 | import type { WrappedCheck } from "jshaiku"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 12 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | import client from "../utils/client.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 14 | import addPlural from "../utils/plurals.js"; |
| 15 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 16 | |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 17 | const command = new SlashCommandBuilder() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 18 | .setName("categorise") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | .setDescription("Categorises your servers channels"); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 20 | |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 21 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 22 | const channels = interaction.guild.channels.cache.filter( |
| 23 | (c) => c.type !== "GUILD_CATEGORY" |
| 24 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | const categorised = {}; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | await interaction.reply({ embeds: LoadingEmbed, ephemeral: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | const predicted = {}; |
| 28 | const types = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 29 | 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"] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | }; |
| 39 | for (const c of channels.values()) { |
| 40 | for (const type in types) { |
| 41 | for (const word of types[type]) { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 42 | if (c.name.toLowerCase().includes(word)) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | predicted[c.id] = predicted[c.id] ?? []; |
| 44 | predicted[c.id].push(type); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 48 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 49 | let m; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | for (const c of channels) { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 51 | // convert channel to a channel if its a string |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 52 | let channel: string | GuildChannel; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 53 | if (typeof c === "string") |
| 54 | channel = interaction.guild.channels.cache.get( |
| 55 | channel as string |
| 56 | ).id; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | else channel = (c[0] as unknown as GuildChannel).id; |
| 58 | console.log(channel); |
| 59 | if (!predicted[channel]) predicted[channel] = []; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | 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 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 113 | let i; |
| 114 | try { |
| 115 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 116 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | 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 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 133 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 134 | i.deferUpdate(); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 135 | let selected; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 136 | 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 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 145 | categorised[channel] = selected; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 146 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 147 | console.log(categorised); |
| 148 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 149 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 150 | const check = ( |
| 151 | _interaction: CommandInteraction, |
| 152 | _defaultCheck: WrappedCheck |
| 153 | ) => { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 154 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 155 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 156 | |
| 157 | export { command }; |
| 158 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 159 | export { check }; |