blob: 3ead1c5f424caae7e01ed9048357f94fb9c1164c [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 }
pineafan435a8782023-06-24 12:45:58 +010049 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();
TheCodedProfc3195b52023-06-23 15:53:00 -040052 if (!attachment) return;
pineafan435a8782023-06-24 12:45:58 +010053 const attachmentData = await (await fetch(attachment.url)).text();
pineafan67c9f1f2023-06-23 22:50:26 +010054 const decoded = atob(attachmentData);
pineafan435a8782023-06-24 12:45:58 +010055 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 });
TheCodedProfc3195b52023-06-23 15:53:00 -0400103 }
PineaFan538d3752023-01-12 21:48:23 +0000104 switch (interaction.customId) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000105 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 }
TheCodedProf4a7c25d2023-06-07 17:09:45 -0400120 case "log:showDetails": {
121 return await logDetailsCallback(interaction);
122 }
pineafan02ba0232022-07-24 22:16:15 +0100123 }
pineafan6de4da52023-03-07 20:43:44 +0000124 // 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": {
TheCodedProf35e73712023-03-10 17:35:35 -0500131 const check = kickCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +0000132 if (check !== true) return await errorMessage(interaction, check!);
133 return await kickCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +0000134 }
135 case "ban": {
TheCodedProf35e73712023-03-10 17:35:35 -0500136 const check = banCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +0000137 if (check !== true) return await errorMessage(interaction, check!);
138 return await banCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +0000139 }
140 case "mute": {
TheCodedProf35e73712023-03-10 17:35:35 -0500141 const check = muteCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +0000142 if (check !== true) return await errorMessage(interaction, check!);
143 return await muteCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +0000144 }
145 case "nickname": {
TheCodedProf35e73712023-03-10 17:35:35 -0500146 const check = nicknameCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +0000147 if (check !== true) return await errorMessage(interaction, check || "Something went wrong");
148 return await nicknameCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +0000149 }
150 case "warn": {
TheCodedProf35e73712023-03-10 17:35:35 -0500151 const check = warnCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +0000152 if (check !== true) return await errorMessage(interaction, check!);
153 return await warnCallback(interaction, member);
154 }
155 }
156 }
pineafanad54d752022-04-18 19:01:43 +0100157 }
158}
159
TheCodedProf35e73712023-03-10 17:35:35 -0500160const getReason = async (buttonInteraction: ButtonInteraction, prompt: string) => {
161 const modal = new ModalBuilder()
162 .addComponents(
163 new ActionRowBuilder<TextInputBuilder>().addComponents(
TheCodedProfca29ebb2023-03-10 17:40:09 -0500164 new TextInputBuilder().setStyle(TextInputStyle.Paragraph).setLabel(prompt).setCustomId("typed")
TheCodedProf35e73712023-03-10 17:35:35 -0500165 )
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");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500181};
TheCodedProf35e73712023-03-10 17:35:35 -0500182
183async function modifySuggestion(interaction: ButtonInteraction, accept: boolean) {
184 const message = interaction.message;
PineaFan1dee28f2023-01-16 22:09:07 +0000185 await message.fetch();
186 if (message.embeds.length === 0) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500187 const embed = message.embeds[0]!;
TheCodedProfca29ebb2023-03-10 17:40:09 -0500188 const issueNum = embed.footer!.text;
189 if (!issueNum) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500190 const issue = {
191 owner: "ClicksMinutePer",
192 repo: "Nucleus",
193 issue_number: parseInt(issueNum)
TheCodedProfca29ebb2023-03-10 17:40:09 -0500194 };
TheCodedProf35e73712023-03-10 17:35:35 -0500195 let name = "Unknown";
196 const components: InteractionEditReplyOptions["components"] = [];
TheCodedProfca29ebb2023-03-10 17:40:09 -0500197 switch (interaction.customId) {
TheCodedProf35e73712023-03-10 17:35:35 -0500198 case "accept:Suggestion": {
199 name = "Accepted";
200 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500201 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 );
TheCodedProf35e73712023-03-10 17:35:35 -0500217 break;
218 }
219 case "deny:Suggestion": {
220 name = "Denied";
221 const reason = await getReason(interaction, "Reason for denial");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500222 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" });
TheCodedProf35e73712023-03-10 17:35:35 -0500227 // await client.GitHub.rest.issues.lock({...issue, lock_reason: "resolved"})
TheCodedProfca29ebb2023-03-10 17:40:09 -0500228 components.push(
229 new ActionRowBuilder<ButtonBuilder>().addComponents(
230 new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger)
231 )
232 );
TheCodedProf35e73712023-03-10 17:35:35 -0500233 break;
234 }
235 case "close:Suggestion": {
236 name = "Closed";
237 const reason = await getReason(interaction, "Reason for closing");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500238 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" });
TheCodedProf35e73712023-03-10 17:35:35 -0500243 // await client.GitHub.rest.issues.lock({...issue})
TheCodedProfca29ebb2023-03-10 17:40:09 -0500244 components.push(
245 new ActionRowBuilder<ButtonBuilder>().addComponents(
246 new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger)
247 )
248 );
TheCodedProf35e73712023-03-10 17:35:35 -0500249 break;
250 }
251 case "implement:Suggestion": {
252 name = "Implemented";
253 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500254 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" });
TheCodedProf35e73712023-03-10 17:35:35 -0500257 break;
258 }
259 case "lock:Suggestion": {
260 name = "Locked";
261 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500262 await client.GitHub.rest.issues.lock({ ...issue });
TheCodedProf35e73712023-03-10 17:35:35 -0500263 break;
264 }
265 case "spam:Suggestion": {
266 name = "Marked as Spam";
267 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500268 await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" });
269 await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "spam" });
TheCodedProf35e73712023-03-10 17:35:35 -0500270 break;
271 }
272 }
273
TheCodedProf46518a42023-02-18 17:08:23 -0500274 const newcolor = accept ? "Success" : "Danger";
TheCodedProf35e73712023-03-10 17:35:35 -0500275 const newEmoji = accept ? "ICONS.ADD" : "ICONS.OPP.ADD";
PineaFan1dee28f2023-01-16 22:09:07 +0000276
277 const newEmbed = new EmojiEmbed()
TheCodedProf35e73712023-03-10 17:35:35 -0500278 .setEmoji(newEmoji)
279 .setTitle(embed!.title!.replace(/.+> /, ""))
PineaFan1dee28f2023-01-16 22:09:07 +0000280 .setDescription(embed!.description!)
TheCodedProf35e73712023-03-10 17:35:35 -0500281 .setFields({
282 name: name + " by",
TheCodedProfca29ebb2023-03-10 17:40:09 -0500283 value: interaction.user.tag
TheCodedProf35e73712023-03-10 17:35:35 -0500284 })
285 .setStatus(newcolor)
286 .setFooter(embed!.footer);
PineaFan1dee28f2023-01-16 22:09:07 +0000287
TheCodedProf35e73712023-03-10 17:35:35 -0500288 await interaction.editReply({
289 embeds: [newEmbed],
290 components: components
291 });
PineaFan1dee28f2023-01-16 22:09:07 +0000292}
293
PineaFan752af462022-12-31 21:59:38 +0000294export async function callback(_client: NucleusClient, interaction: Interaction) {
pineafan63fc5e22022-08-04 22:04:10 +0100295 await interactionCreate(interaction);
Skyler Grey75ea9172022-08-06 10:22:23 +0100296}