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