PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../utils/defaults.js"; |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 2 | import { CommandInteraction, GuildChannel, ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, StringSelectMenuBuilder, APIMessageComponentEmoji } from "discord.js"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 3 | import { SlashCommandBuilder } from "@discordjs/builders"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | import client from "../utils/client.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 6 | import addPlural from "../utils/plurals.js"; |
| 7 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 8 | |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 9 | const command = new SlashCommandBuilder().setName("categorize").setDescription("Categorizes your servers channels"); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 10 | |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 11 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 12 | const channels = interaction.guild!.channels.cache.filter((c) => c.type !== ChannelType.GuildCategory); |
| 13 | const categorized = {}; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 14 | await interaction.reply({ embeds: LoadingEmbed, ephemeral: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | const predicted = {}; |
| 16 | const types = { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 17 | important: ["rule", "announcement", "alert", "info"], |
| 18 | general: ["general", "main", "topic", "discuss"], |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 19 | commands: ["bot", "command", "music"], |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 20 | images: ["pic", "selfies", "image", "gallery", "meme", "media"], |
| 21 | nsfw: ["porn", "nsfw", "sex", "lewd", "fetish"], |
| 22 | links: ["link"], |
| 23 | advertising: ["ads", "advert", "partner", "bump"], |
| 24 | staff: ["staff", "mod", "admin", "helper", "train"], |
| 25 | spam: ["spam", "count"], |
| 26 | logs: ["log"], |
| 27 | other: ["random", "starboard"], |
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; |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 43 | 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] | 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() |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 50 | .setTitle("Categorize") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 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: [ |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 59 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents([ |
| 60 | new StringSelectMenuBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 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 | ]), |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 72 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 73 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 74 | .setLabel("Accept Suggestion") |
| 75 | .setCustomId("accept") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 76 | .setStyle(ButtonStyle.Success) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 77 | .setDisabled(predicted[channel].length === 0) |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 78 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id")) as APIMessageComponentEmoji), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 79 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 80 | .setLabel('Use "Other"') |
| 81 | .setCustomId("reject") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 82 | .setStyle(ButtonStyle.Secondary) |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 83 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id")) as APIMessageComponentEmoji) |
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 { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 89 | i = await m.awaitMessageComponent({ |
| 90 | time: 300000, |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 91 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id} |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 92 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 93 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 94 | return await interaction.editReply({ |
| 95 | embeds: [ |
| 96 | new EmojiEmbed() |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 97 | .setTitle("Categorize") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 98 | .setEmoji("CHANNEL.CATEGORY.DELETE") |
| 99 | .setStatus("Danger") |
| 100 | .setDescription( |
| 101 | `Select all types that apply to <#${channel}>.\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 102 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join( |
| 103 | ", " |
| 104 | )}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | ) |
| 106 | .setFooter({ text: "Message timed out" }) |
| 107 | ] |
| 108 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 109 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 110 | await i.deferUpdate(); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 111 | let selected; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | if (i.customId === "select") { |
| 113 | selected = i.values; |
| 114 | } |
| 115 | if (i.customId === "accept") { |
| 116 | selected = predicted[channel]; |
| 117 | } |
| 118 | if (i.customId === "reject") { |
| 119 | selected = ["other"]; |
| 120 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 121 | categorized[channel] = selected; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 122 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame^] | 123 | console.log(categorized); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 125 | |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 126 | const check = () => { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 127 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 128 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 129 | |
| 130 | export { command }; |
| 131 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 132 | export { check }; |