blob: 9d3dcebbbea756d760b2f1aca1a6e7aa4bd97877 [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";
TheCodedProf35e73712023-03-10 17:35:35 -050027import client from "../utils/client.js";
pineafan6de4da52023-03-07 20:43:44 +000028
pineafan63fc5e22022-08-04 22:04:10 +010029export const event = "interactionCreate";
pineafanad54d752022-04-18 19:01:43 +010030
pineafan6de4da52023-03-07 20:43:44 +000031async function errorMessage(interaction: ButtonInteraction, message: string) {
32 await interaction.reply({
pineafan1e462ab2023-03-07 21:34:06 +000033 embeds: [new EmojiEmbed().setDescription(message).setStatus("Danger")],
pineafan6de4da52023-03-07 20:43:44 +000034 ephemeral: true,
35 components: []
36 });
37}
38
pineafan0f5cc782022-08-12 21:55:42 +010039async function interactionCreate(interaction: Interaction) {
PineaFana34d04b2023-01-03 22:05:42 +000040 if (interaction.isButton()) {
TheCodedProf35e73712023-03-10 17:35:35 -050041 if (interaction.customId.endsWith(":Suggestion")) {
TheCodedProfca29ebb2023-03-10 17:40:09 -050042 const value =
43 interaction.customId.startsWith("accept") || interaction.customId.startsWith("implement")
44 ? true
45 : false;
TheCodedProf35e73712023-03-10 17:35:35 -050046 return await modifySuggestion(interaction, value);
47 }
PineaFan538d3752023-01-12 21:48:23 +000048 switch (interaction.customId) {
Skyler Greyda16adf2023-03-05 10:22:12 +000049 case "rolemenu": {
50 return await roleMenu(interaction);
51 }
52 case "verifybutton": {
53 return await verify(interaction);
54 }
55 case "createticket": {
56 return await create(interaction);
57 }
58 case "closeticket": {
59 return await close(interaction);
60 }
61 case "createtranscript": {
62 return await createTranscript(interaction);
63 }
pineafan02ba0232022-07-24 22:16:15 +010064 }
pineafan6de4da52023-03-07 20:43:44 +000065 // Mod actions
66 if (interaction.customId.startsWith("mod:")) {
67 const action = interaction.customId.split(":")[1];
68 const memberId = interaction.customId.split(":")[2];
69 const member = await interaction.guild?.members.fetch(memberId!);
70 switch (action) {
71 case "kick": {
TheCodedProf35e73712023-03-10 17:35:35 -050072 const check = kickCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000073 if (check !== true) return await errorMessage(interaction, check!);
74 return await kickCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000075 }
76 case "ban": {
TheCodedProf35e73712023-03-10 17:35:35 -050077 const check = banCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000078 if (check !== true) return await errorMessage(interaction, check!);
79 return await banCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000080 }
81 case "mute": {
TheCodedProf35e73712023-03-10 17:35:35 -050082 const check = muteCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000083 if (check !== true) return await errorMessage(interaction, check!);
84 return await muteCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000085 }
86 case "nickname": {
TheCodedProf35e73712023-03-10 17:35:35 -050087 const check = nicknameCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000088 if (check !== true) return await errorMessage(interaction, check || "Something went wrong");
89 return await nicknameCallback(interaction, member);
pineafan1e462ab2023-03-07 21:34:06 +000090 }
91 case "warn": {
TheCodedProf35e73712023-03-10 17:35:35 -050092 const check = warnCheck(interaction, false, member);
pineafan6de4da52023-03-07 20:43:44 +000093 if (check !== true) return await errorMessage(interaction, check!);
94 return await warnCallback(interaction, member);
95 }
96 }
97 }
pineafanad54d752022-04-18 19:01:43 +010098 }
99}
100
TheCodedProf35e73712023-03-10 17:35:35 -0500101const getReason = async (buttonInteraction: ButtonInteraction, prompt: string) => {
102 const modal = new ModalBuilder()
103 .addComponents(
104 new ActionRowBuilder<TextInputBuilder>().addComponents(
TheCodedProfca29ebb2023-03-10 17:40:09 -0500105 new TextInputBuilder().setStyle(TextInputStyle.Paragraph).setLabel(prompt).setCustomId("typed")
TheCodedProf35e73712023-03-10 17:35:35 -0500106 )
107 )
108 .setTitle("Reason")
109 .setCustomId("modal");
110 await buttonInteraction.showModal(modal);
111 let out: ModalSubmitInteraction;
112 try {
113 out = await buttonInteraction.awaitModalSubmit({
114 filter: (i) => i.customId === "modal" && i.user.id === buttonInteraction.user.id,
115 time: 300000
116 });
117 } catch {
118 return null;
119 }
120 await out.deferUpdate();
121 return out.fields.getTextInputValue("typed");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500122};
TheCodedProf35e73712023-03-10 17:35:35 -0500123
124async function modifySuggestion(interaction: ButtonInteraction, accept: boolean) {
125 const message = interaction.message;
PineaFan1dee28f2023-01-16 22:09:07 +0000126 await message.fetch();
127 if (message.embeds.length === 0) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500128 const embed = message.embeds[0]!;
TheCodedProfca29ebb2023-03-10 17:40:09 -0500129 const issueNum = embed.footer!.text;
130 if (!issueNum) return;
TheCodedProf35e73712023-03-10 17:35:35 -0500131 const issue = {
132 owner: "ClicksMinutePer",
133 repo: "Nucleus",
134 issue_number: parseInt(issueNum)
TheCodedProfca29ebb2023-03-10 17:40:09 -0500135 };
TheCodedProf35e73712023-03-10 17:35:35 -0500136 let name = "Unknown";
137 const components: InteractionEditReplyOptions["components"] = [];
TheCodedProfca29ebb2023-03-10 17:40:09 -0500138 switch (interaction.customId) {
TheCodedProf35e73712023-03-10 17:35:35 -0500139 case "accept:Suggestion": {
140 name = "Accepted";
141 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500142 await client.GitHub.rest.issues.createComment({
143 ...issue,
144 body: "Suggestion accepted by " + interaction.user.tag
145 });
146 components.push(
147 new ActionRowBuilder<ButtonBuilder>().addComponents(
148 new ButtonBuilder()
149 .setCustomId("close:Suggestion")
150 .setLabel("Close")
151 .setStyle(ButtonStyle.Secondary),
152 new ButtonBuilder()
153 .setCustomId("implemented:Suggestion")
154 .setLabel("Implemented")
155 .setStyle(ButtonStyle.Secondary)
156 )
157 );
TheCodedProf35e73712023-03-10 17:35:35 -0500158 break;
159 }
160 case "deny:Suggestion": {
161 name = "Denied";
162 const reason = await getReason(interaction, "Reason for denial");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500163 await client.GitHub.rest.issues.createComment({
164 ...issue,
165 body: "Suggestion denied by " + interaction.user.tag + " for reason:\n>" + reason
166 });
167 await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" });
TheCodedProf35e73712023-03-10 17:35:35 -0500168 // await client.GitHub.rest.issues.lock({...issue, lock_reason: "resolved"})
TheCodedProfca29ebb2023-03-10 17:40:09 -0500169 components.push(
170 new ActionRowBuilder<ButtonBuilder>().addComponents(
171 new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger)
172 )
173 );
TheCodedProf35e73712023-03-10 17:35:35 -0500174 break;
175 }
176 case "close:Suggestion": {
177 name = "Closed";
178 const reason = await getReason(interaction, "Reason for closing");
TheCodedProfca29ebb2023-03-10 17:40:09 -0500179 await client.GitHub.rest.issues.createComment({
180 ...issue,
181 body: "Suggestion closed by " + interaction.user.tag + " for reason:\n>" + reason
182 });
183 await client.GitHub.rest.issues.update({ ...issue, state: "closed" });
TheCodedProf35e73712023-03-10 17:35:35 -0500184 // await client.GitHub.rest.issues.lock({...issue})
TheCodedProfca29ebb2023-03-10 17:40:09 -0500185 components.push(
186 new ActionRowBuilder<ButtonBuilder>().addComponents(
187 new ButtonBuilder().setCustomId("lock:Suggestion").setLabel("Lock").setStyle(ButtonStyle.Danger)
188 )
189 );
TheCodedProf35e73712023-03-10 17:35:35 -0500190 break;
191 }
192 case "implement:Suggestion": {
193 name = "Implemented";
194 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500195 await client.GitHub.rest.issues.createComment({ ...issue, body: "Suggestion implemented" });
196 await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "completed" });
197 await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "resolved" });
TheCodedProf35e73712023-03-10 17:35:35 -0500198 break;
199 }
200 case "lock:Suggestion": {
201 name = "Locked";
202 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500203 await client.GitHub.rest.issues.lock({ ...issue });
TheCodedProf35e73712023-03-10 17:35:35 -0500204 break;
205 }
206 case "spam:Suggestion": {
207 name = "Marked as Spam";
208 await interaction.deferUpdate();
TheCodedProfca29ebb2023-03-10 17:40:09 -0500209 await client.GitHub.rest.issues.update({ ...issue, state: "closed", state_reason: "not_planned" });
210 await client.GitHub.rest.issues.lock({ ...issue, lock_reason: "spam" });
TheCodedProf35e73712023-03-10 17:35:35 -0500211 break;
212 }
213 }
214
TheCodedProf46518a42023-02-18 17:08:23 -0500215 const newcolor = accept ? "Success" : "Danger";
TheCodedProf35e73712023-03-10 17:35:35 -0500216 const newEmoji = accept ? "ICONS.ADD" : "ICONS.OPP.ADD";
PineaFan1dee28f2023-01-16 22:09:07 +0000217
218 const newEmbed = new EmojiEmbed()
TheCodedProf35e73712023-03-10 17:35:35 -0500219 .setEmoji(newEmoji)
220 .setTitle(embed!.title!.replace(/.+> /, ""))
PineaFan1dee28f2023-01-16 22:09:07 +0000221 .setDescription(embed!.description!)
TheCodedProf35e73712023-03-10 17:35:35 -0500222 .setFields({
223 name: name + " by",
TheCodedProfca29ebb2023-03-10 17:40:09 -0500224 value: interaction.user.tag
TheCodedProf35e73712023-03-10 17:35:35 -0500225 })
226 .setStatus(newcolor)
227 .setFooter(embed!.footer);
PineaFan1dee28f2023-01-16 22:09:07 +0000228
TheCodedProf35e73712023-03-10 17:35:35 -0500229 await interaction.editReply({
230 embeds: [newEmbed],
231 components: components
232 });
PineaFan1dee28f2023-01-16 22:09:07 +0000233}
234
PineaFan752af462022-12-31 21:59:38 +0000235export async function callback(_client: NucleusClient, interaction: Interaction) {
pineafan63fc5e22022-08-04 22:04:10 +0100236 await interactionCreate(interaction);
Skyler Grey75ea9172022-08-06 10:22:23 +0100237}