blob: 6a794475dae3b6b9386c702754415de85dacfb72 [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 }
PineaFan538d3752023-01-12 21:48:23 +000049 switch (interaction.customId) {
Skyler Greyda16adf2023-03-05 10:22:12 +000050 case "rolemenu": {
51 return await roleMenu(interaction);
52 }
53 case "verifybutton": {
54 return await verify(interaction);
55 }
56 case "createticket": {
57 return await create(interaction);
58 }
59 case "closeticket": {
60 return await close(interaction);
61 }
62 case "createtranscript": {
63 return await createTranscript(interaction);
64 }
TheCodedProf4a7c25d2023-06-07 17:09:45 -040065 case "log:showDetails": {
66 return await logDetailsCallback(interaction);
67 }
pineafan02ba0232022-07-24 22:16:15 +010068 }
pineafan6de4da52023-03-07 20:43:44 +000069 // Mod actions
70 if (interaction.customId.startsWith("mod:")) {
71 const action = interaction.customId.split(":")[1];
72 const memberId = interaction.customId.split(":")[2];
73 const member = await interaction.guild?.members.fetch(memberId!);
74 switch (action) {
75 case "kick": {
TheCodedProf35e73712023-03-10 17:35:35 -050076 const check = kickCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000077 if (check !== true) return await errorMessage(interaction, check!);
78 return await kickCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000079 }
80 case "ban": {
TheCodedProf35e73712023-03-10 17:35:35 -050081 const check = banCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000082 if (check !== true) return await errorMessage(interaction, check!);
83 return await banCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000084 }
85 case "mute": {
TheCodedProf35e73712023-03-10 17:35:35 -050086 const check = muteCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000087 if (check !== true) return await errorMessage(interaction, check!);
88 return await muteCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000089 }
90 case "nickname": {
TheCodedProf35e73712023-03-10 17:35:35 -050091 const check = nicknameCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000092 if (check !== true) return await errorMessage(interaction, check || "Something went wrong");
93 return await nicknameCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000094 }
95 case "warn": {
TheCodedProf35e73712023-03-10 17:35:35 -050096 const check = warnCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000097 if (check !== true) return await errorMessage(interaction, check!);
98 return await warnCallback(interaction, member);
99 }
100 }
101 }
pineafanad54d752022-04-18 19:01:43 +0100102 }
103}
104
TheCodedProf35e73712023-03-10 17:35:35 -0500105const getReason = async (buttonInteraction: ButtonInteraction, prompt: string) => {
106 const modal = new ModalBuilder()
107 .addComponents(
108 new ActionRowBuilder<TextInputBuilder>().addComponents(
TheCodedProfca29ebb2023-03-10 17:40:09 -0500109 new TextInputBuilder().setStyle(TextInputStyle.Paragraph).setLabel(prompt).setCustomId("typed")
TheCodedProf35e73712023-03-10 17:35:35 -0500110 )
111 )
112 .setTitle("Reason")
113 .setCustomId("modal");
114 await buttonInteraction.showModal(modal);
115 let out: ModalSubmitInteraction;
116 try {
117 out = await buttonInteraction.awaitModalSubmit({
118 filter: (i) => i.customId === "modal" && i.user.id === buttonInteraction.user.id,
119 time: 300000
120 });
121 } catch {
122 return null;
123 }
124 await out.deferUpdate();
125 return out.fields.getTextInputValue("typed");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500126};
TheCodedProf35e73712023-03-10 17:35:35 -0500127
128async function modifySuggestion(interaction: ButtonInteraction, accept: boolean) {
129 const message = interaction.message;
PineaFan1dee28f2023-01-16 22:09:07 +0000130 await message.fetch();
131 if (message.embeds.length === 0) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500132 const embed = message.embeds[0]!;
TheCodedProfca29ebb2023-03-10 17:40:09 -0500133 const issueNum = embed.footer!.text;
134 if (!issueNum) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500135 const issue = {
136 owner: "ClicksMinutePer",
137 repo: "Nucleus",
138 issue_number: parseInt(issueNum)
TheCodedProfca29ebb2023-03-10 17:40:09 -0500139 };
TheCodedProf35e73712023-03-10 17:35:35 -0500140 let name = "Unknown";
141 const components: InteractionEditReplyOptions["components"] = [];
TheCodedProfca29ebb2023-03-10 17:40:09 -0500142 switch (interaction.customId) {
TheCodedProf35e73712023-03-10 17:35:35 -0500143 case "accept:Suggestion": {
144 name = "Accepted";
145 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500146 await client.GitHub.rest.issues.createComment({
147 ...issue,
148 body: "Suggestion accepted by " + interaction.user.tag
149 });
150 components.push(
151 new ActionRowBuilder<ButtonBuilder>().addComponents(
152 new ButtonBuilder()
153 .setCustomId("close:Suggestion")
154 .setLabel("Close")
155 .setStyle(ButtonStyle.Secondary),
156 new ButtonBuilder()
157 .setCustomId("implemented:Suggestion")
158 .setLabel("Implemented")
159 .setStyle(ButtonStyle.Secondary)
160 )
161 );
TheCodedProf35e73712023-03-10 17:35:35 -0500162 break;
163 }
164 case "deny:Suggestion": {
165 name = "Denied";
166 const reason = await getReason(interaction, "Reason for denial");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500167 await client.GitHub.rest.issues.createComment({
168 ...issue,
169 body: "Suggestion denied by " + interaction.user.tag + " for reason:\n>" + reason
170 });
171 await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" });
TheCodedProf35e73712023-03-10 17:35:35 -0500172 // await client.GitHub.rest.issues.lock({...issue, lock_reason: "resolved"})
TheCodedProfca29ebb2023-03-10 17:40:09 -0500173 components.push(
174 new ActionRowBuilder<ButtonBuilder>().addComponents(
175 new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger)
176 )
177 );
TheCodedProf35e73712023-03-10 17:35:35 -0500178 break;
179 }
180 case "close:Suggestion": {
181 name = "Closed";
182 const reason = await getReason(interaction, "Reason for closing");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500183 await client.GitHub.rest.issues.createComment({
184 ...issue,
185 body: "Suggestion closed by " + interaction.user.tag + " for reason:\n>" + reason
186 });
187 await client.GitHub.rest.issues.update({ ...issue, state: "closed" });
TheCodedProf35e73712023-03-10 17:35:35 -0500188 // await client.GitHub.rest.issues.lock({...issue})
TheCodedProfca29ebb2023-03-10 17:40:09 -0500189 components.push(
190 new ActionRowBuilder<ButtonBuilder>().addComponents(
191 new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger)
192 )
193 );
TheCodedProf35e73712023-03-10 17:35:35 -0500194 break;
195 }
196 case "implement:Suggestion": {
197 name = "Implemented";
198 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500199 await client.GitHub.rest.issues.createComment({ ...issue, body: "Suggestion implemented" });
200 await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "completed" });
201 await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "resolved" });
TheCodedProf35e73712023-03-10 17:35:35 -0500202 break;
203 }
204 case "lock:Suggestion": {
205 name = "Locked";
206 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500207 await client.GitHub.rest.issues.lock({ ...issue });
TheCodedProf35e73712023-03-10 17:35:35 -0500208 break;
209 }
210 case "spam:Suggestion": {
211 name = "Marked as Spam";
212 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500213 await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" });
214 await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "spam" });
TheCodedProf35e73712023-03-10 17:35:35 -0500215 break;
216 }
217 }
218
TheCodedProf46518a42023-02-18 17:08:23 -0500219 const newcolor = accept ? "Success" : "Danger";
TheCodedProf35e73712023-03-10 17:35:35 -0500220 const newEmoji = accept ? "ICONS.ADD" : "ICONS.OPP.ADD";
PineaFan1dee28f2023-01-16 22:09:07 +0000221
222 const newEmbed = new EmojiEmbed()
TheCodedProf35e73712023-03-10 17:35:35 -0500223 .setEmoji(newEmoji)
224 .setTitle(embed!.title!.replace(/.+> /, ""))
PineaFan1dee28f2023-01-16 22:09:07 +0000225 .setDescription(embed!.description!)
TheCodedProf35e73712023-03-10 17:35:35 -0500226 .setFields({
227 name: name + " by",
TheCodedProfca29ebb2023-03-10 17:40:09 -0500228 value: interaction.user.tag
TheCodedProf35e73712023-03-10 17:35:35 -0500229 })
230 .setStatus(newcolor)
231 .setFooter(embed!.footer);
PineaFan1dee28f2023-01-16 22:09:07 +0000232
TheCodedProf35e73712023-03-10 17:35:35 -0500233 await interaction.editReply({
234 embeds: [newEmbed],
235 components: components
236 });
PineaFan1dee28f2023-01-16 22:09:07 +0000237}
238
PineaFan752af462022-12-31 21:59:38 +0000239export async function callback(_client: NucleusClient, interaction: Interaction) {
pineafan63fc5e22022-08-04 22:04:10 +0100240 await interactionCreate(interaction);
Skyler Grey75ea9172022-08-06 10:22:23 +0100241}