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.
This commit is contained in:
Isaac 2023-05-23 16:31:53 +01:00
parent a4696273ea
commit fa921fa5a6
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
2 changed files with 18 additions and 2 deletions

View File

@ -2,6 +2,16 @@
* @param {import("client")} client * @param {import("client")} client
*/ */
module.exports = async 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 // load total number of open tickets
const categories = await client.prisma.category.findMany({ const categories = await client.prisma.category.findMany({
select: { select: {
@ -60,4 +70,4 @@ module.exports = async client => {
client.log.info(`Loaded ${cooldowns} active cooldowns`); client.log.info(`Loaded ${cooldowns} active cooldowns`);
client.log.info(`Closed ${deleted} deleted tickets`); client.log.info(`Closed ${deleted} deleted tickets`);
return true; return true;
}; };

View File

@ -48,6 +48,7 @@ module.exports = class TicketManager {
this.client = client; this.client = client;
this.archiver = new TicketArchiver(client); this.archiver = new TicketArchiver(client);
this.$count = { categories: {} }; this.$count = { categories: {} };
this.$numbers = {};
this.$stale = new Collection(); this.$stale = new Collection();
} }
@ -134,6 +135,11 @@ module.exports = class TicketManager {
return await this.client.keyv.get(cacheKey); return await this.client.keyv.get(cacheKey);
} }
getNextNumber(guildId) {
this.$numbers[guildId] += 1;
return this.$numbers[guildId];
}
/** /**
* @param {object} data * @param {object} data
* @param {string} data.categoryId * @param {string} data.categoryId
@ -373,7 +379,7 @@ module.exports = class TicketManager {
const guild = this.client.guilds.cache.get(category.guild.id); const guild = this.client.guilds.cache.get(category.guild.id);
const getMessage = this.client.i18n.getLocale(category.guild.locale); const getMessage = this.client.i18n.getLocale(category.guild.locale);
const creator = await guild.members.fetch(interaction.user.id); 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 const channelName = category.channelName
.replace(/{+\s?(user)?name\s?}+/gi, creator.user.username) .replace(/{+\s?(user)?name\s?}+/gi, creator.user.username)
.replace(/{+\s?(nick|display)(name)?\s?}+/gi, creator.displayName) .replace(/{+\s?(nick|display)(name)?\s?}+/gi, creator.displayName)