pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { callback as roleMenu } from "../actions/roleMenu.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 2 | import verify from "../reflex/verify.js"; |
| 3 | import create from "../actions/tickets/create.js"; |
| 4 | import close from "../actions/tickets/delete.js"; |
| 5 | import createTranscript from "../premium/createTranscript.js"; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 6 | |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 7 | import { |
| 8 | ActionRowBuilder, |
| 9 | ButtonBuilder, |
| 10 | ButtonInteraction, |
| 11 | ButtonStyle, |
| 12 | Interaction, |
| 13 | InteractionEditReplyOptions, |
| 14 | ModalBuilder, |
| 15 | ModalSubmitInteraction, |
| 16 | TextInputBuilder, |
| 17 | TextInputStyle |
| 18 | } from "discord.js"; |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 19 | import type { NucleusClient } from "../utils/client.js"; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 20 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 21 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 22 | import { callback as banCallback, check as banCheck } from "../commands/mod/ban.js"; |
| 23 | import { callback as kickCallback, check as kickCheck } from "../commands/mod/kick.js"; |
| 24 | import { callback as muteCallback, check as muteCheck } from "../commands/mod/mute.js"; |
| 25 | import { callback as nicknameCallback, check as nicknameCheck } from "../commands/mod/nick.js"; |
| 26 | import { callback as warnCallback, check as warnCheck } from "../commands/mod/warn.js"; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 27 | import client from "../utils/client.js"; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 28 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | export const event = "interactionCreate"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 30 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 31 | async function errorMessage(interaction: ButtonInteraction, message: string) { |
| 32 | await interaction.reply({ |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 33 | embeds: [new EmojiEmbed().setDescription(message).setStatus("Danger")], |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 34 | ephemeral: true, |
| 35 | components: [] |
| 36 | }); |
| 37 | } |
| 38 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 39 | async function interactionCreate(interaction: Interaction) { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 40 | if (interaction.isButton()) { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 41 | if (interaction.customId.endsWith(":Suggestion")) { |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 42 | const value = |
| 43 | interaction.customId.startsWith("accept") || interaction.customId.startsWith("implement") |
| 44 | ? true |
| 45 | : false; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 46 | return await modifySuggestion(interaction, value); |
| 47 | } |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 48 | switch (interaction.customId) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 49 | case "rolemenu": { |
| 50 | return await roleMenu(interaction); |
| 51 | } |
| 52 | case "verifybutton": { |
| 53 | return await verify(interaction); |
| 54 | } |
| 55 | case "createticket": { |
| 56 | return await create(interaction); |
| 57 | } |
| 58 | case "closeticket": { |
| 59 | return await close(interaction); |
| 60 | } |
| 61 | case "createtranscript": { |
| 62 | return await createTranscript(interaction); |
| 63 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 64 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 65 | // Mod actions |
| 66 | if (interaction.customId.startsWith("mod:")) { |
| 67 | const action = interaction.customId.split(":")[1]; |
| 68 | const memberId = interaction.customId.split(":")[2]; |
| 69 | const member = await interaction.guild?.members.fetch(memberId!); |
| 70 | switch (action) { |
| 71 | case "kick": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 72 | const check = kickCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 73 | if (check !== true) return await errorMessage(interaction, check!); |
| 74 | return await kickCallback(interaction, member); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 75 | } |
| 76 | case "ban": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 77 | const check = banCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 78 | if (check !== true) return await errorMessage(interaction, check!); |
| 79 | return await banCallback(interaction, member); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 80 | } |
| 81 | case "mute": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 82 | const check = muteCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 83 | if (check !== true) return await errorMessage(interaction, check!); |
| 84 | return await muteCallback(interaction, member); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 85 | } |
| 86 | case "nickname": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 87 | const check = nicknameCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 88 | if (check !== true) return await errorMessage(interaction, check || "Something went wrong"); |
| 89 | return await nicknameCallback(interaction, member); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 90 | } |
| 91 | case "warn": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 92 | const check = warnCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 93 | if (check !== true) return await errorMessage(interaction, check!); |
| 94 | return await warnCallback(interaction, member); |
| 95 | } |
| 96 | } |
| 97 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 101 | const getReason = async (buttonInteraction: ButtonInteraction, prompt: string) => { |
| 102 | const modal = new ModalBuilder() |
| 103 | .addComponents( |
| 104 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 105 | new TextInputBuilder().setStyle(TextInputStyle.Paragraph).setLabel(prompt).setCustomId("typed") |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 106 | ) |
| 107 | ) |
| 108 | .setTitle("Reason") |
| 109 | .setCustomId("modal"); |
| 110 | await buttonInteraction.showModal(modal); |
| 111 | let out: ModalSubmitInteraction; |
| 112 | try { |
| 113 | out = await buttonInteraction.awaitModalSubmit({ |
| 114 | filter: (i) => i.customId === "modal" && i.user.id === buttonInteraction.user.id, |
| 115 | time: 300000 |
| 116 | }); |
| 117 | } catch { |
| 118 | return null; |
| 119 | } |
| 120 | await out.deferUpdate(); |
| 121 | return out.fields.getTextInputValue("typed"); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 122 | }; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 123 | |
| 124 | async function modifySuggestion(interaction: ButtonInteraction, accept: boolean) { |
| 125 | const message = interaction.message; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 126 | await message.fetch(); |
| 127 | if (message.embeds.length === 0) return; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 128 | const embed = message.embeds[0]!; |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 129 | const issueNum = embed.footer!.text; |
| 130 | if (!issueNum) return; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 131 | const issue = { |
| 132 | owner: "ClicksMinutePer", |
| 133 | repo: "Nucleus", |
| 134 | issue_number: parseInt(issueNum) |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 135 | }; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 136 | let name = "Unknown"; |
| 137 | const components: InteractionEditReplyOptions["components"] = []; |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 138 | switch (interaction.customId) { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 139 | case "accept:Suggestion": { |
| 140 | name = "Accepted"; |
| 141 | await interaction.deferUpdate(); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 142 | await client.GitHub.rest.issues.createComment({ |
| 143 | ...issue, |
| 144 | body: "Suggestion accepted by " + interaction.user.tag |
| 145 | }); |
| 146 | components.push( |
| 147 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 148 | new ButtonBuilder() |
| 149 | .setCustomId("close:Suggestion") |
| 150 | .setLabel("Close") |
| 151 | .setStyle(ButtonStyle.Secondary), |
| 152 | new ButtonBuilder() |
| 153 | .setCustomId("implemented:Suggestion") |
| 154 | .setLabel("Implemented") |
| 155 | .setStyle(ButtonStyle.Secondary) |
| 156 | ) |
| 157 | ); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | case "deny:Suggestion": { |
| 161 | name = "Denied"; |
| 162 | const reason = await getReason(interaction, "Reason for denial"); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 163 | await client.GitHub.rest.issues.createComment({ |
| 164 | ...issue, |
| 165 | body: "Suggestion denied by " + interaction.user.tag + " for reason:\n>" + reason |
| 166 | }); |
| 167 | await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 168 | // await client.GitHub.rest.issues.lock({...issue, lock_reason: "resolved"}) |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 169 | components.push( |
| 170 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 171 | new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger) |
| 172 | ) |
| 173 | ); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | case "close:Suggestion": { |
| 177 | name = "Closed"; |
| 178 | const reason = await getReason(interaction, "Reason for closing"); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 179 | await client.GitHub.rest.issues.createComment({ |
| 180 | ...issue, |
| 181 | body: "Suggestion closed by " + interaction.user.tag + " for reason:\n>" + reason |
| 182 | }); |
| 183 | await client.GitHub.rest.issues.update({ ...issue, state: "closed" }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 184 | // await client.GitHub.rest.issues.lock({...issue}) |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 185 | components.push( |
| 186 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 187 | new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger) |
| 188 | ) |
| 189 | ); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | case "implement:Suggestion": { |
| 193 | name = "Implemented"; |
| 194 | await interaction.deferUpdate(); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 195 | await client.GitHub.rest.issues.createComment({ ...issue, body: "Suggestion implemented" }); |
| 196 | await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "completed" }); |
| 197 | await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "resolved" }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 198 | break; |
| 199 | } |
| 200 | case "lock:Suggestion": { |
| 201 | name = "Locked"; |
| 202 | await interaction.deferUpdate(); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 203 | await client.GitHub.rest.issues.lock({ ...issue }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | case "spam:Suggestion": { |
| 207 | name = "Marked as Spam"; |
| 208 | await interaction.deferUpdate(); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 209 | await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" }); |
| 210 | await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "spam" }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 215 | const newcolor = accept ? "Success" : "Danger"; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 216 | const newEmoji = accept ? "ICONS.ADD" : "ICONS.OPP.ADD"; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 217 | |
| 218 | const newEmbed = new EmojiEmbed() |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 219 | .setEmoji(newEmoji) |
| 220 | .setTitle(embed!.title!.replace(/.+> /, "")) |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 221 | .setDescription(embed!.description!) |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 222 | .setFields({ |
| 223 | name: name + " by", |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 224 | value: interaction.user.tag |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 225 | }) |
| 226 | .setStatus(newcolor) |
| 227 | .setFooter(embed!.footer); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 228 | |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 229 | await interaction.editReply({ |
| 230 | embeds: [newEmbed], |
| 231 | components: components |
| 232 | }); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 233 | } |
| 234 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 235 | export async function callback(_client: NucleusClient, interaction: Interaction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 236 | await interactionCreate(interaction); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 237 | } |