|
| 1 | +import { SteveCommand } from '@lib/structures/commands/SteveCommand'; |
| 2 | +import { CommandStore, KlasaMessage, ScheduledTask, SettingsUpdateResult } from 'klasa'; |
| 3 | +import { Message, ColorResolvable } from 'discord.js'; |
| 4 | +import { UserSettings } from '@lib/types/settings/UserSettings'; |
| 5 | +import { friendlyDuration, newEmbed } from '@lib/util/util'; |
| 6 | +import { oneLine } from 'common-tags'; |
| 7 | +import { Colors } from '@lib/types/enums'; |
| 8 | + |
| 9 | +export default class extends SteveCommand { |
| 10 | + |
| 11 | + public constructor(store: CommandStore, file: string[], directory: string) { |
| 12 | + super(store, file, directory, { |
| 13 | + aliases: ['pomo', 'pom'], |
| 14 | + description: 'Be productive with the pomodoro technique!', |
| 15 | + examples: ['pomo start', 'pomo end', 'pomo check', 'pomo show', 'pomo set|work|25m', 'pomo set|short|5m', 'pomo set|long|15m'], |
| 16 | + extendedHelp: oneLine`This command helps faciliate use of the |
| 17 | + [pomodoro technique](https://en.wikipedia.org/wiki/Pomodoro_Technique). Note that if you change the length of a work cycle |
| 18 | + or break while that cycle is happening, the change will not take effect until the next time that cycle occurs.`, |
| 19 | + helpUsage: '*start*/*check*/*end*/*show*/*set* (segment|duration)', |
| 20 | + subcommands: true, |
| 21 | + usage: '<start|check|end|show|set> (segment:pomSegment) (duration:timespan)' |
| 22 | + }); |
| 23 | + |
| 24 | + this |
| 25 | + .createCustomResolver('pomSegment', (str, possible, msg, [action]) => { |
| 26 | + if (action !== 'set') return null; |
| 27 | + const beefsteak = ['work', 'long', 'short']; // thank allison for the variable name |
| 28 | + if (!beefsteak.includes(str)) throw `**${str}** is not a valid segment name.`; |
| 29 | + return str; |
| 30 | + }) |
| 31 | + .createCustomResolver('timespan', (str, possible, msg, [action]) => |
| 32 | + action === 'set' ? this.client.arguments.get('timespan').run(str, possible, msg) : null); |
| 33 | + } |
| 34 | + |
| 35 | + public async start(msg: KlasaMessage): Promise<[[ScheduledTask, SettingsUpdateResult, SettingsUpdateResult], SettingsUpdateResult, Message]> { |
| 36 | + if (msg.author.pomodoro.running) throw 'Your pomodoro timer is already running! Get back to work smh'; |
| 37 | + |
| 38 | + return Promise.all([ |
| 39 | + msg.author.pomodoro.startPomodoroTimer(), |
| 40 | + msg.author.pomodoro.incrementWorkRoundNumber(), |
| 41 | + msg.channel.send('Starting your pomodoro timer. You got this; get after it!')]); |
| 42 | + } |
| 43 | + |
| 44 | + public async check(msg: KlasaMessage): Promise<Message> { |
| 45 | + const task = msg.author.pomodoro.getPomodoroTask(); |
| 46 | + if (!task) throw 'You\'re not currently pomodoroing!'; |
| 47 | + |
| 48 | + return msg.channel.send(oneLine`You have ${friendlyDuration(task.time.getTime() - Date.now())} left in your |
| 49 | + ${msg.author.pomodoro.friendlyCurrentSegment}!`); |
| 50 | + } |
| 51 | + |
| 52 | + public async end(msg: KlasaMessage): Promise<Message> { |
| 53 | + if (!msg.author.pomodoro.running) throw 'You\'re not currently pomodoroing!'; |
| 54 | + await msg.author.pomodoro.reset(); |
| 55 | + return msg.channel.send('Your pomodoro timer has ended. Great job!'); |
| 56 | + } |
| 57 | + |
| 58 | + public async show(msg: KlasaMessage): Promise<Message> { |
| 59 | + const workLength = friendlyDuration(msg.author.pomodoro.workSegmentLength); |
| 60 | + const shortLength = friendlyDuration(msg.author.pomodoro.shortBreakSegmentLength); |
| 61 | + const longLength = friendlyDuration(msg.author.pomodoro.longBreakSegmentLength); |
| 62 | + |
| 63 | + const embed = newEmbed() |
| 64 | + .addFields( |
| 65 | + { name: 'Work Cycle', value: workLength, inline: true }, |
| 66 | + { name: 'Short Break', value: shortLength, inline: true }, |
| 67 | + { name: 'Long Break', value: longLength, inline: true } |
| 68 | + ) |
| 69 | + .setColor(msg.author.settings.get(UserSettings.EmbedColor) as ColorResolvable || Colors.YellowGreen) |
| 70 | + .setTitle(`Pomodoro Settings for ${msg.author.tag}`); |
| 71 | + |
| 72 | + return msg.channel.send(embed); |
| 73 | + } |
| 74 | + |
| 75 | + public async set(msg: KlasaMessage, [pomType, duration]: [string, number]): Promise<[SettingsUpdateResult, Message]> { |
| 76 | + let update: string; |
| 77 | + let friendlySegmentName: string; |
| 78 | + |
| 79 | + switch (pomType) { |
| 80 | + case 'work': |
| 81 | + update = UserSettings.Pomodoro.WorkTime; |
| 82 | + friendlySegmentName = 'work period'; |
| 83 | + break; |
| 84 | + case 'long': |
| 85 | + update = UserSettings.Pomodoro.LongBreakTime; |
| 86 | + friendlySegmentName = 'long break'; |
| 87 | + break; |
| 88 | + case 'short': |
| 89 | + update = UserSettings.Pomodoro.ShortBreakTime; |
| 90 | + friendlySegmentName = 'short break'; |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + return Promise.all([ |
| 95 | + msg.author.settings.update(update, duration), |
| 96 | + msg.channel.send(`You've updated your ${friendlySegmentName} length to ${friendlyDuration(duration)}.`) |
| 97 | + ]); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments