From 424de10a8c3008e26b92b319bbce382e16be73ad Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Sun, 25 Feb 2024 20:39:32 +1100 Subject: [PATCH] Optimize functions in punishment model --- src/commands/case.ts | 8 ++++---- src/commands/unpunish.ts | 2 +- src/models/punishments.ts | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/commands/case.ts b/src/commands/case.ts index d3be107..0df2ff9 100644 --- a/src/commands/case.ts +++ b/src/commands/case.ts @@ -25,16 +25,16 @@ export default class Case { update: async()=>{ const reason = interaction.options.getString('reason'); await client.punishments.updateReason(caseId, reason); - if (client.punishments.findCase(caseId)) { + if (client.punishments.findCaseOrCancels('case_id', caseId)) { await this.updateEntry(client, caseId, reason); await interaction.reply({embeds: [new client.embed().setColor(client.config.embedColorGreen).setTitle('Case updated').setDescription(`Case #${caseId} has been successfully updated with new reason:\n\`${reason}\``)]}); } else interaction.reply({embeds: [new client.embed().setColor(client.config.embedColorRed).setTitle('Case not updated').setDescription(`Case #${caseId} is not found in database, not updating the reason.`)]}); }, view: async()=>{ - const punishment = await client.punishments.findCase(caseId); + const punishment = await client.punishments.findCaseOrCancels('case_id', caseId); if (!punishment) return interaction.reply('Case ID is not found in database.'); - const cancelledBy = punishment.dataValues.expired ? await client.punishments.findByCancels(punishment.dataValues.case_id) : null; - const cancels = punishment.dataValues.cancels ? await client.punishments.findCase(punishment.dataValues.cancels) : null; + const cancelledBy = punishment.dataValues.expired ? await client.punishments.findCaseOrCancels('cancels', punishment.dataValues.case_id) : null; + const cancels = punishment.dataValues.cancels ? await client.punishments.findCaseOrCancels('case_id', punishment.dataValues.cancels) : null; const embed = new client.embed().setColor(client.config.embedColor).setTimestamp(Number(punishment.dataValues.time)).setTitle(`${punishment.dataValues.type[0].toUpperCase()+punishment.dataValues.type.slice(1)} | Case #${punishment.dataValues.case_id}`).addFields( {name: 'User', value: `${punishment.member_name}\n${MessageTool.formatMention(punishment.dataValues.member, 'user')}\n\`${punishment.dataValues.member}\``, inline: true}, {name: 'Moderator', value: `${client.users.resolve(punishment.moderator).tag}\n${MessageTool.formatMention(punishment.dataValues.moderator, 'user')}\n\`${punishment.dataValues.moderator}\``, inline: true}, diff --git a/src/commands/unpunish.ts b/src/commands/unpunish.ts index 7c61bd7..9780227 100644 --- a/src/commands/unpunish.ts +++ b/src/commands/unpunish.ts @@ -5,7 +5,7 @@ import MessageTool from '../helpers/MessageTool.js'; export default class Unpunish { static async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ if (!MessageTool.isModerator(interaction.member as Discord.GuildMember)) return MessageTool.youNeedRole(interaction, 'dcmod'); - const punishment = await client.punishments.findCase(interaction.options.getInteger('case_id', true)); + const punishment = await client.punishments.findCaseOrCancels('case_id', interaction.options.getInteger('case_id', true)); if (!punishment) return interaction.reply({content: 'Case ID is not found in database.', ephemeral: true}); if (['unban', 'unmute', 'punishmentOverride'].includes(punishment.dataValues.type)) return interaction.reply({content: 'This case ID is immutable. (Informative case)', ephemeral: true}); if (punishment.dataValues.expired) return interaction.reply({content: 'This case ID is already expired.', ephemeral: true}); diff --git a/src/models/punishments.ts b/src/models/punishments.ts index 17c54de..2e212ec 100644 --- a/src/models/punishments.ts +++ b/src/models/punishments.ts @@ -96,15 +96,15 @@ export class PunishmentsSvc { } query = async(pattern:string)=>await this.model.sequelize.query(pattern); async updateReason(caseId:number, reason:string) { - const findCase = this.findCase(caseId); - if (findCase) return this.model.update({reason: reason}, {where: {case_id: caseId}}); + const findCase = this.findCaseOrCancels('case_id', caseId); + if (findCase) return this.model.update({reason}, {where: {case_id: caseId}}); } - findCase =(caseId:number)=>this.model.findOne({where: {case_id: caseId}}); - findByCancels =(caseId:number)=>this.model.findOne({where: {cancels: caseId}}) + findCaseOrCancels = (column:'case_id'|'cancels', id:number)=>this.model.findOne({where: {[column]: id}}); getAllCases =()=>this.model.findAll(); async generateCaseId() { const result = await this.model.max('case_id'); - return (result as number ?? 0) + 1; + if (typeof result === 'number') return result + 1; + else return 0; } async findInCache():Promise { const cacheKey = 'punishments';