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 | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 27 | import { callback as logDetailsCallback } from "../actions/logs/showDetails.js"; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 28 | import client from "../utils/client.js"; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 29 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | export const event = "interactionCreate"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 31 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 32 | async function errorMessage(interaction: ButtonInteraction, message: string) { |
| 33 | await interaction.reply({ |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 34 | embeds: [new EmojiEmbed().setDescription(message).setStatus("Danger")], |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 35 | ephemeral: true, |
| 36 | components: [] |
| 37 | }); |
| 38 | } |
| 39 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 40 | async function interactionCreate(interaction: Interaction) { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 41 | if (interaction.isButton()) { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 42 | if (interaction.customId.endsWith(":Suggestion")) { |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 43 | const value = |
| 44 | interaction.customId.startsWith("accept") || interaction.customId.startsWith("implement") |
| 45 | ? true |
| 46 | : false; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 47 | return await modifySuggestion(interaction, value); |
| 48 | } |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame] | 49 | if (interaction.customId === "log:message.edit") { |
| 50 | await interaction.channel?.messages.fetch({ message: interaction.message.id, force: true }); |
| 51 | const attachment = interaction.message.embeds[0]?.image ?? interaction.message.attachments.first(); |
TheCodedProf | c3195b5 | 2023-06-23 15:53:00 -0400 | [diff] [blame] | 52 | if (!attachment) return; |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame] | 53 | const attachmentData = await (await fetch(attachment.url)).text(); |
pineafan | 67c9f1f | 2023-06-23 22:50:26 +0100 | [diff] [blame] | 54 | const decoded = atob(attachmentData); |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame] | 55 | const json = ( |
| 56 | JSON.parse(decoded) as { data: { count: number; value: string; added?: boolean; removed?: boolean }[] } |
| 57 | ).data; |
| 58 | // "Before" is everything where added is false |
| 59 | // "After" is everything where removed is false |
| 60 | const before: string = json |
| 61 | .filter((d) => !d.added) |
| 62 | .map((d) => d.value) |
| 63 | .join(""); |
| 64 | const after: string = json |
| 65 | .filter((d) => !d.removed) |
| 66 | .map((d) => d.value) |
| 67 | .join(""); |
| 68 | const { renderDateFooter } = client.logger; |
| 69 | await interaction.reply({ |
| 70 | embeds: [ |
| 71 | new EmojiEmbed() |
| 72 | .setTitle("Before") |
| 73 | .setDescription(before) |
| 74 | .setStatus("Danger") |
| 75 | .setEmoji("ICONS.OPP.ADD"), |
| 76 | new EmojiEmbed() |
| 77 | .setTitle("After") |
| 78 | .setDescription(after) |
| 79 | .setStatus("Success") |
| 80 | .setEmoji("ICONS.ADD") |
| 81 | .setFooter({ text: `Edited at ${renderDateFooter(interaction.message.createdTimestamp!)}` }) // Created timestamp of the log is when the edit was made |
| 82 | ], |
| 83 | ephemeral: true |
| 84 | }); |
| 85 | } else if (interaction.customId === "log:message.delete") { |
| 86 | await interaction.channel?.messages.fetch({ message: interaction.message.id, force: true }); |
| 87 | const attachment = interaction.message.embeds[0]?.image ?? interaction.message.attachments.first(); |
| 88 | if (!attachment) return; |
| 89 | const attachmentData = await (await fetch(attachment.url)).text(); |
| 90 | const decoded = atob(attachmentData); |
| 91 | const json = JSON.parse(decoded) as { data: string }; |
| 92 | await interaction.reply({ |
| 93 | embeds: [ |
| 94 | new EmojiEmbed() |
| 95 | .setTitle("Message") |
| 96 | .setDescription(json.data) |
| 97 | .setStatus("Danger") |
| 98 | .setEmoji("MESSAGE.DELETE") |
| 99 | .setFooter({ text: `Deleted at ${client.logger.renderDateFooter(Date.now())}` }) |
| 100 | ], |
| 101 | ephemeral: true |
| 102 | }); |
TheCodedProf | c3195b5 | 2023-06-23 15:53:00 -0400 | [diff] [blame] | 103 | } |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 104 | switch (interaction.customId) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 105 | case "rolemenu": { |
| 106 | return await roleMenu(interaction); |
| 107 | } |
| 108 | case "verifybutton": { |
| 109 | return await verify(interaction); |
| 110 | } |
| 111 | case "createticket": { |
| 112 | return await create(interaction); |
| 113 | } |
| 114 | case "closeticket": { |
| 115 | return await close(interaction); |
| 116 | } |
| 117 | case "createtranscript": { |
| 118 | return await createTranscript(interaction); |
| 119 | } |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 120 | case "log:showDetails": { |
| 121 | return await logDetailsCallback(interaction); |
| 122 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 123 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 124 | // Mod actions |
| 125 | if (interaction.customId.startsWith("mod:")) { |
| 126 | const action = interaction.customId.split(":")[1]; |
| 127 | const memberId = interaction.customId.split(":")[2]; |
| 128 | const member = await interaction.guild?.members.fetch(memberId!); |
| 129 | switch (action) { |
| 130 | case "kick": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 131 | const check = kickCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 132 | if (check !== true) return await errorMessage(interaction, check!); |
| 133 | return await kickCallback(interaction, member); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 134 | } |
| 135 | case "ban": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 136 | const check = banCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 137 | if (check !== true) return await errorMessage(interaction, check!); |
| 138 | return await banCallback(interaction, member); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 139 | } |
| 140 | case "mute": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 141 | const check = muteCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 142 | if (check !== true) return await errorMessage(interaction, check!); |
| 143 | return await muteCallback(interaction, member); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 144 | } |
| 145 | case "nickname": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 146 | const check = nicknameCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 147 | if (check !== true) return await errorMessage(interaction, check || "Something went wrong"); |
| 148 | return await nicknameCallback(interaction, member); |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 149 | } |
| 150 | case "warn": { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 151 | const check = warnCheck(interaction, false, member); |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 152 | if (check !== true) return await errorMessage(interaction, check!); |
| 153 | return await warnCallback(interaction, member); |
| 154 | } |
| 155 | } |
| 156 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 160 | const getReason = async (buttonInteraction: ButtonInteraction, prompt: string) => { |
| 161 | const modal = new ModalBuilder() |
| 162 | .addComponents( |
| 163 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 164 | new TextInputBuilder().setStyle(TextInputStyle.Paragraph).setLabel(prompt).setCustomId("typed") |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 165 | ) |
| 166 | ) |
| 167 | .setTitle("Reason") |
| 168 | .setCustomId("modal"); |
| 169 | await buttonInteraction.showModal(modal); |
| 170 | let out: ModalSubmitInteraction; |
| 171 | try { |
| 172 | out = await buttonInteraction.awaitModalSubmit({ |
| 173 | filter: (i) => i.customId === "modal" && i.user.id === buttonInteraction.user.id, |
| 174 | time: 300000 |
| 175 | }); |
| 176 | } catch { |
| 177 | return null; |
| 178 | } |
| 179 | await out.deferUpdate(); |
| 180 | return out.fields.getTextInputValue("typed"); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 181 | }; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 182 | |
| 183 | async function modifySuggestion(interaction: ButtonInteraction, accept: boolean) { |
| 184 | const message = interaction.message; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 185 | await message.fetch(); |
| 186 | if (message.embeds.length === 0) return; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 187 | const embed = message.embeds[0]!; |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 188 | const issueNum = embed.footer!.text; |
| 189 | if (!issueNum) return; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 190 | const issue = { |
| 191 | owner: "ClicksMinutePer", |
| 192 | repo: "Nucleus", |
| 193 | issue_number: parseInt(issueNum) |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 194 | }; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 195 | let name = "Unknown"; |
| 196 | const components: InteractionEditReplyOptions["components"] = []; |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 197 | switch (interaction.customId) { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 198 | case "accept:Suggestion": { |
| 199 | name = "Accepted"; |
| 200 | await interaction.deferUpdate(); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 201 | await client.GitHub.rest.issues.createComment({ |
| 202 | ...issue, |
| 203 | body: "Suggestion accepted by " + interaction.user.tag |
| 204 | }); |
| 205 | components.push( |
| 206 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 207 | new ButtonBuilder() |
| 208 | .setCustomId("close:Suggestion") |
| 209 | .setLabel("Close") |
| 210 | .setStyle(ButtonStyle.Secondary), |
| 211 | new ButtonBuilder() |
| 212 | .setCustomId("implemented:Suggestion") |
| 213 | .setLabel("Implemented") |
| 214 | .setStyle(ButtonStyle.Secondary) |
| 215 | ) |
| 216 | ); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 217 | break; |
| 218 | } |
| 219 | case "deny:Suggestion": { |
| 220 | name = "Denied"; |
| 221 | const reason = await getReason(interaction, "Reason for denial"); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 222 | await client.GitHub.rest.issues.createComment({ |
| 223 | ...issue, |
| 224 | body: "Suggestion denied by " + interaction.user.tag + " for reason:\n>" + reason |
| 225 | }); |
| 226 | await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 227 | // await client.GitHub.rest.issues.lock({...issue, lock_reason: "resolved"}) |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 228 | components.push( |
| 229 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 230 | new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger) |
| 231 | ) |
| 232 | ); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | case "close:Suggestion": { |
| 236 | name = "Closed"; |
| 237 | const reason = await getReason(interaction, "Reason for closing"); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 238 | await client.GitHub.rest.issues.createComment({ |
| 239 | ...issue, |
| 240 | body: "Suggestion closed by " + interaction.user.tag + " for reason:\n>" + reason |
| 241 | }); |
| 242 | await client.GitHub.rest.issues.update({ ...issue, state: "closed" }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 243 | // await client.GitHub.rest.issues.lock({...issue}) |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 244 | components.push( |
| 245 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 246 | new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger) |
| 247 | ) |
| 248 | ); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | case "implement:Suggestion": { |
| 252 | name = "Implemented"; |
| 253 | await interaction.deferUpdate(); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 254 | await client.GitHub.rest.issues.createComment({ ...issue, body: "Suggestion implemented" }); |
| 255 | await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "completed" }); |
| 256 | await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "resolved" }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | case "lock:Suggestion": { |
| 260 | name = "Locked"; |
| 261 | await interaction.deferUpdate(); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 262 | await client.GitHub.rest.issues.lock({ ...issue }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | case "spam:Suggestion": { |
| 266 | name = "Marked as Spam"; |
| 267 | await interaction.deferUpdate(); |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 268 | await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" }); |
| 269 | await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "spam" }); |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | } |
| 273 | |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 274 | const newcolor = accept ? "Success" : "Danger"; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 275 | const newEmoji = accept ? "ICONS.ADD" : "ICONS.OPP.ADD"; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 276 | |
| 277 | const newEmbed = new EmojiEmbed() |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 278 | .setEmoji(newEmoji) |
| 279 | .setTitle(embed!.title!.replace(/.+> /, "")) |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 280 | .setDescription(embed!.description!) |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 281 | .setFields({ |
| 282 | name: name + " by", |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 283 | value: interaction.user.tag |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 284 | }) |
| 285 | .setStatus(newcolor) |
| 286 | .setFooter(embed!.footer); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 287 | |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 288 | await interaction.editReply({ |
| 289 | embeds: [newEmbed], |
| 290 | components: components |
| 291 | }); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 292 | } |
| 293 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 294 | export async function callback(_client: NucleusClient, interaction: Interaction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 295 | await interactionCreate(interaction); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 296 | } |