import { HaltExecution } from "@purrkit/router"; import { Result } from "@sapphire/framework"; import { getTicketByChannelId, getTicketByGuild, getTicketById } from "@stealth-developers/db"; import { kitten } from "@/client"; import { getTextChannel, logger, upsertUser } from "@/lib"; const base = kitten.builder(); export const useGetData = base.use(async (interaction) => { if (!interaction.guild) return {}; const memberOrUser = interaction.member ?? interaction.user; const result = await upsertUser(memberOrUser, interaction.guild); if (!result) throw new HaltExecution("Failed to add you to the database. Please try again."); return { user: result.user, actor: result.actor, guild: result.guild, }; }); export const useGetTicketData = useGetData.use(async (interaction, { guild }) => { if (interaction.isChatInputCommand()) { const ticketId = interaction.options.getNumber("ticket", false); if (ticketId && guild) { const ticket = await getTicketByGuild(guild.id, ticketId); if (!ticket) return {}; return { ticket }; } } if (interaction.isButton()) { const customId = interaction.customId; if (customId.startsWith("ticket:")) { const ticketId = customId.split(":")[1]; if (ticketId) { const ticket = await getTicketById(ticketId); if (!ticket) return {}; if (!ticket.channelId) return { ticket }; const ticketChannelResult = await Result.fromAsync(getTextChannel(ticket.channelId, true)); if (ticketChannelResult.isErr()) { logger.error( ticketChannelResult.unwrapErr(), "Failed to get ticket channel in middleware", ); return { ticket }; } return { ticket, ticketChannel: ticketChannelResult.unwrap() }; } } else { return {}; } } if (!interaction.channel) { logger.warn({ interaction }, "No channel found in interaction"); return {}; } const ticket = await getTicketByChannelId(interaction.channel.id); if (!ticket) { logger.warn({ interaction }, `No ticket found in channel ${interaction.channel.id}`); return {}; } if (!ticket.channelId) return { ticket }; const ticketChannel = await getTextChannel(ticket.channelId, true); return { ticket, ticketChannel }; });