From fcb221a2757f2d2b8aa213f676804f650607310b Mon Sep 17 00:00:00 2001 From: Dominic Ruggiero Date: Sun, 12 Jan 2025 20:59:15 -0500 Subject: [PATCH] reject active orders when the bot is kicked & don't error when updating the order message fails fixes BOT-3A fixes BOT-3B fixes BOT-2B --- src/modules/leave-reject.ts | 16 ++++++++++++ src/orders/cache.ts | 6 +++++ src/orders/updateStatus.ts | 1 - src/utils/updateOrderStatusMessage.ts | 36 ++++++++++++++++----------- 4 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 src/modules/leave-reject.ts diff --git a/src/modules/leave-reject.ts b/src/modules/leave-reject.ts new file mode 100644 index 0000000..8d6c4e7 --- /dev/null +++ b/src/modules/leave-reject.ts @@ -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", + }); + } +}); diff --git a/src/orders/cache.ts b/src/orders/cache.ts index daf0b35..af4c110 100644 --- a/src/orders/cache.ts +++ b/src/orders/cache.ts @@ -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, diff --git a/src/orders/updateStatus.ts b/src/orders/updateStatus.ts index 86214b8..0a0aabc 100644 --- a/src/orders/updateStatus.ts +++ b/src/orders/updateStatus.ts @@ -5,7 +5,6 @@ import { ButtonBuilder, ButtonStyle, EmbedBuilder, - Message, TextBasedChannel, WebhookMessageCreateOptions, } from "discord.js"; diff --git a/src/utils/updateOrderStatusMessage.ts b/src/utils/updateOrderStatusMessage.ts index 48cba04..29b0d31 100644 --- a/src/utils/updateOrderStatusMessage.ts +++ b/src/utils/updateOrderStatusMessage.ts @@ -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;