pineafan | e23c4ec | 2022-07-27 21:56:27 +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 | 4edb776 | 2022-06-26 19:21:04 +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") |
| 12 | .setDescription("Categorises your servers channels") |
| 13 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 14 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 15 | let channels = interaction.guild.channels.cache.filter(c => c.type !== "GUILD_CATEGORY"); |
| 16 | let categorised = {} |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 17 | await interaction.reply({embeds: LoadingEmbed, ephemeral: true}); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 18 | let predicted = {} |
| 19 | let types = { |
| 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"] |
| 29 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 30 | for (let c of channels.values()) { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 31 | for (let type in types) { |
| 32 | for (let word of types[type]) { |
| 33 | if (c.name.toLowerCase().includes(word)) { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +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; |
| 41 | for (let c of channels) { |
| 42 | // convert channel to a channel if its a string |
| 43 | let channel: any |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 44 | if (typeof c === "string") channel = interaction.guild.channels.cache.get(channel).id |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 45 | else channel = (c[0] as unknown as GuildChannel).id |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 46 | console.log(channel) |
| 47 | if (!predicted[channel]) predicted[channel] = [] |
| 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 | ]) |
| 68 | ]}) |
| 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"}) |
| 80 | ]}) |
| 81 | } |
| 82 | i.deferUpdate() |
| 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"]; } |
| 87 | categorised[channel] = selected |
| 88 | } |
| 89 | console.log(categorised) |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | export { command }; |
| 97 | export { callback }; |
| 98 | export { check }; |