Skip to content

Commit

Permalink
fix to make it work on the real bot
Browse files Browse the repository at this point in the history
also reject orders when banning
  • Loading branch information
mrhappyma committed Jan 18, 2025
1 parent 75b73f0 commit 5b81d55
Showing 1 changed file with 111 additions and 86 deletions.
197 changes: 111 additions & 86 deletions src/modules/bans.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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<UserSelectMenuBuilder>().addComponents([
new UserSelectMenuBuilder().setCustomId("devtools:manage-bans:user"),
]);
Expand All @@ -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: <t:${Math.floor(
ban.endAt.getTime() / 1000
)}:R>\nAppeal at: <t:${Math.floor(
(ban.appealAt?.getTime() ?? 0) / 1000
)}:R>`,
}))
);
const a1 = new ActionRowBuilder<StringSelectMenuBuilder>().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: <t:${Math.floor(
ban.endAt.getTime() / 1000
)}:R>\nAppeal at: <t:${Math.floor(
(ban.appealAt?.getTime() ?? 0) / 1000
)}:R>`,
}))
)
.setDisabled(bans.length == 0),
]);
if (bans.length == 0)
a1.components[0].addOptions({
label: "No bans",
value: "no-bans",
);
const a1 = new ActionRowBuilder<StringSelectMenuBuilder>().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<ButtonBuilder>().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<ButtonBuilder>().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<TextInputBuilder>().addComponents([
new TextInputBuilder()
.setCustomId("reason")
.setLabel("reason")
.setStyle(TextInputStyle.Paragraph),
]),
new ActionRowBuilder<TextInputBuilder>().addComponents([
new TextInputBuilder()
.setCustomId("message")
.setLabel("message")
.setStyle(TextInputStyle.Paragraph),
]),
new ActionRowBuilder<TextInputBuilder>().addComponents([
new TextInputBuilder()
.setCustomId("endAt")
.setLabel("end at")
.setStyle(TextInputStyle.Short),
]),
new ActionRowBuilder<TextInputBuilder>().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<TextInputBuilder>().addComponents([
new TextInputBuilder()
.setCustomId("reason")
.setLabel("reason")
.setStyle(TextInputStyle.Paragraph),
]),
new ActionRowBuilder<TextInputBuilder>().addComponents([
new TextInputBuilder()
.setCustomId("message")
.setLabel("message")
.setStyle(TextInputStyle.Paragraph),
]),
new ActionRowBuilder<TextInputBuilder>().addComponents([
new TextInputBuilder()
.setCustomId("endAt")
.setLabel("end at")
.setStyle(TextInputStyle.Short),
]),
new ActionRowBuilder<TextInputBuilder>().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];
Expand All @@ -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,
});
}
}
);

0 comments on commit 5b81d55

Please sign in to comment.