aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/kick.ts
blob: dcd19debac07f0133c27939c38910ba9c8be6524 (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
import { BotCommand } from '../../lib/extensions/BotCommand';
import { BotMessage } from '../../lib/extensions/BotMessage';
import { Modlog, ModlogType } from '../../lib/models';
import { GuildMember } from 'discord.js';

export default class PrefixCommand extends BotCommand {
	constructor() {
		super('kick', {
			aliases: ['kick'],
			args: [
				{
					id: 'user',
					type: 'member',
					prompt: {
						start: 'What user would you like to kick?',
						retry: 'Invalid response. What user would you like to kick?'
					}
				},
				{
					id: 'reason'
				}
			],
			clientPermissions: ['KICK_MEMBERS'],
			userPermissions: ['KICK_MEMBERS']
		});
	}
	async exec(
		message: BotMessage,
		{ user, reason }: { user: GuildMember; reason?: string }
	): Promise<void> {
		let modlogEnry: Modlog;
		try {
			modlogEnry = Modlog.build({
				user: user.id,
				guild: message.guild.id,
				moderator: message.author.id,
				type: ModlogType.KICK,
				reason
			});
			await modlogEnry.save();
		} catch (e) {
			console.error(e);
			await message.util.send(
				'Error saving to database. Please report this to a developer.'
			);
			return;
		}
		try {
			await user.send(
				`You were kicked in ${message.guild.name} with reason \`${
					reason || 'No reason given'
				}\``
			);
		} catch (e) {
			await message.channel.send('Error sending message to user');
		}
		try {
			await user.kick(
				`Kicked by ${message.author.tag} with ${
					reason ? `reason ${reason}` : 'no reason'
				}`
			);
		} catch {
			await message.util.send('Error kicking :/');
			await modlogEnry.destroy();
			return;
		}
		await message.util.send(
			`Kicked <@!${user.id}> with reason \`${reason || 'No reason given'}\``
		);
	}
}