aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/mute.ts
blob: eeb2d04600a26e041f17b68d971755d4bfb1cd95 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Argument } from 'discord-akairo';
import { ApplicationCommandOptionType } from 'discord-api-types';
import { CommandInteraction, Message, User } from 'discord.js';
import moment from 'moment';
import { BushCommand } from '../../lib/extensions/BushCommand';
import { Guild, ModLog, ModLogType, Mute } from '../../lib/models';

export default class MuteCommand extends BushCommand {
	constructor() {
		super('mute', {
			aliases: ['mute'],
			category: 'moderation',
			args: [
				{
					id: 'user',
					type: 'user',
					prompt: {
						start: 'What user would you like to mute?',
						retry: '{error} Choose a valid user to mute.'
					}
				},
				{
					id: 'reason',
					match: 'separate',
					prompt: {
						start: 'Why would you like to mute this user?',
						retry: '{error} Choose a mute reason.',
						optional: true
					}
				},
				{
					id: 'time',
					type: 'duration',
					match: 'option',
					flag: '--time'
				}
			],
			clientPermissions: ['MANAGE_ROLES'],
			userPermissions: ['MANAGE_MESSAGES'],
			description: {
				content: 'Mute a user.',
				usage: 'mute <member> <reason> [--time]',
				examples: ['mute @user bad boi --time 1h']
			},
			slashOptions: [
				{
					type: ApplicationCommandOptionType.USER,
					name: 'user',
					description: 'The user to mute.',
					required: true
				},
				{
					type: ApplicationCommandOptionType.STRING,
					name: 'reason',
					description: 'Why the user is getting muted.',
					required: false
				},
				{
					type: ApplicationCommandOptionType.STRING,
					name: 'time',
					description: 'How long the user should be muted for.',
					required: false
				}
			],
			slash: true
		});
	}
	async *genResponses(
		message: Message | CommandInteraction,
		user: User,
		reason?: string,
		time?: number
	): AsyncIterable<string> {
		const duration = moment.duration(time);
		let modlogEnry: ModLog;
		let muteEntry: Mute;
		// Create guild entry so postgres doesn't get mad when I try and add a modlog entry
		await Guild.findOrCreate({
			where: {
				id: message.guild.id
			},
			defaults: {
				id: message.guild.id
			}
		});
		try {
			const muteRole = (await Guild.findByPk(message.guild.id)).get('muteRole');
			try {
				if (time) {
					modlogEnry = ModLog.build({
						user: user.id,
						guild: message.guild.id,
						reason,
						type: ModLogType.TEMP_MUTE,
						duration: duration.asMilliseconds(),
						moderator: message instanceof CommandInteraction ? message.user.id : message.author.id
					});
					muteEntry = Mute.build({
						user: user.id,
						guild: message.guild.id,
						reason,
						expires: new Date(new Date().getTime() + duration.asMilliseconds()),
						modlog: modlogEnry.id
					});
				} else {
					modlogEnry = ModLog.build({
						user: user.id,
						guild: message.guild.id,
						reason,
						type: ModLogType.MUTE,
						moderator: message instanceof CommandInteraction ? message.user.id : message.author.id
					});
					muteEntry = Mute.build({
						user: user.id,
						guild: message.guild.id,
						reason,
						modlog: modlogEnry.id
					});
				}
				await modlogEnry.save();
				await muteEntry.save();
			} catch (e) {
				this.client.console.error(`MuteCommand`, `Error saving to database. ${e?.stack}`);
				yield `${this.client.util.emojis.error} Error saving to database. Please report this to a developer.`;
				return;
			}
			try {
				await user.send(
					`You were muted in ${message.guild.name} ${time ? `for ${duration.humanize()}` : 'permanently'} with reason \`${
						reason || 'No reason given'
					}\``
				);
			} catch (e) {
				yield `${this.client.util.emojis.warn} Unable to dm user`;
			}
			await (
				await message.guild.members.fetch(user)
			).roles.add(
				muteRole,
				`Muted by ${message instanceof CommandInteraction ? message.user.tag : message.author.tag} with ${
					reason ? `reason ${reason}` : 'no reason'
				}`
			);
			yield `${this.client.util.emojis.success} muted <@!${user.id}> ${
				time ? `for ${duration.humanize()}` : 'permanently'
			} with reason \`${reason || 'No reason given'}\``;
		} catch {
			yield `${this.client.util.emojis.error} Error muting :/`;
			await muteEntry.destroy();
			await modlogEnry.destroy();
			return;
		}
	}
	async exec(
		message: Message,
		{ user, reason, time }: { user: User; reason?: string[]; time?: string | number }
	): Promise<void> {
		this.client.console.debug(reason);

		if (typeof time === 'string') {
			time = (await Argument.cast('duration', this.client.commandHandler.resolver, message, time)) as number;
		}
		for await (const response of this.genResponses(message, user, reason.join(' '), time)) {
			await message.util.sendNew(response);
		}
	}
}