aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/massEvidence.ts
blob: 62f4825513dad81a1ebde0a6c530f969f34c33fd (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
106
107
108
import {
	BushCommand,
	clientSendAndPermCheck,
	colors,
	emojis,
	ModLog,
	overflowEmbed,
	regex,
	type ArgType,
	type CommandMessage,
	type OptArgType,
	type SlashMessage
} from '#lib';
import assert from 'assert/strict';
import { ApplicationCommandOptionType, PermissionFlagsBits } from 'discord.js';
import { EvidenceCommand } from '../index.js';

export default class MassEvidenceCommand extends BushCommand {
	public constructor() {
		super('massEvidence', {
			aliases: ['mass-evidence'],
			category: 'moderation',
			description: 'Add evidence to the last punishment of multiple users.',
			usage: ['mass-ban <...users> [--evidence "<evidence>"]'],
			examples: [
				'mass-evidence 311294982898057217 792202575851814942 792199864510447666 792201010118131713 --evidence "ironmoon said so"'
			],
			args: [
				{
					id: 'users',
					description: 'The ids of users to add evidence to each of their last punishment.',
					type: 'string',
					match: 'rest',
					prompt: 'What are the ids of all the users you would like add evidence to their last punishment?',
					retry: '{error} Choose a valid list of user ids to add evidence to their last punishment.',
					slashType: ApplicationCommandOptionType.String
				},
				{
					id: 'evidence',
					description: 'The evidence for the punishment.',
					flag: ['--evidence'],
					match: 'option',
					prompt: 'What is the evidence for the punishment?',
					retry: '{error} Provide valid evidence for the punishment.',
					optional: true,
					slashType: ApplicationCommandOptionType.String
				}
			],
			quoted: true,
			slash: true,
			channel: 'guild',
			clientPermissions: (m) => clientSendAndPermCheck(m),
			userPermissions: [PermissionFlagsBits.ManageMessages],
			lock: 'user'
		});
	}

	public override async exec(
		message: CommandMessage | SlashMessage,
		args: { users: ArgType<'string'>; evidence: OptArgType<'string'> }
	) {
		assert(message.inGuild());

		const evidence = EvidenceCommand.getEvidence(message, args.evidence);
		if (!evidence) return;

		const ids = args.users.split(/\n| /).filter((id) => id.length > 0);
		if (ids.length === 0) return message.util.send(`${emojis.error} You must provide at least one user id.`);
		for (const id of ids) {
			if (!regex.snowflake.test(id)) return message.util.send(`${emojis.error} ${id} is not a valid snowflake.`);
		}

		const caseMap = (
			await Promise.all(
				ids.map((id) =>
					ModLog.findOne({
						where: { guild: message.guild.id, user: id },
						order: [['createdAt', 'DESC']]
					}).catch(() => null)
				)
			)
		).map((c, i) => [ids[i], c] as const);

		const cases = caseMap.filter(([, c]) => c && !c.pseudo).map(([id, c]) => [id, c!] as const);

		const promises = cases.map(([, c]) => ((c.evidence = evidence), c.save()));

		const res = await Promise.all(promises);

		const lines = ids.map((id, i) => {
			const case_ = res[i];
			if (!case_) return `${emojis.error} ${id} - no case found.`;
			return `${emojis.success} ${id} - ${case_.id}`;
		});

		this.client.emit('massEvidence', message.member!, message.guild, evidence, lines);

		const embeds = overflowEmbed(
			{
				color: colors.DarkRed,
				title: 'Mass Evidence'
			},
			lines
		);

		return message.util.send({ embeds });
	}
}