diff --git a/src/modules/bans.ts b/src/modules/bans.ts index fafef78..e30abc4 100644 --- a/src/modules/bans.ts +++ b/src/modules/bans.ts @@ -1,5 +1,5 @@ -import { ban } from "@prisma/client"; -import bot, { prisma } from ".."; +import { ban, orderStatus } from "@prisma/client"; +import messagesClient, { prisma } from ".."; import { ActionRowBuilder, ButtonBuilder, @@ -12,6 +12,9 @@ import { UserSelectMenuBuilder, } from "discord.js"; import * as chrono from "chrono-node"; +import { getActiveOrdersForUser, updateOrder } from "../orders/cache"; +import updateOrderStatus from "../orders/updateStatus"; +import bot from ".."; const bans: ban[] = []; //load active bans @@ -47,9 +50,10 @@ const addBan = async ( }, }); bans.push(ban); + return ban; }; -bot.registerButton("devtools:manage-bans", async (interaction) => { +messagesClient.registerButton("devtools:manage-bans", async (interaction) => { const a1 = new ActionRowBuilder().addComponents([ new UserSelectMenuBuilder().setCustomId("devtools:manage-bans:user"), ]); @@ -58,92 +62,98 @@ bot.registerButton("devtools:manage-bans", async (interaction) => { }); }); -bot.registerUserSelectMenu("devtools:manage-bans:user", async (interaction) => { - const user = interaction.users.first()!; - const bans = userActiveBans(user.id); - const embed = new EmbedBuilder() - .setTitle(`Bans for ${user.tag}`) - .setDescription( - bans.length > 0 ? "User is currently banned" : "User is not banned" - ) - .addFields( - bans.map((ban) => ({ - name: ban.id.toString(), - value: `Reason: ${ban.reason}\nMessage: ${ - ban.message - }\nEnd at: \nAppeal at: `, - })) - ); - const a1 = new ActionRowBuilder().addComponents([ - new StringSelectMenuBuilder() - .setCustomId("devtools:manage-bans:select") - .setPlaceholder("Select a ban") - .addOptions( +messagesClient.registerUserSelectMenu( + "devtools:manage-bans:user", + async (interaction) => { + const user = interaction.users.first()!; + const bans = userActiveBans(user.id); + const embed = new EmbedBuilder() + .setTitle(`Bans for ${user.tag}`) + .setDescription( + bans.length > 0 ? "User is currently banned" : "User is not banned" + ) + .addFields( bans.map((ban) => ({ - label: `Ban ${ban.id}`, - value: ban.id.toString(), + name: ban.id.toString(), + value: `Reason: ${ban.reason}\nMessage: ${ + ban.message + }\nEnd at: \nAppeal at: `, })) - ) - .setDisabled(bans.length == 0), - ]); - if (bans.length == 0) - a1.components[0].addOptions({ - label: "No bans", - value: "no-bans", + ); + const a1 = new ActionRowBuilder().addComponents([ + new StringSelectMenuBuilder() + .setCustomId("devtools:manage-bans:select") + .setPlaceholder("Select a ban") + .addOptions( + bans.map((ban) => ({ + label: `Ban ${ban.id}`, + value: ban.id.toString(), + })) + ) + .setDisabled(bans.length == 0), + ]); + if (bans.length == 0) + a1.components[0].addOptions({ + label: "No bans", + value: "no-bans", + }); + const a2 = new ActionRowBuilder().addComponents([ + new ButtonBuilder() + .setCustomId(`devtools:manage-bans:create:${user.id}`) + .setLabel("Create ban") + .setStyle(ButtonStyle.Danger) + .setEmoji("🔨"), + ]); + return await interaction.update({ + embeds: [embed], + components: [a1, a2], }); - const a2 = new ActionRowBuilder().addComponents([ - new ButtonBuilder() - .setCustomId(`devtools:manage-bans:create:${user.id}`) - .setLabel("Create ban") - .setStyle(ButtonStyle.Danger) - .setEmoji("🔨"), - ]); - return await interaction.update({ - embeds: [embed], - components: [a1, a2], - }); -}); + } +); -bot.registerButton(/devtools:manage-bans:create:(.+)/, async (interaction) => { - const userId = interaction.customId.split(":")[3]; - const modal = new ModalBuilder() - .setTitle("Create ban") - .setCustomId(`devtools:manage-bans:create:${userId}:modal`) - .addComponents([ - new ActionRowBuilder().addComponents([ - new TextInputBuilder() - .setCustomId("reason") - .setLabel("reason") - .setStyle(TextInputStyle.Paragraph), - ]), - new ActionRowBuilder().addComponents([ - new TextInputBuilder() - .setCustomId("message") - .setLabel("message") - .setStyle(TextInputStyle.Paragraph), - ]), - new ActionRowBuilder().addComponents([ - new TextInputBuilder() - .setCustomId("endAt") - .setLabel("end at") - .setStyle(TextInputStyle.Short), - ]), - new ActionRowBuilder().addComponents([ - new TextInputBuilder() - .setCustomId("appealAt") - .setLabel("appeal at") - .setStyle(TextInputStyle.Short) - .setRequired(false), - ]), - ]); - return await interaction.showModal(modal); -}); +messagesClient.registerButton( + /devtools:manage-bans:create:(.+)/, + async (interaction) => { + const userId = interaction.customId.split(":")[3]; + const modal = new ModalBuilder() + .setTitle("Create ban") + .setCustomId(`devtools:manage-bans:create:${userId}:modal`) + .addComponents([ + new ActionRowBuilder().addComponents([ + new TextInputBuilder() + .setCustomId("reason") + .setLabel("reason") + .setStyle(TextInputStyle.Paragraph), + ]), + new ActionRowBuilder().addComponents([ + new TextInputBuilder() + .setCustomId("message") + .setLabel("message") + .setStyle(TextInputStyle.Paragraph), + ]), + new ActionRowBuilder().addComponents([ + new TextInputBuilder() + .setCustomId("endAt") + .setLabel("end at") + .setStyle(TextInputStyle.Short), + ]), + new ActionRowBuilder().addComponents([ + new TextInputBuilder() + .setCustomId("appealAt") + .setLabel("appeal at") + .setStyle(TextInputStyle.Short) + .setRequired(false), + ]), + ]); + return await interaction.showModal(modal); + } +); -bot.registerModal( +messagesClient.registerModal( /devtools:manage-bans:create:(.+):modal/, async (interaction) => { const userId = interaction.customId.split(":")[3]; @@ -162,10 +172,25 @@ bot.registerModal( ephemeral: true, }); - await addBan(userId, reason, message, endAt, appealAt ?? undefined); - return await interaction.reply({ + const ban = await addBan( + userId, + reason, + message, + endAt, + appealAt ?? undefined + ); + await interaction.reply({ content: "Ban added", ephemeral: true, }); + const orders = getActiveOrdersForUser(userId); + for (const order of orders) { + await updateOrderStatus({ + id: order.id, + status: orderStatus.REJECTED, + reason: `You are banned from ordering. Ban ID: ${ban.id}`, + chef: bot.client.user!.id, + }); + } } );