aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/modlog.ts
blob: 6840f4eb58c3ada62eb97438089983662a9944a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { BushCommand, BushMessage, BushSlashMessage, BushUser, ButtonPaginator, ModLog } from '@lib';
import { MessageEmbed, User } from 'discord.js';

export default class ModlogCommand extends BushCommand {
	public constructor() {
		super('modlog', {
			aliases: ['modlog', 'modlogs'],
			category: 'moderation',
			description: {
				content: "View a user's modlogs, or view a specific case.",
				usage: 'modlogs <search> [--hidden]',
				examples: ['modlogs @Tyman']
			},
			args: [
				{
					id: 'search',
					customType: util.arg.union('user', 'string'),
					prompt: {
						start: 'What case id or user would you like to see?',
						retry: '{error} Choose a valid case id or user.'
					}
				},
				{
					id: 'hidden',
					match: 'flag',
					flag: ['--hidden', '-h'],
					default: false
				}
			],
			clientPermissions: (m) => util.clientSendAndPermCheck(m),
			userPermissions: (m) => util.userGuildPermCheck(m, ['MANAGE_MESSAGES']),
			slash: true,
			slashOptions: [
				{
					name: 'search',
					description: 'What case id or user would you like to see?',
					type: 'STRING',
					required: true
				},
				{
					name: 'hidden',
					description: 'Would you like to see hidden modlogs?',
					type: 'BOOLEAN',
					required: false
				}
			]
		});
	}

	#generateModlogInfo(log: ModLog, showUser: boolean): string {
		const trim = (str: string): string => (str.endsWith('\n') ? str.substring(0, str.length - 1).trim() : str.trim());
		const modLog = [`**Case ID**: ${util.discord.escapeMarkdown(log.id)}`, `**Type**: ${log.type.toLowerCase()}`];
		if (showUser) modLog.push(`**User**: <@!${log.user}>`);
		modLog.push(`**Moderator**: <@!${log.moderator}>`);
		if (log.duration) modLog.push(`**Duration**: ${util.humanizeDuration(log.duration)}`);
		modLog.push(`**Reason**: ${trim(log.reason ?? 'No Reason Specified.')}`);
		modLog.push(`**Date**: ${util.timestamp(log.createdAt)}`);
		if (log.evidence) modLog.push(`**Evidence:** ${trim(log.evidence)}`);
		return modLog.join(`\n`);
	}

	public override async exec(
		message: BushMessage | BushSlashMessage,
		{ search, hidden }: { search: BushUser | string; hidden: boolean }
	): Promise<unknown> {
		const foundUser = search instanceof User ? search : await util.resolveUserAsync(search);
		if (foundUser) {
			const logs = await ModLog.findAll({
				where: {
					guild: message.guild!.id,
					user: foundUser.id
				},
				order: [['createdAt', 'ASC']]
			});
			const niceLogs = logs
				.filter((log) => !log.pseudo)
				.filter((log) => !(log.hidden && hidden))
				.map((log) => this.#generateModlogInfo(log, false));
			if (!logs.length || !niceLogs.length)
				return message.util.reply(`${util.emojis.error} **${foundUser.tag}** does not have any modlogs.`);
			const chunked: string[][] = util.chunk(niceLogs, 4);
			const embedPages = chunked.map(
				(chunk) =>
					new MessageEmbed({
						title: `${foundUser.tag}'s Mod Logs`,
						description: chunk.join('\n━━━━━━━━━━━━━━━\n'),
						color: util.colors.default
					})
			);
			return await ButtonPaginator.send(message, embedPages, undefined, true);
		} else if (search) {
			const entry = await ModLog.findByPk(search as string);
			if (!entry || entry.pseudo || (entry.hidden && !hidden))
				return message.util.send(`${util.emojis.error} That modlog does not exist.`);
			if (entry.guild !== message.guild!.id)
				return message.util.reply(`${util.emojis.error} This modlog is from another server.`);
			const embed = {
				title: `Case ${entry.id}`,
				description: this.#generateModlogInfo(entry, true),
				color: util.colors.default
			};
			return await ButtonPaginator.send(message, [embed]);
		}
	}
}