Skip to content

Commit

Permalink
reject active orders when the bot is kicked
Browse files Browse the repository at this point in the history
& don't error when updating the order message fails

fixes BOT-3A
fixes BOT-3B
fixes BOT-2B
  • Loading branch information
mrhappyma committed Jan 13, 2025
1 parent 14393f9 commit fcb221a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/modules/leave-reject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { orderStatus } from "@prisma/client";
import bot from "..";
import { getActiveOrdersForGuild } from "../orders/cache";
import updateOrderStatus from "../orders/updateStatus";

bot.client.on("guildDelete", async (guild) => {
const orders = getActiveOrdersForGuild(guild.id);
for (const order of orders) {
await updateOrderStatus({
id: order.id,
status: orderStatus.REJECTED,
chef: bot.client.user!.id,
reason: "Guild became unavailable",
});
}
});
6 changes: 6 additions & 0 deletions src/orders/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export const getActiveOrdersForChef = (id: string) => {
);
};

export const getActiveOrdersForGuild = (id: string) => {
return Array.from(cache.values()).filter(
(order) => order.guildId == id && activeOrderStatuses.includes(order.status)
);
};

export const createOrder = async (
order: string,
guildId: string,
Expand Down
1 change: 0 additions & 1 deletion src/orders/updateStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ButtonBuilder,
ButtonStyle,
EmbedBuilder,
Message,
TextBasedChannel,
WebhookMessageCreateOptions,
} from "discord.js";
Expand Down
36 changes: 21 additions & 15 deletions src/utils/updateOrderStatusMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@ const messages = [
const updateOrderStatusMessage = async (order: order, message: string) => {
const footerMessage = messages[Math.floor(Math.random() * messages.length)];

const orderChannel = await (
await bot.client.guilds.fetch(order.guildId)
).channels.fetch(order.channelId);
if (!orderChannel?.isTextBased()) return false;
const orderMessage = await orderChannel.messages.fetch(order.statusMessageId);
try {
const orderChannel = await (
await bot.client.guilds.fetch(order.guildId)
).channels.fetch(order.channelId);
if (!orderChannel?.isTextBased()) return false;
const orderMessage = await orderChannel.messages.fetch(
order.statusMessageId
);

orderMessage.edit({
embeds: [
{
title: `Order status - ${order.order}`,
description: message,
footer: {
text: `Order #${order.id} | ${footerMessage}`,
orderMessage.edit({
embeds: [
{
title: `Order status - ${order.order}`,
description: message,
footer: {
text: `Order #${order.id} | ${footerMessage}`,
},
},
},
],
});
],
});
} catch {
return false;
}
};
export default updateOrderStatusMessage;

0 comments on commit fcb221a

Please sign in to comment.