blob: b7eb74551e657280e6642def0cd13fa2ae63b787 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { callback as roleMenu } from "../actions/roleMenu.js";
pineafan73a7c4a2022-07-24 10:38:04 +01002import verify from "../reflex/verify.js";
3import create from "../actions/tickets/create.js";
4import close from "../actions/tickets/delete.js";
5import createTranscript from "../premium/createTranscript.js";
PineaFana34d04b2023-01-03 22:05:42 +00006
TheCodedProfca29ebb2023-03-10 17:40:09 -05007import {
8 ActionRowBuilder,
9 ButtonBuilder,
10 ButtonInteraction,
11 ButtonStyle,
12 Interaction,
13 InteractionEditReplyOptions,
14 ModalBuilder,
15 ModalSubmitInteraction,
16 TextInputBuilder,
17 TextInputStyle
18} from "discord.js";
PineaFan752af462022-12-31 21:59:38 +000019import type { NucleusClient } from "../utils/client.js";
PineaFan1dee28f2023-01-16 22:09:07 +000020import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafanad54d752022-04-18 19:01:43 +010021
pineafan6de4da52023-03-07 20:43:44 +000022import { callback as banCallback, check as banCheck } from "../commands/mod/ban.js";
23import { callback as kickCallback, check as kickCheck } from "../commands/mod/kick.js";
24import { callback as muteCallback, check as muteCheck } from "../commands/mod/mute.js";
25import { callback as nicknameCallback, check as nicknameCheck } from "../commands/mod/nick.js";
26import { callback as warnCallback, check as warnCheck } from "../commands/mod/warn.js";
TheCodedProf4a7c25d2023-06-07 17:09:45 -040027import { callback as logDetailsCallback } from "../actions/logs/showDetails.js";
TheCodedProf35e73712023-03-10 17:35:35 -050028import client from "../utils/client.js";
pineafan6de4da52023-03-07 20:43:44 +000029
pineafan63fc5e22022-08-04 22:04:10 +010030export const event = "interactionCreate";
pineafanad54d752022-04-18 19:01:43 +010031
pineafan6de4da52023-03-07 20:43:44 +000032async function errorMessage(interaction: ButtonInteraction, message: string) {
33 await interaction.reply({
pineafan1e462ab2023-03-07 21:34:06 +000034 embeds: [new EmojiEmbed().setDescription(message).setStatus("Danger")],
pineafan6de4da52023-03-07 20:43:44 +000035 ephemeral: true,
36 components: []
37 });
38}
39
pineafan0f5cc782022-08-12 21:55:42 +010040async function interactionCreate(interaction: Interaction) {
PineaFana34d04b2023-01-03 22:05:42 +000041 if (interaction.isButton()) {
TheCodedProf35e73712023-03-10 17:35:35 -050042 if (interaction.customId.endsWith(":Suggestion")) {
TheCodedProfca29ebb2023-03-10 17:40:09 -050043 const value =
44 interaction.customId.startsWith("accept") || interaction.customId.startsWith("implement")
45 ? true
46 : false;
TheCodedProf35e73712023-03-10 17:35:35 -050047 return await modifySuggestion(interaction, value);
48 }
pineafan67c9f1f2023-06-23 22:50:26 +010049 if (interaction.customId === "log:edit") {
50 const attachment = interaction.message.embeds[0]?.image;
51 console.log(attachment)
TheCodedProfc3195b52023-06-23 15:53:00 -040052 if (!attachment) return;
pineafan67c9f1f2023-06-23 22:50:26 +010053 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)
TheCodedProfc3195b52023-06-23 15:53:00 -040059 }
PineaFan538d3752023-01-12 21:48:23 +000060 switch (interaction.customId) {
Skyler Greyda16adf2023-03-05 10:22:12 +000061 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 }
TheCodedProf4a7c25d2023-06-07 17:09:45 -040076 case "log:showDetails": {
77 return await logDetailsCallback(interaction);
78 }
pineafan02ba0232022-07-24 22:16:15 +010079 }
pineafan6de4da52023-03-07 20:43:44 +000080 // 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": {
TheCodedProf35e73712023-03-10 17:35:35 -050087 const check = kickCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000088 if (check !== true) return await errorMessage(interaction, check!);
89 return await kickCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000090 }
91 case "ban": {
TheCodedProf35e73712023-03-10 17:35:35 -050092 const check = banCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000093 if (check !== true) return await errorMessage(interaction, check!);
94 return await banCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000095 }
96 case "mute": {
TheCodedProf35e73712023-03-10 17:35:35 -050097 const check = muteCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000098 if (check !== true) return await errorMessage(interaction, check!);
99 return await muteCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +0000100 }
101 case "nickname": {
TheCodedProf35e73712023-03-10 17:35:35 -0500102 const check = nicknameCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +0000103 if (check !== true) return await errorMessage(interaction, check || "Something went wrong");
104 return await nicknameCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +0000105 }
106 case "warn": {
TheCodedProf35e73712023-03-10 17:35:35 -0500107 const check = warnCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +0000108 if (check !== true) return await errorMessage(interaction, check!);
109 return await warnCallback(interaction, member);
110 }
111 }
112 }
pineafanad54d752022-04-18 19:01:43 +0100113 }
114}
115
TheCodedProf35e73712023-03-10 17:35:35 -0500116const getReason = async (buttonInteraction: ButtonInteraction, prompt: string) => {
117 const modal = new ModalBuilder()
118 .addComponents(
119 new ActionRowBuilder<TextInputBuilder>().addComponents(
TheCodedProfca29ebb2023-03-10 17:40:09 -0500120 new TextInputBuilder().setStyle(TextInputStyle.Paragraph).setLabel(prompt).setCustomId("typed")
TheCodedProf35e73712023-03-10 17:35:35 -0500121 )
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");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500137};
TheCodedProf35e73712023-03-10 17:35:35 -0500138
139async function modifySuggestion(interaction: ButtonInteraction, accept: boolean) {
140 const message = interaction.message;
PineaFan1dee28f2023-01-16 22:09:07 +0000141 await message.fetch();
142 if (message.embeds.length === 0) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500143 const embed = message.embeds[0]!;
TheCodedProfca29ebb2023-03-10 17:40:09 -0500144 const issueNum = embed.footer!.text;
145 if (!issueNum) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500146 const issue = {
147 owner: "ClicksMinutePer",
148 repo: "Nucleus",
149 issue_number: parseInt(issueNum)
TheCodedProfca29ebb2023-03-10 17:40:09 -0500150 };
TheCodedProf35e73712023-03-10 17:35:35 -0500151 let name = "Unknown";
152 const components: InteractionEditReplyOptions["components"] = [];
TheCodedProfca29ebb2023-03-10 17:40:09 -0500153 switch (interaction.customId) {
TheCodedProf35e73712023-03-10 17:35:35 -0500154 case "accept:Suggestion": {
155 name = "Accepted";
156 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500157 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 );
TheCodedProf35e73712023-03-10 17:35:35 -0500173 break;
174 }
175 case "deny:Suggestion": {
176 name = "Denied";
177 const reason = await getReason(interaction, "Reason for denial");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500178 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" });
TheCodedProf35e73712023-03-10 17:35:35 -0500183 // await client.GitHub.rest.issues.lock({...issue, lock_reason: "resolved"})
TheCodedProfca29ebb2023-03-10 17:40:09 -0500184 components.push(
185 new ActionRowBuilder<ButtonBuilder>().addComponents(
186 new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger)
187 )
188 );
TheCodedProf35e73712023-03-10 17:35:35 -0500189 break;
190 }
191 case "close:Suggestion": {
192 name = "Closed";
193 const reason = await getReason(interaction, "Reason for closing");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500194 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" });
TheCodedProf35e73712023-03-10 17:35:35 -0500199 // await client.GitHub.rest.issues.lock({...issue})
TheCodedProfca29ebb2023-03-10 17:40:09 -0500200 components.push(
201 new ActionRowBuilder<ButtonBuilder>().addComponents(
202 new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger)
203 )
204 );
TheCodedProf35e73712023-03-10 17:35:35 -0500205 break;
206 }
207 case "implement:Suggestion": {
208 name = "Implemented";
209 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500210 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" });
TheCodedProf35e73712023-03-10 17:35:35 -0500213 break;
214 }
215 case "lock:Suggestion": {
216 name = "Locked";
217 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500218 await client.GitHub.rest.issues.lock({ ...issue });
TheCodedProf35e73712023-03-10 17:35:35 -0500219 break;
220 }
221 case "spam:Suggestion": {
222 name = "Marked as Spam";
223 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500224 await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" });
225 await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "spam" });
TheCodedProf35e73712023-03-10 17:35:35 -0500226 break;
227 }
228 }
229
TheCodedProf46518a42023-02-18 17:08:23 -0500230 const newcolor = accept ? "Success" : "Danger";
TheCodedProf35e73712023-03-10 17:35:35 -0500231 const newEmoji = accept ? "ICONS.ADD" : "ICONS.OPP.ADD";
PineaFan1dee28f2023-01-16 22:09:07 +0000232
233 const newEmbed = new EmojiEmbed()
TheCodedProf35e73712023-03-10 17:35:35 -0500234 .setEmoji(newEmoji)
235 .setTitle(embed!.title!.replace(/.+> /, ""))
PineaFan1dee28f2023-01-16 22:09:07 +0000236 .setDescription(embed!.description!)
TheCodedProf35e73712023-03-10 17:35:35 -0500237 .setFields({
238 name: name + " by",
TheCodedProfca29ebb2023-03-10 17:40:09 -0500239 value: interaction.user.tag
TheCodedProf35e73712023-03-10 17:35:35 -0500240 })
241 .setStatus(newcolor)
242 .setFooter(embed!.footer);
PineaFan1dee28f2023-01-16 22:09:07 +0000243
TheCodedProf35e73712023-03-10 17:35:35 -0500244 await interaction.editReply({
245 embeds: [newEmbed],
246 components: components
247 });
PineaFan1dee28f2023-01-16 22:09:07 +0000248}
249
PineaFan752af462022-12-31 21:59:38 +0000250export async function callback(_client: NucleusClient, interaction: Interaction) {
pineafan63fc5e22022-08-04 22:04:10 +0100251 await interactionCreate(interaction);
Skyler Grey75ea9172022-08-06 10:22:23 +0100252}