aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/warn.ts
blob: e8b140168bb609c6caeebf46609efd513a96521c (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
import { GuildMember, Message } from 'discord.js';
import { BushCommand } from '../../lib/extensions/BushCommand';
import { Guild, Modlog, ModlogType } from '../../lib/models';

export default class WarnCommand extends BushCommand {
	public constructor() {
		super('warn', {
			aliases: ['warn'],
			userPermissions: ['MANAGE_MESSAGES'],
			args: [
				{
					id: 'member',
					type: 'member'
				},
				{
					id: 'reason',
					match: 'rest'
				}
			],
			description: {
				content: 'Warn a member and log it in modlogs',
				usage: 'warn <member> <reason>',
				examples: ['warn @Tyman being cool']
			}
		});
	}
	public async exec(
		message: Message,
		{ member, reason }: { member: GuildMember; reason: string }
	): Promise<void> {
		// 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 entry = Modlog.build({
				user: member.id,
				guild: message.guild.id,
				moderator: message.author.id,
				type: ModlogType.WARN,
				reason
			});
			await entry.save();
		} catch (e) {
			await message.util.send(
				'Error saving to database, please contact the developers'
			);
			return;
		}
		try {
			await member.send(
				`You were warned in ${message.guild.name} for reason "${reason}".`
			);
		} catch (e) {
			await message.util.send('Error messaging user, warning still saved.');
			return;
		}
		await message.util.send(
			`${member.user.tag} was warned for reason "${reason}".`
		);
	}
}