From fa921fa5a6be16eaae0b7c9f11c9714d4ad3a51e Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 23 May 2023 16:31:53 +0100 Subject: [PATCH] fix: duplicated ticket numbers (fixes #418) Now there is a change that a number will be skipped (if there's an error), but that isn't a problem. --- src/lib/sync.js | 12 +++++++++++- src/lib/tickets/manager.js | 8 +++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib/sync.js b/src/lib/sync.js index 91f3d58..b0c3800 100644 --- a/src/lib/sync.js +++ b/src/lib/sync.js @@ -2,6 +2,16 @@ * @param {import("client")} client */ module.exports = async client => { + // load ticket numbers + const guilds = await client.prisma.guild.findMany({ select: { id: true } }); + for (const guild of guilds) { + const { _max: { number: max } } = await client.prisma.ticket.aggregate({ + _max: { number: true }, + where: { guildId: guild.id }, + }); + client.tickets.$numbers[guild.id] = max ?? 0; + } + // load total number of open tickets const categories = await client.prisma.category.findMany({ select: { @@ -60,4 +70,4 @@ module.exports = async client => { client.log.info(`Loaded ${cooldowns} active cooldowns`); client.log.info(`Closed ${deleted} deleted tickets`); return true; -}; \ No newline at end of file +}; diff --git a/src/lib/tickets/manager.js b/src/lib/tickets/manager.js index ee75512..9e0167e 100644 --- a/src/lib/tickets/manager.js +++ b/src/lib/tickets/manager.js @@ -48,6 +48,7 @@ module.exports = class TicketManager { this.client = client; this.archiver = new TicketArchiver(client); this.$count = { categories: {} }; + this.$numbers = {}; this.$stale = new Collection(); } @@ -134,6 +135,11 @@ module.exports = class TicketManager { return await this.client.keyv.get(cacheKey); } + getNextNumber(guildId) { + this.$numbers[guildId] += 1; + return this.$numbers[guildId]; + } + /** * @param {object} data * @param {string} data.categoryId @@ -373,7 +379,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 = (await this.client.prisma.ticket.count({ where: { guildId: category.guild.id } })) + 1; + const number = 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)