Simp-O-Matic

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

commit f08d933f06add761da802631f4b6da38afde927a
parent 117940887cf2c5d4134aa1849f37c329f9bb5967
Author: Demonstrandum <moi@knutsen.co>
Date:   Fri, 20 Mar 2020 02:38:46 +0000

Format rules output.

Diffstat:
Mlib/default.ts | 2+-
Mlib/extensions.ts | 41+++++++++++++++++++++++++++--------------
Mlib/rule.ts | 30+++++++++++++++++++++---------
3 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/lib/default.ts b/lib/default.ts @@ -70,7 +70,7 @@ export default { ], reject: [ { - match: "/\\.{8,}/", + match: "/\\.{20,}/", response: "Too many dots..." }, ], diff --git a/lib/extensions.ts b/lib/extensions.ts @@ -37,8 +37,10 @@ declare global { tail(): string; first(): string; last(off? : number): string; - format(fmt: string): string; + format(fmt: string): string; emojify(): string; + shorten(width?: number): string; + lines(): string[]; } interface Number { @@ -109,25 +111,36 @@ String.prototype.first = Array.prototype.first as any; String.prototype.last = Array.prototype.last as any; export const FORMATS: TextFormat = { - italics: '*', - bold: '**', - bold_italics: '***', - underline: '__', - underline_italics: '--*', - underline_bold: '__**', - underline_bold_italics: '__***', - strikethrough: '~~', - block: '`', - code_block: '```', - block_quote: '>', - multiline_block_quote: '>>>', - hidden: '||', + italics: '*', + bold: '**', + bold_italics: '***', + underline: '__', + underline_italics: '--*', + underline_bold: '__**', + underline_bold_italics: '__***', + strikethrough: '~~', + block: '`', + code_block: '```', + block_quote: '>', + multiline_block_quote: '>>>', + hidden: '||', }; String.prototype.format = function (fmt: string) { return `${fmt}${this}${fmt}`; }; +String.prototype.shorten = function (width=40) { + if (this.length <= width) return String(this); + return this.slice(0, width - 3) + '...'; +}; + +String.prototype.lines = function () { + return this + .replace(/\n/g, '\n<-|LINE|->') + .split('<-|LINE|->'); +}; + // Number Extensions: Number.prototype.round_to = function (dp : number) { const exp = 10 ** dp; diff --git a/lib/rule.ts b/lib/rule.ts @@ -1,3 +1,5 @@ +import { glue_strings } from './utils'; + export const rule = (rule_kind: string) => home_scope => { const { message, args, CONFIG, KNOWN_COMMANDS, @@ -7,13 +9,17 @@ export const rule = (rule_kind: string) => home_scope => { if (args.length === 0 || args[0] === 'ls') { // Make a pretty list. let str = `**${rule_kind.capitalize()} Rules:**\n`; + if (rules_array.squeeze().length === 0) + str += "There are none."; + rules_array.each((entry, i) => { str += `${i + 1}. Matches: \`${entry.match}\``; if (entry.response) - str += `\n Responds with: ‘${entry.response}’`; + str += `\n Responds with: ‘${entry.response.shorten()}’`; str += '\n'; }); - message.channel.send(str); + for (const msg of glue_strings(str.lines())) + message.channel.send(msg); } else if (args[0] === 'rm') { // Remove a rule. const match = args[1].match(/#?(\d+)/); @@ -27,7 +33,7 @@ export const rule = (rule_kind: string) => home_scope => { + ` There are only ${rules_array.length} ${rule_kind} rules.`); message.answer(`Rule matching \`${rules_array[index].match}\`` - + ` at index location #${index + 1} has been deleted.`); + + ` at index location number ${index + 1} has been deleted.`); delete CONFIG.rules[rule_kind][index]; } else if (args.length >= 1) { @@ -70,12 +76,18 @@ export const rule = (rule_kind: string) => home_scope => { response = args.tail().join(' ').trim(); } // Add the rule to the CONFIG.rules. - CONFIG.rules[rule_kind].push({ - match: options.length - ? new RegExp(regex, options) - : new RegExp(regex), - response: response.length ? response : null - }); + try { + CONFIG.rules[rule_kind].push({ + match: options.length + ? new RegExp(regex, options) + : new RegExp(regex), + response: response.length ? response : null + }); + } catch (e) { + message.answer('**Error** creating regular expression!\n' + + e.message.toString().format('`')); + return; + } message.channel.send(`Rule with regular expression matching:\n` + `/${regex}/${options}`.format('```') + `\nhas been added to the list of ${rule_kind} rules.`);