1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-09-30 00:50:59 -04:00
Daggerbot-TS/src/commands/case.ts

97 lines
6.6 KiB
TypeScript
Raw Normal View History

2023-08-30 04:34:59 -04:00
import Discord from 'discord.js';
2023-04-14 06:47:58 -04:00
import TClient from '../client.js';
2023-12-24 10:21:40 -05:00
import Formatters from '../helpers/Formatters.js';
2023-10-06 01:54:27 -04:00
import MessageTool from '../helpers/MessageTool.js';
2023-12-24 10:21:40 -05:00
export default class Case {
private static async updateEntry(client:TClient, caseId:number, reason:string) {
const logsArray = [client.config.dcServer.channels.logs, client.config.dcServer.channels.bankick_log];
for (const channelID of logsArray) {
const channel = await client.channels.fetch(channelID) as Discord.TextChannel;
if (channel && channel.type === Discord.ChannelType.GuildText) {
const messages = await channel.messages.fetch({limit: 3});
messages.forEach(async message=>{
if (message?.embeds[0]?.title.match(new RegExp(`Case #${caseId}`))) {
const findIndex = message?.embeds[0].fields.findIndex(x=>x.name === 'Reason');
await message.edit({embeds: [new client.embed(message.embeds[0]).spliceFields(findIndex, 1, {name: 'Reason', value: `\`${reason}\``})]});
}
})
}
}
}
static run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
2023-10-06 01:54:27 -04:00
if (!MessageTool.isStaff(interaction.member)) return MessageTool.youNeedRole(interaction, 'dcmod');
2023-02-24 19:55:11 -05:00
const caseId = interaction.options.getInteger('id');
({
2023-03-05 05:04:10 -05:00
update: async()=>{
const reason = interaction.options.getString('reason');
2023-12-24 10:21:40 -05:00
await client.punishments.updateReason(caseId, reason);
if (client.punishments.findCase(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.`)]});
2023-03-05 05:04:10 -05:00
},
view: async()=>{
2023-12-24 10:21:40 -05:00
const punishment = await client.punishments.findCase(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 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: `${MessageTool.formatMention(punishment.dataValues.member, 'user')} \`${punishment.dataValues.member}\``, inline: true},
{name: 'Moderator', value: `${MessageTool.formatMention(punishment.dataValues.moderator, 'user')} \`${punishment.dataValues.moderator}\``, inline: true},
2023-03-05 05:04:10 -05:00
{name: '\u200b', value: '\u200b', inline: true},
2023-12-24 10:21:40 -05:00
{name: 'Reason', value: `\`${punishment.reason || 'Reason unspecified'}\``, inline: true})
if (punishment.dataValues.duration) embed.addFields({name: 'Duration', value: `${Formatters.timeFormat(punishment.dataValues.duration, 100)}`})
if (punishment.dataValues.expired) embed.addFields({name: 'Expired', value: `This case has been overwritten by Case #${cancelledBy.dataValues.case_id} for reason \`${cancelledBy.dataValues.reason}\``})
if (punishment.dataValues.cancels) embed.addFields({name: 'Overwrites', value: `This case overwrites Case #${cancels.dataValues.case_id} with reason \`${cancels.dataValues.reason}\``})
2023-03-05 05:04:10 -05:00
interaction.reply({embeds: [embed]});
},
member: async()=>{
const user = (interaction.options.getUser('user') as Discord.User);
if (user.bot) return interaction.reply(`**${user.username}**'s punishment history cannot be viewed as they are a bot.`)
2023-12-24 10:21:40 -05:00
const punishments = await client.punishments.getAllCases();
const userPunishmentData = punishments.filter(x=>x.dataValues.member === user.id);
const userPunishment = userPunishmentData.sort((a,b)=>a.dataValues.time-b.dataValues.time).map(punishment=>{
2023-03-05 05:04:10 -05:00
return {
2023-12-24 10:21:40 -05:00
name: `${punishment.dataValues.type[0].toUpperCase()+punishment.dataValues.type.slice(1)} | Case #${punishment.dataValues.case_id}`,
value: `Reason: \`${punishment.dataValues.reason}\`\n${punishment.dataValues.duration ? `Duration: ${Formatters.timeFormat(punishment.dataValues.duration, 3)}\n` : ''}Moderator: ${MessageTool.formatMention(punishment.dataValues.moderator, 'user')}${punishment.dataValues.expired ? `\nOverwritten by Case #${punishments.find(x=>x.dataValues.cancels===punishment.dataValues.case_id)?.case_id}` : ''}${punishment.dataValues.cancels ? `\nOverwrites Case #${punishment.dataValues.cancels}` : ''}`
2023-03-05 05:04:10 -05:00
}
});
if (!punishments || !userPunishment) return interaction.reply(`**${user.username}** has a clean record.`)
2023-03-05 05:04:10 -05:00
const pageNum = interaction.options.getInteger('page') ?? 1;
return interaction.reply({embeds: [new client.embed().setColor(client.config.embedColor).setTitle(`${user.username}'s punishment history`).setDescription(`**ID:** \`${user.id}\``).setFooter({text: `${userPunishment.length} total punishments. Viewing page ${pageNum} out of ${Math.ceil(userPunishment.length/6)}.`}).addFields(userPunishment.slice((pageNum - 1) * 6, pageNum * 6))]});
}
2023-02-24 19:55:11 -05:00
} as any)[interaction.options.getSubcommand()]();
2023-12-24 10:21:40 -05:00
}
static data = new Discord.SlashCommandBuilder()
.setName('case')
.setDescription('Retrieve case information or user\'s punishment history')
2023-05-23 01:14:17 -04:00
.addSubcommand(x=>x
2023-03-05 05:04:10 -05:00
.setName('view')
2023-12-24 10:21:40 -05:00
.setDescription('View information of the case ID')
2023-05-23 01:14:17 -04:00
.addIntegerOption(x=>x
2023-03-05 05:04:10 -05:00
.setName('id')
.setDescription('Case ID')
2023-03-05 05:04:10 -05:00
.setRequired(true)))
2023-05-23 01:14:17 -04:00
.addSubcommand(x=>x
2023-03-05 05:04:10 -05:00
.setName('member')
.setDescription('View member\'s punishment history')
2023-05-23 01:14:17 -04:00
.addUserOption(x=>x
2023-03-05 05:04:10 -05:00
.setName('user')
.setDescription('Which user do you want to view their punishment history?')
.setRequired(true))
2023-05-23 01:14:17 -04:00
.addIntegerOption(x=>x
2023-03-05 05:04:10 -05:00
.setName('page')
.setDescription('Select the page number')))
2023-05-23 01:14:17 -04:00
.addSubcommand(x=>x
2023-03-05 05:04:10 -05:00
.setName('update')
.setDescription('Update the case with new reason')
2023-05-23 01:14:17 -04:00
.addIntegerOption(x=>x
2023-03-05 05:04:10 -05:00
.setName('id')
.setDescription('Case ID to be updated')
2023-03-05 05:04:10 -05:00
.setRequired(true))
2023-05-23 01:14:17 -04:00
.addStringOption(x=>x
2023-03-05 05:04:10 -05:00
.setName('reason')
.setDescription('New reason for the case')
.setRequired(true)))
2023-12-24 10:21:40 -05:00
}