pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "../utils/defaultEmbeds.js"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 2 | import { CommandInteraction, GuildChannel, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 3 | import { SlashCommandBuilder } from "@discordjs/builders"; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 4 | // @ts-expect-error |
| 5 | import type { WrappedCheck } from "jshaiku"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 7 | import client from "../utils/client.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 8 | import addPlural from "../utils/plurals.js"; |
| 9 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 10 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 11 | const command = new SlashCommandBuilder().setName("categorise").setDescription("Categorises your servers channels"); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 12 | |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 13 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 14 | const channels = interaction.guild.channels.cache.filter((c) => c.type !== "GUILD_CATEGORY"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | const categorised = {}; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 16 | await interaction.reply({ embeds: LoadingEmbed, ephemeral: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | const predicted = {}; |
| 18 | const types = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 19 | 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"] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | }; |
| 29 | for (const c of channels.values()) { |
| 30 | for (const type in types) { |
| 31 | for (const word of types[type]) { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 32 | if (c.name.toLowerCase().includes(word)) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | predicted[c.id] = predicted[c.id] ?? []; |
| 34 | predicted[c.id].push(type); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 38 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 39 | let m; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | for (const c of channels) { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 41 | // convert channel to a channel if its a string |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 42 | let channel: string | GuildChannel; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 43 | if (typeof c === "string") channel = interaction.guild.channels.cache.get(channel as string).id; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | else channel = (c[0] as unknown as GuildChannel).id; |
| 45 | console.log(channel); |
| 46 | if (!predicted[channel]) predicted[channel] = []; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 47 | 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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 53 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 54 | ) |
| 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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 64 | .setPlaceholder("Select all types that apply to this channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 65 | .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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 78 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id"))), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | new MessageButton() |
| 80 | .setLabel('Use "Other"') |
| 81 | .setCustomId("reject") |
| 82 | .setStyle("SECONDARY") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 83 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id"))) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | ]) |
| 85 | ] |
| 86 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 87 | let i; |
| 88 | try { |
| 89 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 90 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | 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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 99 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join( |
| 100 | ", " |
| 101 | )}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | ) |
| 103 | .setFooter({ text: "Message timed out" }) |
| 104 | ] |
| 105 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 106 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 107 | i.deferUpdate(); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 108 | let selected; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | 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 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 118 | categorised[channel] = selected; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 119 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 120 | console.log(categorised); |
| 121 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 122 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 123 | const check = (_interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 124 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 125 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 126 | |
| 127 | export { command }; |
| 128 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 129 | export { check }; |