1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-18 00:31:00 -05:00

Optimize functions in punishment model

This commit is contained in:
toast-ts 2024-02-25 20:39:32 +11:00
parent cd40816e8f
commit 424de10a8c
3 changed files with 10 additions and 10 deletions

View File

@ -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},

View File

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

View File

@ -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<Punishment[]> {
const cacheKey = 'punishments';