Simp-O-Matic

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

commit d694acc2c58f7e992d6a744d4f8e4be37e16ca59
parent 2be7df5ba89aaae4ce300fd1c3de72d9ed2f5e08
Author: Demonstrandum <moi@knutsen.co>
Date:   Wed, 18 Mar 2020 18:31:36 +0000

Deep alias expansions... again.

Diffstat:
ACODE_OF_CONDUCT.md | 1+
MHELP.md | 2+-
Alib/commands/8ball.ts | 38++++++++++++++++++++++++++++++++++++++
Mlib/commands/echo.ts | 6+++---
Alib/commands/flip.ts | 4++++
Alib/commands/pp.ts | 15+++++++++++++++
Mlib/default.ts | 6+++++-
Mlib/main.ts | 15++++++++++-----
8 files changed, 77 insertions(+), 10 deletions(-)

diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md @@ -0,0 +1 @@ +Nice CoC diff --git a/HELP.md b/HELP.md @@ -82,7 +82,7 @@ - `!cowsay <options> [phrase]` **〈not impl.〉** — Make a cow say something, using Unix-like command line arguments. - `!figlet <options> [phrase]` **〈not impl.〉** — Print text in ASCII format, using Unix-like command line arguments. - `!roll <upper-bound>` **〈not impl.〉** — Roll a dice, default upper bound is 6. -- `!8ball` **〈not impl.〉** — Ask a question, receive a response. +- `!8ball` — Ask a question, receive a response. - `!summon [@user-name]` **〈not impl.〉** — Summon someone to the server by making the bot poke them in their DMs about it. - `!mock [phrase]` **〈not impl.〉** — Say something, _bUt iN a MocKiNg WaY_... - `!boomer [phrase]` **〈not impl.〉** — Say something, but in the way your demented boomer uncle would write it on Facebook. diff --git a/lib/commands/8ball.ts b/lib/commands/8ball.ts @@ -0,0 +1,38 @@ +export default home_scope => { + const { message, args, HELP_SECTIONS, KNOWN_COMMANDS } = home_scope; + + if (args.length === 0 || args[0] == 'help') { + message.channel.send(HELP_SECTIONS[KNOWN_COMMANDS.indexOf('8ball')].trim()); + return; + } + + const responses: string[] = [ + "Perhaps.", + "Yep.", + "Nope.", + "For sure.", + "Maybe, maybe not.", + "As I see it, yes.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again.", + "Don’t count on it.", + "It is certain.", + "It is decidedly so.", + "Most likely.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Outlook good.", + "Reply hazy, try again.", + "Signs point to yes.", + "Very doubtful.", + "Without a doubt.", + "Yes.", + "Yes – definitely.", + "You may rely on it.", + ]; + + message.answer(":8ball:\s" + responses[Math.floor(Math.random() * responses.length)]); +}; diff --git a/lib/commands/echo.ts b/lib/commands/echo.ts @@ -1,4 +1,4 @@ export default home_scope => { - const { message, args } = home_scope; - message.channel.send(args.join(' ')); -}; + const { message } = home_scope; + message.channel.send(message.content); +} diff --git a/lib/commands/flip.ts b/lib/commands/flip.ts @@ -0,0 +1,4 @@ +export default home_scope => { + const { message } = home_scope; + message.answer(Math.random() < 0.5 ? 'Heads!' : 'Tails!'); +}; diff --git a/lib/commands/pp.ts b/lib/commands/pp.ts @@ -0,0 +1,15 @@ +export default home_scope => { + const { message, CONFIG } = home_scope; + + let user = message.author.id; + try { + user = message.mentions.users.first().id; + } finally { + const shaft = '='.repeat( + CONFIG.pp_sizes[user] + ? CONFIG.pp_sizes[user] + : CONFIG.pp_sizes[user] = Math.ceil(Math.random() * 16)); + + message.answer(`8${shaft}>`); + } +} diff --git a/lib/default.ts b/lib/default.ts @@ -8,6 +8,10 @@ export default { permissions: 8, lang: 'en', + pp_sizes: { + '541761315887120399': 16 + }, + weather_locations: { '541761315887120399': 'Moscow' }, @@ -60,7 +64,7 @@ export default { response: 'Obama.' }, { - match: "/(^|[^\\p{L}\\p{N}])+bot([^\\p{L}\\p{N}]|$)+/iu", + match: "/(^|[^\\p{L}\\p{N}])+bots?([^\\p{L}\\p{N}]|$)+/iu", response: "The hell you sayn' about bots?" } ], diff --git a/lib/main.ts b/lib/main.ts @@ -109,7 +109,7 @@ export class SimpOMatic { expand_alias(operator, args) { const expander = unexpanded => { let expanded = unexpanded; - if (CONFIG.operators.aliases.hasOwnProperty(unexpanded)) + if (CONFIG.commands.aliases.hasOwnProperty(unexpanded)) expanded = CONFIG.commands.aliases[unexpanded].trim().squeeze(); const expanded_command_words = expanded.split(' '); @@ -121,11 +121,14 @@ export class SimpOMatic { return expanded }; + // Continue expanding until we have no more change. - const expanded = expander(operator); - if (expanded === operator) - return operator; - return this.expand_alias(operator, args) + let expanded = expander(operator); + while (expanded !== operator) { + operator = expanded; + expanded = expander(operator); + } + return expanded; } process_command(message : Message) { @@ -163,6 +166,8 @@ export class SimpOMatic { const args = words.tail(); let operator = words[0].toLowerCase(); + // Expansion of aliases will expand aliases used within + // the alias definition too. Yay. operator = this.expand_alias(operator, args); operator = operator.toLowerCase();