Simp-O-Matic

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

commit 5727a2d7a02bc6a134fcabe63a5309ac9a980a1c
parent 66644cd66d097ad6246a870a70e5d4f2eb3a1e93
Author: Bruno <b-coimbra@hotmail.com>
Date:   Thu, 26 Mar 2020 16:04:41 -0300

Merge remote-tracking branch 'upstream/master'

Diffstat:
Mlib/commands/alias.ts | 3+++
Alib/commands/epoch.ts | 7+++++++
Mlib/main.ts | 36++++++++++++++++++++----------------
Mlib/utils.ts | 20++++++++++++++++++++
4 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/lib/commands/alias.ts b/lib/commands/alias.ts @@ -64,6 +64,9 @@ export default (home_scope: HomeScope) => { `\`${p}${args[0]}\` now maps to \`${p}${args.tail().join(' ')}\``); } else { if (args.length === 1) { + if (args[0][0] === CONFIG.commands.prefix) + args[0] = args[0].tail(); + if (args[0] in CONFIG.commands.aliases) { const aliases = Object.keys(CONFIG.commands.aliases); const n = aliases.indexOf(args[0]) + 1; diff --git a/lib/commands/epoch.ts b/lib/commands/epoch.ts @@ -0,0 +1,7 @@ +export default (home_scope: HomeScope) => { + const { message } = home_scope; + const now = new Date(); + message.channel.send(`Local time relative to bot: + ${now.toString()} / ${now.toISOString().format('`')} + ${now.valueOf().toString().format('`')}`.squeeze()); +}; diff --git a/lib/main.ts b/lib/main.ts @@ -17,7 +17,7 @@ import { execSync as shell } from 'child_process'; import './extensions'; import { deep_merge, pp, compile_match, export_config, access, glue_strings, - deep_copy, recursive_regex_to_string } from './utils'; + deep_copy, recursive_regex_to_string, pastebin_pull } from './utils'; // Default bot configuration for a Guild, JSON. import DEFAULT_GUILD_CONFIG from './default'; @@ -303,6 +303,16 @@ export class SimpOMatic { has been saved to the local file system. Pastebin file: ${pastebin_url}`.squeeze()); break; + } case 'refresh': { + message.reply('Pulling pastebin...'); + pastebin_pull(GLOBAL_CONFIG).then((res: Types.GlobalConfig) => { + GLOBAL_CONFIG = res; + message.reply('Dynamic configuration refresh succeded.'); + }).catch(e => { + message.reply('Could not update from pastebin:\n```' + + e.toString() + '```'); + }); + break; } case 'ls': { const dirs = JSON.stringify({ '__dirname': __dirname, @@ -480,9 +490,10 @@ export class SimpOMatic { let CLIENT: Client = null; -function on_termination(error_type) { +function on_termination(error_type, e?: Error) { // Back-up the resultant CONFIG to an external file. console.warn(`Received ${error_type}, shutting down.`); + if (e) console.warn(e); // Message all system channels. console.log('Sending system messages.'); system_message(CLIENT, @@ -510,6 +521,7 @@ function on_termination(error_type) { } // Handle exits. +const global_this = this; process .on('beforeExit', on_termination.bind(this, 'beforeExit')) .on('exit', on_termination.bind(this, 'exit')) @@ -517,22 +529,14 @@ process .on('SIGINT', on_termination.bind(this, 'SIGINT')) .on('SIGUSR1', on_termination.bind(this, 'SIGUSR1')) .on('SIGUSR2', on_termination.bind(this, 'SIGUSR2')) - .on('uncaughtException', on_termination.bind(this, 'exception')); + .on('uncaughtException', e => + on_termination.bind(global_this, 'exception', e)); -// GLOBAL_CONFIG will eventually update to the online version. -pastebin_latest().then(res => { - GLOBAL_CONFIG = deep_merge(GLOBAL_CONFIG, res); - // Remove any duplicates. - const gc_string = export_config(GLOBAL_CONFIG, { ugly: true }); - GLOBAL_CONFIG = JSON.parse(gc_string); - // Precompile all regular-expressions in known places. - for(const guild in GLOBAL_CONFIG.guilds) - if (GLOBAL_CONFIG.guilds.hasOwnProperty(guild)) - ['respond', 'reject', 'replace', 'trigger'] - .each(name => - GLOBAL_CONFIG.guilds[guild].rules[name] - .mut_map(compile_match)); +process.on('uncaughtException', (e) => e); +// GLOBAL_CONFIG will eventually update to the online version. +pastebin_pull(GLOBAL_CONFIG).then((res: Types.GlobalConfig) => { + GLOBAL_CONFIG = res; // Start The Simp'O'Matic. CLIENT = SimpOMatic.start(); }).catch(console.warn); diff --git a/lib/utils.ts b/lib/utils.ts @@ -2,6 +2,7 @@ import { inspect } from 'util'; import deep_clone from 'deepcopy'; import { HELP_SECTIONS, KNOWN_COMMANDS } from './main'; +import { pastebin_latest } from './api/pastebin'; import './extensions'; export const deep_copy = deep_clone; @@ -121,3 +122,22 @@ export const export_config = (obj: Types.GlobalConfig, { ugly = false }) => { return JSON.stringify(o, null, ugly ? null : 4); }; + +export const pastebin_pull = (global_conf: Types.GlobalConfig) => + new Promise((resolve, reject) => { + // GLOBAL_CONFIG will eventually update to the online version. + pastebin_latest().then(res => { + global_conf = deep_merge(global_conf, res); + // Remove any duplicates. + const gc_string = export_config(global_conf, { ugly: true }); + global_conf = JSON.parse(gc_string); + // Precompile all regular-expressions in known places. + for (const guild in global_conf.guilds) + if (global_conf.guilds.hasOwnProperty(guild)) + ['respond', 'reject', 'replace', 'trigger'] + .each(name => + global_conf.guilds[guild].rules[name] + .mut_map(compile_match)); + return resolve(global_conf); + }).catch(reject); + });