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';