Skip to content

Commit

Permalink
Add chrono-node for natural language time parsing and update reminder…
Browse files Browse the repository at this point in the history
… command with examples
  • Loading branch information
chimpdev committed Sep 29, 2024
1 parent 06a3632 commit f4b313a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"async-lock": "^1.4.1",
"axios": "^1.6.1",
"body-parser": "^1.20.2",
"chrono-node": "^2.7.7",
"cloudflare": "^3.2.0",
"colord": "^2.9.3",
"colorette": "^2.0.20",
Expand Down
16 changes: 16 additions & 0 deletions server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/src/bot/commands/reminder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
.setDescription('reminder')
.addSubcommand(subcommand => subcommand.setName('create').setDescription('Create a reminder about something.')
.addStringOption(option => option.setName('about').setDescription('What is the reminder about?').setRequired(true))
.addStringOption(option => option.setName('when').setDescription('When should the reminder be sent?').setRequired(true)))
.addStringOption(option => option.setName('when').setDescription('Examples: tomorrow, in 9 hours, next week, next Friday at 3pm').setRequired(true)))
.addSubcommand(subcommand => subcommand.setName('delete').setDescription('Delete a reminder.')
.addStringOption(option => option.setName('reminder').setDescription('Select the reminder to delete.').setRequired(true).setAutocomplete(true))),
execute: async interaction => {
Expand All @@ -23,7 +23,7 @@ module.exports = {
if (about.length > 512) return interaction.reply({ content: 'The reminder description must be 512 characters or less.', ephemeral: true });

var reminderTime = parseTimeDuration(when);
if (!reminderTime) return interaction.reply({ content: 'Invalid time duration. You can use `1d`, `1h`, `1m`, `1s` for days, hours, minutes, and seconds respectively.', ephemeral: true });
if (!reminderTime) return interaction.reply({ content: 'Invalid time duration. Examples: `tomorrow`, `in 9 hours`, `next week`, `next Friday at 3pm`', ephemeral: true });
if (reminderTime < (60000 * 5)) return interaction.reply({ content: 'The reminder time must be at least 5 minutes.', ephemeral: true });

if (!interaction.deferred && !interaction.replied) await interaction.deferReply({ ephemeral: true });
Expand Down
31 changes: 3 additions & 28 deletions server/src/utils/parseTimeDuration.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
function parseTimeDuration(duration) {
const regex = /(\d+)\s*(s|sec|second|m|min|minute|h|hour|d|day|w|week)/;
const match = duration.match(regex);
if (!match) return null;
const chrono = require('chrono-node/en');

const value = parseInt(match[1]);
const unit = match[2].toLowerCase();
switch (unit) {
case 's':
case 'sec':
case 'second':
return value * 1000;
case 'm':
case 'min':
case 'minute':
return value * 60 * 1000;
case 'h':
case 'hour':
return value * 60 * 60 * 1000;
case 'd':
case 'day':
return value * 24 * 60 * 60 * 1000;
case 'w':
case 'week':
return value * 7 * 24 * 60 * 60 * 1000;
default:
return null;
}
function parseTimeDuration(duration) {
return chrono.parseDate(duration);
}


module.exports = parseTimeDuration;

0 comments on commit f4b313a

Please sign in to comment.