From fbc08c6cd07be366017a9295d8e5ca5afd848180 Mon Sep 17 00:00:00 2001 From: Isaac Date: Wed, 31 May 2023 15:10:51 +0100 Subject: [PATCH] fix: creating the first ticket in a new guild (closes #428) Previously required `sync`ing (or restarting) to avoid the ticket number error. --- src/lib/tickets/manager.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib/tickets/manager.js b/src/lib/tickets/manager.js index ca2c900..0983379 100644 --- a/src/lib/tickets/manager.js +++ b/src/lib/tickets/manager.js @@ -135,7 +135,14 @@ module.exports = class TicketManager { return await this.client.keyv.get(cacheKey); } - getNextNumber(guildId) { + async getNextNumber(guildId) { + if (this.$numbers[guildId] === undefined) { + const { _max: { number: max } } = await this.client.prisma.ticket.aggregate({ + _max: { number: true }, + where: { guildId }, + }); + this.client.tickets.$numbers[guildId] = max ?? 0; + } this.$numbers[guildId] += 1; return this.$numbers[guildId]; } @@ -379,7 +386,7 @@ module.exports = class TicketManager { const guild = this.client.guilds.cache.get(category.guild.id); const getMessage = this.client.i18n.getLocale(category.guild.locale); const creator = await guild.members.fetch(interaction.user.id); - const number = this.getNextNumber(category.guild.id); + const number = await this.getNextNumber(category.guild.id); const channelName = category.channelName .replace(/{+\s?(user)?name\s?}+/gi, creator.user.username) .replace(/{+\s?(nick|display)(name)?\s?}+/gi, creator.displayName)