Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit d31a5dc69df5d3c8102f24f901c1573695c28c2d
parent e7853ebd402fe51926faa7a628642b2d076459c4
Author: Bruno <b-coimbra@hotmail.com>
Date:   Thu, 26 Mar 2020 20:03:03 -0300

fixed annoying cron bug

Diffstat:
MHELP.md | 1+
Mlib/commands/cron.ts | 40+++++++++++++++++++++++++++++-----------
Mlib/main.ts | 38++++++++++++++++++++------------------
3 files changed, 50 insertions(+), 29 deletions(-)

diff --git a/HELP.md b/HELP.md @@ -65,6 +65,7 @@ - `!cron` **〈not impl.〉** — Run commands repeatedly based on some timer (look-up cron syntax for more info): - `!cron [minute] [hour] [day-of-month] [month] [day-of-week] ![command] <...>` — runs a command (with or without arguments) repeatedly as specified by the schedule signature. - `!cron <ls>` — lists all active cron-jobs numerically. + - `!cron <clear>` — clears all executed cron-jobs. - `!cron rm #[job-index]` — removes a cron-job by index. - `!choose [comma-separated-values]` **〈not impl.〉** — Choose randomly from a list of items, separated by commas. - `!define [word]` — Looks a word up in the Oxford English Dictionary. diff --git a/lib/commands/cron.ts b/lib/commands/cron.ts @@ -36,8 +36,8 @@ const MATCHERS = { months: 'jan feb mar apr may jun jul aug sep oct nov dec' .split(' ').map(month => month.capitalize()), prefix: (x: string) => new RegExp("^\\" + x), - ordinals: /st|nd|rd|th/, - greenwich: /pm|am/ + ordinals: /(st|nd|rd|th)/, + greenwich: /(pm|am)/ }; const RESPONSES = { @@ -48,6 +48,7 @@ const RESPONSES = { schedule: ':warning: There is no schedule to execute the command on.' }, empty: ":warning: There are no cron jobs being executed.", + clear: "Cleared all executed cron jobs.", removed: (id: number) => `Removed cron job #${id.toString().format(FORMATS.bold)}.`, added: (cron: Cron) => @@ -57,7 +58,8 @@ const RESPONSES = { let result: string = ""; result += `#${cron.id} `.format(FORMATS.bold); - result += `${cron.command.name.shorten(20)}`.format(FORMATS.block); + result += `${cron.command.name} ${cron.command.args.join(' ')}` + .shorten(20).format(FORMATS.block); if (schedule?.hours && schedule?.minutes) { result += `: ${schedule.hours}:${schedule.minutes}`; @@ -85,6 +87,9 @@ const RESPONSES = { result += `${schedule.ordinal}`; } + if (cron?.executed_at) + return result.format(FORMATS.strikethrough); + return result; } }; @@ -96,6 +101,14 @@ export class Timer { this.homescope = homescope; } + get defaultDate() { + const now = new Date(); + const default_values = { + month: now.getMonth() - 1 + }; + return default_values; + } + compare(job: Cron): void { const current = new Date(); current.setDate(current.getDate()); @@ -123,15 +136,16 @@ export class Timer { if (job.executed_at === timespan) return; - console.log('Executed cron job #' + job.id); + console.log('Executed cron job #', job.id); this.homescope.message.content = - `${this.homescope.CONFIG.commands.prefix} ${job.command.name} ${job.command.args.join(' ')}`; + `${this.homescope.CONFIG.commands.prefix}${job.command.name} ${job.command.args.join(' ')}`; job.executed_at = timespan; this.homescope.main.process_command( - this.homescope.message + this.homescope.message, + true ); } @@ -176,15 +190,16 @@ export default (home_scope: HomeScope) => { message.answer(RESPONSES.removed(job)); }; + const clear = () => { + crons = crons.filter(f => !f?.executed_at); + submit(); + message.answer(RESPONSES.clear); + }; + const list = () => { if (crons.length === 0) return message.answer(RESPONSES.empty); - console.log('list command:', crons - .filter(x => x !== null) - .map(x => RESPONSES.list(x)) - .join("\n")); - message.channel.send( crons .filter(x => x !== null) @@ -259,6 +274,9 @@ export default (home_scope: HomeScope) => { ? message.answer(RESPONSES.help.rm) : rm(job); } + else if (args[0] === 'clear') { + clear(); + } else { const cron: Cron = parse(args); diff --git a/lib/main.ts b/lib/main.ts @@ -201,7 +201,7 @@ export class SimpOMatic { return expanded; } - process_command(message : Message) { + process_command(message : Message, ignore_spam: boolean = false) { const CONFIG = GLOBAL_CONFIG.guilds[message.guild.id]; if (message.content.startsWith('..')) return; @@ -213,23 +213,25 @@ export class SimpOMatic { } const current_command = this._COMMAND_HISTORY.last(); - // Try and slow the fellas down a little. - if (!!last_command - && last_command.channel === current_command.channel - && last_command.author.id === current_command.author.id) { - // Only give spam warning if commands are coming - // fast _in the same channel_. - const delta = current_command.createdTimestamp - last_command.createdTimestamp; - if (last_command.content === current_command.content - && delta <= 1400) { - if (delta <= 400) return; - return message.answer(`I can't help but notice you're running \ - the same commands over in rather rapid succession. - Would you like to slow down a little?`.squeeze()); - } - if (delta <= 900) { - if (delta <= 300) return; - return message.answer('Slow down there bucko.'); + if (!ignore_spam) { + // Try and slow the fellas down a little. + if (!!last_command + && last_command.channel === current_command.channel + && last_command.author.id === current_command.author.id) { + // Only give spam warning if commands are coming + // fast _in the same channel_. + const delta = current_command.createdTimestamp - last_command.createdTimestamp; + if (last_command.content === current_command.content + && delta <= 1400) { + if (delta <= 400) return; + return message.answer(`I can't help but notice you're running \ +the same commands over in rather rapid succession. +Would you like to slow down a little?`.squeeze()); + } + if (delta <= 900) { + if (delta <= 300) return; + return message.answer('Slow down there bucko.'); + } } }