aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/remind.ts
blob: 498a63dd834b332a8ff67223a3c53a9c47472123 (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
import {
	BotCommand,
	castDurationContent,
	clientSendAndPermCheck,
	dateDelta,
	emojis,
	format,
	Reminder,
	Time,
	timestamp,
	type CommandMessage,
	type OptArgType,
	type SlashMessage
} from '#lib';
import { ApplicationCommandOptionType } from 'discord.js';

export default class RemindCommand extends BotCommand {
	public constructor() {
		super('remind', {
			aliases: ['remind', 'remindme', 'reminder'],
			category: 'utilities',
			description: 'Create reminders that will be DMed to you when the time expires.',
			usage: ['remind <duration> <reminder>'],
			examples: ['template 1 2'],
			args: [
				{
					id: 'reminder',
					type: 'contentWithDuration',
					match: 'rest',
					description: 'The reason to be reminded and the duration to remind the user in.',
					prompt: 'What would you like to be reminded about and when?',
					retry: '{error} Choose a reason to be reminded about with a duration for when to be notified.',
					optional: true,
					slashType: ApplicationCommandOptionType.String
				}
			],
			slash: true,
			clientPermissions: (m) => clientSendAndPermCheck(m),
			userPermissions: []
		});
	}

	public override async exec(
		message: CommandMessage | SlashMessage,
		args: { reminder: OptArgType<'contentWithDuration'> | string }
	) {
		const { duration, content } = await castDurationContent(args.reminder, message);

		if (!content.trim()) return await message.util.reply(`${emojis.error} Please enter a reason to be reminded about.`);
		if (!duration) return await message.util.reply(`${emojis.error} Please enter a time to remind you in.`);

		if (duration < Time.Second * 30)
			return await message.util.reply(`${emojis.error} You cannot be reminded in less than 30 seconds.`);

		const expires = new Date(Date.now() + duration);

		const success = await Reminder.create({
			content: content.trim(),
			messageUrl: message.url!,
			user: message.author.id,
			created: new Date(),
			expires: expires
		}).catch(() => false);

		if (!success) return await message.util.reply(`${emojis.error} Could not create a reminder.`);

		// This isn't technically accurate, but it prevents it from being .99 seconds
		const delta = format.bold(dateDelta(new Date(Date.now() + duration)));
		return await message.util.reply(`${emojis.success} I will remind you in ${delta} (${timestamp(expires, 'T')}).`);
	}
}