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; |
| 54 | console.log(channel); |
| 55 | if (!predicted[channel]) predicted[channel] = []; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | m = await interaction.editReply({ |
| 57 | embeds: [ |
| 58 | new EmojiEmbed() |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 59 | .setTitle("Categorize") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | .setDescription( |
| 61 | `Select all types that apply to <#${channel}>.\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 62 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | ) |
| 64 | .setEmoji("CHANNEL.CATEGORY.CREATE") |
| 65 | .setStatus("Success") |
| 66 | ], |
| 67 | components: [ |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 68 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([ |
| 69 | new StringSelectMenuBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | .setCustomId("selected") |
| 71 | .setMaxValues(Object.keys(types).length) |
| 72 | .setMinValues(1) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 73 | .setPlaceholder("Select all types that apply to this channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 74 | .setOptions( |
| 75 | Object.keys(types).map((type) => ({ |
| 76 | label: type, |
| 77 | value: type |
| 78 | })) |
| 79 | ) |
| 80 | ]), |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 81 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 82 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 83 | .setLabel("Accept Suggestion") |
| 84 | .setCustomId("accept") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 85 | .setStyle(ButtonStyle.Success) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 86 | .setDisabled(predicted[channel].length === 0) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 87 | .setEmoji( |
| 88 | client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id")) as APIMessageComponentEmoji |
| 89 | ), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 90 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | .setLabel('Use "Other"') |
| 92 | .setCustomId("reject") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 93 | .setStyle(ButtonStyle.Secondary) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 94 | .setEmoji( |
| 95 | client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id")) as APIMessageComponentEmoji |
| 96 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | ]) |
| 98 | ] |
| 99 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 100 | let i; |
| 101 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 102 | i = await m.awaitMessageComponent({ |
| 103 | time: 300000, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 104 | filter: (i) => { |
| 105 | return ( |
| 106 | i.user.id === interaction.user.id && |
| 107 | i.channel!.id === interaction.channel!.id && |
| 108 | i.message.id === m.id |
| 109 | ); |
| 110 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 111 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 112 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | return await interaction.editReply({ |
| 114 | embeds: [ |
| 115 | new EmojiEmbed() |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 116 | .setTitle("Categorize") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | .setEmoji("CHANNEL.CATEGORY.DELETE") |
| 118 | .setStatus("Danger") |
| 119 | .setDescription( |
| 120 | `Select all types that apply to <#${channel}>.\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 121 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join( |
| 122 | ", " |
| 123 | )}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 124 | ) |
| 125 | .setFooter({ text: "Message timed out" }) |
| 126 | ] |
| 127 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 128 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 129 | await i.deferUpdate(); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 130 | let selected; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 131 | if (i.customId === "select") { |
| 132 | selected = i.values; |
| 133 | } |
| 134 | if (i.customId === "accept") { |
| 135 | selected = predicted[channel]; |
| 136 | } |
| 137 | if (i.customId === "reject") { |
| 138 | selected = ["other"]; |
| 139 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 140 | categorized[channel] = selected; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 141 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 142 | console.log(categorized); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 143 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 144 | |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 145 | const check = () => { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 146 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 147 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 148 | |
| 149 | export { command }; |
| 150 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 151 | export { check }; |