Simp-O-Matic

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

commit f9b691e67925539420c16f3cc4fc4d8aec7d4d57
parent 76ca9b132550080e3083aba25c432d5d2c2666b9
Author: Demonstrandum <moi@knutsen.co>
Date:   Thu, 19 Mar 2020 01:21:36 +0000

Ignore deleted messages...

Diffstat:
Mlib/commands/reject.ts | 25++++++++++++++-----------
Mlib/main.ts | 24++++++++++++++++--------
2 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/lib/commands/reject.ts b/lib/commands/reject.ts @@ -23,11 +23,15 @@ export default home_scope => { + ' as to which rule to remove.') const index = Number(match[1]) - 1; - delete CONFIG.rules.reject[index]; + if (index >= reject.length) + return message(`Cannot delete rule at index ${index + 1}...` + + ` There are only ${reject.length} rejection rules.`); + + message.answer(`Rule matching \`${reject[index].match}\`` + + ` at index location #${index + 1} has been deleted.`); - message.answer(`Rule matching ${reject[index].match}` - + ` at index location no. ${index + 1} has been deleted.`); - } else if (args.length >= 2) { + delete CONFIG.rules.reject[index]; + } else if (args.length >= 1) { // Add a rule. let regex, options, response; // Eat up the regex/word... @@ -51,15 +55,14 @@ export default home_scope => { const after = phrase.slice(i).trim(); [options, response] = after .replace(/^([a-z]+)(.*)/, '$1-@@@-$2') - .split('-@@@-').map(String.prototype.trim); + .split('-@@@-').map(s => s.trim()); } else { // Were looking at a single word. // If no regex is given to match, we'll instead match a word // such that it will have to be matched on its own, not // surrounded by other letters or numbers, OR, it may exits // at the begging or end of the line. - regex = new RegExp( - `(^|[^\\p{L}\\p{N}])+${args[0]}?([^\\p{L}\\p{N}]|$)+`, - 'ui'); + regex = `(^|[^\\p{L}\\p{N}])+${args[0]}?([^\\p{L}\\p{N}]|$)+`, + options = 'ui'; response = args.tail().join(' ').trim(); } // Add the rule to the CONFIG.rules. @@ -69,9 +72,9 @@ export default home_scope => { : new RegExp(regex), response: response.length ? response : null }); - message.channel.send(`Rule with regular expression matching:` - + `/${regex}/${options}`.format('\n```\n') - + `has been added to list of rejection rules.`); + message.channel.send(`Rule with regular expression matching:\n` + + `/${regex}/${options}`.format('```') + + `\nhas been added to the list of rejection rules.`); } else { message.answer('Insufficient or nonsensical arguments provided.'); message.reply(`Here's how you use the command:\n` diff --git a/lib/main.ts b/lib/main.ts @@ -38,12 +38,13 @@ const CONFIG = deep_merge( JSON.parse(read_file('./bot.json', 'utf-8'))); // CONFIG will eventually update to the online version. -pastebin_latest().then(res => - deep_merge(CONFIG, res)).catch(console.log); +pastebin_latest().then(res => { + deep_merge(CONFIG, res); -// Precompile all regular-expressions in known places. -['respond', 'reject', 'replace'] - .each(name => CONFIG.rules[name].mut_map(compile_match)) + // Precompile all regular-expressions in known places. + ['respond', 'reject', 'replace'] + .each(name => CONFIG.rules[name].mut_map(compile_match)) +}).catch(console.log); // Store secrets in an object, retrieved from shell's // environment variables. @@ -525,6 +526,7 @@ export class SimpOMatic { process_generic(message : Message) { const { content } = message; + if (!content) return; // Message with no content (deleted)... for (const responder of CONFIG.rules.respond) { const match = content.match(responder.match); const { response } = responder; @@ -535,7 +537,10 @@ export class SimpOMatic { const { response } = rejecter; if (match) { if (response) message.answer(response); - if (message.deletable) message.delete(); + if (message.deletable) { + message.delete(); + break; + } } } } @@ -562,9 +567,9 @@ export class SimpOMatic { return Promise.resolve(command.content); } - let filter = _ => true; + let filter = m => m.content; if (opts.mention) - filter = m => m.author.toString() === opts.mentioning; + filter = m => m.content && m.author.toString() === opts.mentioning; const messages = await channel.fetchMessages({ limit: CONFIG.commands.max_history @@ -618,6 +623,9 @@ export class SimpOMatic { @On("message") async on_message(message : Message, client : Client) { + // Ignore empty messages... + if (!message.content) return; + console.log('Message acknowledged.'); if (SimpOMatic._client.user.id === message.author.id) { return;