PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../utils/defaults.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 2 | import { |
| 3 | CommandInteraction, |
| 4 | GuildChannel, |
| 5 | ActionRowBuilder, |
| 6 | ButtonBuilder, |
| 7 | ButtonStyle, |
| 8 | ChannelType, |
| 9 | StringSelectMenuBuilder, |
| 10 | APIMessageComponentEmoji |
| 11 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 12 | import { SlashCommandBuilder } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 13 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | import client from "../utils/client.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 15 | import addPlural from "../utils/plurals.js"; |
| 16 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 17 | |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 18 | const command = new SlashCommandBuilder().setName("categorize").setDescription("Categorizes your servers channels"); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 19 | |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 20 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 21 | const channels = interaction.guild!.channels.cache.filter((c) => c.type !== ChannelType.GuildCategory); |
| 22 | const categorized = {}; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | await interaction.reply({ embeds: LoadingEmbed, ephemeral: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | const predicted = {}; |
| 25 | const types = { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 26 | important: ["rule", "announcement", "alert", "info"], |
| 27 | general: ["general", "main", "topic", "discuss"], |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 28 | commands: ["bot", "command", "music"], |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 29 | images: ["pic", "selfies", "image", "gallery", "meme", "media"], |
| 30 | nsfw: ["porn", "nsfw", "sex", "lewd", "fetish"], |
| 31 | links: ["link"], |
| 32 | advertising: ["ads", "advert", "partner", "bump"], |
| 33 | staff: ["staff", "mod", "admin", "helper", "train"], |
| 34 | spam: ["spam", "count"], |
| 35 | logs: ["log"], |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 36 | other: ["random", "starboard"] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | }; |
| 38 | for (const c of channels.values()) { |
| 39 | for (const type in types) { |
| 40 | for (const word of types[type]) { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 41 | if (c.name.toLowerCase().includes(word)) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | predicted[c.id] = predicted[c.id] ?? []; |
| 43 | predicted[c.id].push(type); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 47 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 48 | let m; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | for (const c of channels) { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 50 | // convert channel to a channel if its a string |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 51 | let channel: string | GuildChannel; |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 52 | if (typeof c === "string") channel = interaction.guild!.channels.cache.get(c as string)!.id; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | else channel = (c[0] as unknown as GuildChannel).id; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | if (!predicted[channel]) predicted[channel] = []; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 55 | m = await interaction.editReply({ |
| 56 | embeds: [ |
| 57 | new EmojiEmbed() |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 58 | .setTitle("Categorize") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 59 | .setDescription( |
| 60 | `Select all types that apply to <#${channel}>.\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 61 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | ) |
| 63 | .setEmoji("CHANNEL.CATEGORY.CREATE") |
| 64 | .setStatus("Success") |
| 65 | ], |
| 66 | components: [ |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 67 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([ |
| 68 | new StringSelectMenuBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | .setCustomId("selected") |
| 70 | .setMaxValues(Object.keys(types).length) |
| 71 | .setMinValues(1) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 72 | .setPlaceholder("Select all types that apply to this channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | .setOptions( |
| 74 | Object.keys(types).map((type) => ({ |
| 75 | label: type, |
| 76 | value: type |
| 77 | })) |
| 78 | ) |
| 79 | ]), |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 80 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 81 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | .setLabel("Accept Suggestion") |
| 83 | .setCustomId("accept") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 84 | .setStyle(ButtonStyle.Success) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | .setDisabled(predicted[channel].length === 0) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 86 | .setEmoji( |
| 87 | client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id")) as APIMessageComponentEmoji |
| 88 | ), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 89 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | .setLabel('Use "Other"') |
| 91 | .setCustomId("reject") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 92 | .setStyle(ButtonStyle.Secondary) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 93 | .setEmoji( |
| 94 | client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id")) as APIMessageComponentEmoji |
| 95 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 96 | ]) |
| 97 | ] |
| 98 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 99 | let i; |
| 100 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 101 | i = await m.awaitMessageComponent({ |
| 102 | time: 300000, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 103 | filter: (i) => { |
| 104 | return ( |
| 105 | i.user.id === interaction.user.id && |
| 106 | i.channel!.id === interaction.channel!.id && |
| 107 | i.message.id === m.id |
| 108 | ); |
| 109 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 110 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 111 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | return await interaction.editReply({ |
| 113 | embeds: [ |
| 114 | new EmojiEmbed() |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 115 | .setTitle("Categorize") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 116 | .setEmoji("CHANNEL.CATEGORY.DELETE") |
| 117 | .setStatus("Danger") |
| 118 | .setDescription( |
| 119 | `Select all types that apply to <#${channel}>.\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 120 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join( |
| 121 | ", " |
| 122 | )}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 123 | ) |
| 124 | .setFooter({ text: "Message timed out" }) |
| 125 | ] |
| 126 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 127 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 128 | await i.deferUpdate(); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 129 | let selected; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 130 | if (i.customId === "select") { |
| 131 | selected = i.values; |
| 132 | } |
| 133 | if (i.customId === "accept") { |
| 134 | selected = predicted[channel]; |
| 135 | } |
| 136 | if (i.customId === "reject") { |
| 137 | selected = ["other"]; |
| 138 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 139 | categorized[channel] = selected; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 140 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 141 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 142 | |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 143 | const check = () => { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 144 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 145 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 146 | |
| 147 | export { command }; |
| 148 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 149 | export { check }; |