Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/commands/Productivity/setsnooze.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SteveCommand } from '@lib/structures/commands/SteveCommand';
import { GuildMessage } from '@lib/types/Messages';
import { UserSettings } from '@lib/types/settings/UserSettings';
import { ApplyOptions } from '@skyra/decorators';
import { friendlyDuration } from '@utils/util';
import { Message } from 'discord.js';
import { CommandOptions } from 'klasa';

@ApplyOptions<CommandOptions>({
description: lang => lang.tget('commandSetSnoozeDescription'),
usage: '<duration:timespan>'
})
export default class extends SteveCommand {

public async run(msg: GuildMessage, [duration]: [number]): Promise<Message> {
await msg.author.settings.update(UserSettings.SnoozeDuration, duration);

return msg.channel.send(msg.guild.language.tget('commandSetSnoozeSet', friendlyDuration(duration)));
}

}
36 changes: 36 additions & 0 deletions src/commands/Productivity/snooze.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { CommandOptions, KlasaMessage } from 'klasa';
import { Message } from 'discord.js';
import { ApplyOptions, CreateResolver } from '@skyra/decorators';
import { UserSettings } from '@lib/types/settings/UserSettings';
import { SteveCommand } from '@lib/structures/commands/SteveCommand';
import { friendlyDuration } from '@utils/util';

@ApplyOptions<CommandOptions>({
description: lang => lang.tget('commandSnoozeDescription'),
extendedHelp: lang => lang.tget('commandSnoozeExtended'),
usage: '(duration:timespan)'
})
@CreateResolver(
'timespan',
(str, possible, msg) => str
? msg.client.arguments.get('timespan').run(str, possible, msg)
: msg.author.settings.get(UserSettings.SnoozeDuration)
)
export default class extends SteveCommand {

public async run(msg: KlasaMessage, [duration]: [number]): Promise<Message> {
const reminder = this.client.schedule.getUserSnooze(msg.author.id);
if (!reminder) return msg.channel.send(msg.language.tget('commandSnoozeNoRemind'));

await this.client.schedule.createReminder(duration,
// @ts-expect-error 2339
reminder.data.userID ?? reminder.data.user, reminder.data.content,
// @ts-expect-error 2339
reminder.data.channelID ?? reminder.data.channel);

await reminder.delete();

return msg.channel.send(msg.language.tget('commandSnoozeCreated', reminder.data.content, friendlyDuration(duration)));
}

}
15 changes: 15 additions & 0 deletions src/extendables/Schedule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Time } from '@lib/types/Enums';
import { ApplyOptions } from '@skyra/decorators';
import { Extendable, ExtendableOptions, Schedule, ScheduledTask } from 'klasa';

Expand All @@ -21,6 +22,13 @@ export default class extends Extendable {
});
}

public createSnooze(this: Schedule, userID: string, content: string, channelID: string): Promise<Reminder> {
return this.create('snooze', Date.now() + (5 * Time.MINUTE), {
catchUp: true,
data: { userID, content, channelID }
});
}

public getUserReminders(this: Schedule, userID: string): Reminder[] {
const filter = (task: ScheduledTask) => {
const id = task.data.userID ?? task.data.user;
Expand All @@ -29,6 +37,13 @@ export default class extends Extendable {
return this.tasks.filter(filter);
}

public getUserSnooze(this: Schedule, userID: string): Reminder | undefined {
return this.tasks.filter((task: ScheduledTask) => {
const id = task.data.userID ?? task.data.user;
return task.taskName === 'snooze' && id === userID;
}).pop();
}

}

export type ModerationTask = 'unmute' | 'undeafen' | 'unban';
Expand Down
13 changes: 13 additions & 0 deletions src/languages/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,19 @@ export default class extends Language {
commandRemindViewEmbed: {
title: 'Pending Reminders'
},
commandSnoozeDescription: 'Repeat your last reminder.',
commandSnoozeExtended: builder.display('snooze', {
examples: [
'',
'5m'
],
reminder: 'You can only snooze reminders that happened in the past 5 minutes.',
extendedHelp: 'You can change your default snooze time with the `setsnooze` command.'
}),
commandSnoozeCreated: (content, duration) => `I'll remind you **${content}** in ${duration}.`,
commandSnoozeNoRemind: 'It looks like you haven\'t had any reminders go off in the past 5 minutes.',
commandSetSnoozeDescription: 'Set your default snooze duration.',
commandSetSnoozeSet: duration => `Your default snooze duration is now ${duration}.`,
/**
* ################################
* # POMODORO #
Expand Down
4 changes: 3 additions & 1 deletion src/lib/schemas/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Time } from '@lib/types/Enums';
import { Client } from 'klasa';

export default Client.defaultUserSchema
.add('embedColor', 'Color');
.add('embedColor', 'Color')
.add('snoozeDuration', 'Integer', { 'default': 30 * Time.MINUTE });
2 changes: 2 additions & 0 deletions src/lib/types/Augments.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ declare module 'klasa' {
interface Schedule {
createModerationTask(taskName: ModerationTask, duration: number, taskData: ModerationTaskData): Promise<ScheduledTask>;
createReminder(duration: number, userID: string, content: string, channelID: string): Promise<Reminder>;
createSnooze(userID: string, content: string, channelID: string): Promise<Reminder>;
getUserReminders(userID: string): Reminder[];
getUserSnooze(userID: string): Reminder;
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib/types/Languages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ declare module 'klasa' {
commandRemindViewEmbed: {
title: string;
};
commandSnoozeDescription: string;
commandSnoozeExtended: string;
commandSnoozeNoRemind: string;
commandSnoozeCreated: (content: string, duration: string) => string;
commandSetSnoozeDescription: string;
commandSetSnoozeSet: (duration: string) => string;
commandPomodoroDescription: string;
commandPomodoroExtended: string;
commandPomodoroUnderConstruction: string;
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/settings/UserSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
export namespace UserSettings {

export const EmbedColor = 'embedColor';
export const SnoozeDuration = 'snoozeDuration';

}
7 changes: 6 additions & 1 deletion src/tasks/reminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Task } from 'klasa';
import { Message } from 'discord.js';
import { OldReminderData, ReminderData } from '../extendables/Schedule';
import { floatPromise } from '@utils/util';

export default class extends Task {

Expand All @@ -11,7 +12,11 @@ export default class extends Task {
// @ts-expect-error 2339
const _user = await this.client.users.fetch(data.userID ?? data.user);

if (_channel && _channel.isText()) return _channel.send(`${_user}, here's the reminder you asked for: **${data.content}**`);
if (_channel && _channel.isText()) {
// @ts-expect-error 2339
floatPromise(this, this.client.schedule.createSnooze(data.userID ?? data.user, data.content, data.channelID ?? data.channel));
return _channel.send(`${_user}, here's the reminder you asked for: **${data.content}**`);
}
}

}
11 changes: 11 additions & 0 deletions src/tasks/snooze.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { Task } from 'klasa';
import { OldReminderData, ReminderData } from '../extendables/Schedule';

export default class extends Task {

public run(data: ReminderData | OldReminderData): ReminderData | OldReminderData {
return data;
}

}