pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "../utils/defaultEmbeds.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +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"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 4 | import { WrappedCheck } from "jshaiku"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 5 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 6 | import client from "../utils/client.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 7 | import addPlural from "../utils/plurals.js"; |
| 8 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 9 | |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 10 | const command = new SlashCommandBuilder() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 11 | .setName("categorise") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | .setDescription("Categorises your servers channels"); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 13 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 14 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | const channels = interaction.guild.channels.cache.filter(c => c.type !== "GUILD_CATEGORY"); |
| 16 | const categorised = {}; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 17 | await interaction.reply({embeds: LoadingEmbed, ephemeral: true}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 18 | const predicted = {}; |
| 19 | const types = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 20 | general: ["general", "muted", "main", "topic", "discuss"], |
| 21 | commands: ["bot", "command", "music"], |
| 22 | images: ["pic", "selfies", "image"], |
| 23 | nsfw: ["porn", "nsfw", "sex"], |
| 24 | links: ["links"], |
| 25 | advertising: ["ads", "advert", "server", "partner"], |
| 26 | staff: ["staff", "mod", "admin"], |
| 27 | spam: ["spam"], |
| 28 | other: ["random"] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | }; |
| 30 | for (const c of channels.values()) { |
| 31 | for (const type in types) { |
| 32 | for (const word of types[type]) { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 33 | if (c.name.toLowerCase().includes(word)) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | predicted[c.id] = predicted[c.id] ?? []; |
| 35 | predicted[c.id].push(type); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 39 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 40 | let m; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | for (const c of channels) { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 42 | // convert channel to a channel if its a string |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | let channel: any; |
| 44 | if (typeof c === "string") channel = interaction.guild.channels.cache.get(channel).id; |
| 45 | else channel = (c[0] as unknown as GuildChannel).id; |
| 46 | console.log(channel); |
| 47 | if (!predicted[channel]) predicted[channel] = []; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 48 | m = await interaction.editReply({embeds: [new EmojiEmbed() |
| 49 | .setTitle("Categorise") |
| 50 | .setDescription(`Select all types that apply to <#${channel}>.\n\n` + |
| 51 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`) |
| 52 | .setEmoji("CHANNEL.CATEGORY.CREATE") |
| 53 | .setStatus("Success") |
| 54 | ], components: [ |
| 55 | new MessageActionRow().addComponents([new MessageSelectMenu() |
| 56 | .setCustomId("selected") |
| 57 | .setMaxValues(Object.keys(types).length) |
| 58 | .setMinValues(1) |
| 59 | .setPlaceholder("Select all types that apply to this channel") |
| 60 | .setOptions(Object.keys(types).map(type => ({label: type, value: type}))) |
| 61 | ]), |
| 62 | new MessageActionRow().addComponents([ |
| 63 | new MessageButton().setLabel("Accept Suggestion").setCustomId("accept").setStyle("SUCCESS").setDisabled(predicted[channel].length === 0) |
| 64 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.TICK", "id"))), |
| 65 | new MessageButton().setLabel("Use \"Other\"").setCustomId("reject").setStyle("SECONDARY") |
| 66 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.CROSS", "id"))) |
| 67 | ]) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | ]}); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 69 | let i; |
| 70 | try { |
| 71 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 72 | } catch (e) { |
| 73 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 74 | .setTitle("Categorise") |
| 75 | .setEmoji("CHANNEL.CATEGORY.DELETE") |
| 76 | .setStatus("Danger") |
| 77 | .setDescription(`Select all types that apply to <#${channel}>.\n\n` + |
| 78 | `${addPlural(predicted[channel].length, "Suggestion")}: ${predicted[channel].join(", ")}`) |
| 79 | .setFooter({text: "Message timed out"}) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | ]}); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 81 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 82 | i.deferUpdate(); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 83 | let selected; |
| 84 | if (i.customId === "select") { selected = i.values; } |
| 85 | if (i.customId === "accept") { selected = predicted[channel]; } |
| 86 | if (i.customId === "reject") { selected = ["other"]; } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | categorised[channel] = selected; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 88 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | console.log(categorised); |
| 90 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 91 | |
| 92 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 93 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 95 | |
| 96 | export { command }; |
| 97 | export { callback }; |
| 98 | export { check }; |