fix: creating the first ticket in a new guild (closes #428)

Previously required `sync`ing (or restarting) to avoid the ticket number error.
This commit is contained in:
Isaac 2023-05-31 15:10:51 +01:00
parent 2e18fbbef2
commit fbc08c6cd0
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33

View File

@ -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)