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