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