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