Simp-O-Matic

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

commit a968dcec3b69c96dcc2eee600334840c94513617
parent d26e9e5b514336fbefaf984f96499f0f2b928ec7
Author: Demonstrandum <moi@knutsen.co>
Date:   Tue, 10 Nov 2020 23:34:35 +0000

Attempt to use safe JSON.stringify.

Diffstat:
Mlib/commands/get.ts | 2+-
Mlib/commands/set.ts | 2+-
Mlib/extensions.ts | 20++++++++++++++++++++
Mlib/main.ts | 12++++++------
Mlib/utils.ts | 6+++---
Mlib/web.ts | 2+-
6 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/lib/commands/get.ts b/lib/commands/get.ts @@ -12,7 +12,7 @@ export default (home_scope: HomeScope) => { try { const accessors = args[0].trim().split('.').squeeze(); - const resolution = JSON.stringify( + const resolution = JSON.dump( recursive_regex_to_string( deep_copy(access(CONFIG, accessors))), null, 4); diff --git a/lib/commands/set.ts b/lib/commands/set.ts @@ -12,7 +12,7 @@ export default (home_scope: HomeScope) => { const parent = accessors.pop(); const obj = access(CONFIG, accessors); obj[parent] = JSON.parse(args.tail().join(' ')); - const normal = JSON.stringify(obj[parent], null, 4); + const normal = JSON.dump(obj[parent], null, 4); message.channel.send(`Assignment successful. \`${args[0].trim()} = ${normal}\``.squeeze()); diff --git a/lib/extensions.ts b/lib/extensions.ts @@ -141,6 +141,26 @@ declare global { to_abbrev(figures): string; truncate(): number; } + + interface JSON { + dump(object: any, + replacer?: (key: string, value: any) => any, + space?: string | number): string; + } +} + +// JSON Extensions: +JSON.dump = function (object, replacer=null, space=4) { + const DEFAULT_REPLACER = replacer = (key, value) => { + if (key === 'vc') return null; + return value; + }; + + if (replacer === null) replacer = DEFAULT_REPLACER; + else replacer = (key, value) => + replacer(key, DEFAULT_REPLACER(key, value)); + + return JSON.stringify(object, replacer, space); } // Array Extensions: diff --git a/lib/main.ts b/lib/main.ts @@ -242,7 +242,7 @@ export class SimpOMatic { } else { // For now, this won't be silent. system_message(client, "Received unknown data:\n```json\n" - + JSON.stringify(body, null, 4).slice(0, 1930) + + JSON.dump(body, null, 4).slice(0, 1930) + "```"); } }); @@ -408,7 +408,7 @@ Would you like to slow down a little?`.squeeze()); }); break; } case 'ls': { - const dirs = JSON.stringify({ + const dirs = JSON.dump({ '__dirname': __dirname, 'process.cwd()': process.cwd() }); @@ -625,11 +625,11 @@ function on_termination(error_type, e?: Error) { `Bot got \`${error_type}\` signal.\n` + `**Shutting down...**`); - write_file( - `${process.cwd()}/export-exit.json`, - export_config(GLOBAL_CONFIG, {})); + const exported = export_config(GLOBAL_CONFIG, {}); - pastebin_update(export_config(GLOBAL_CONFIG, {})) + write_file(`${process.cwd()}/export-exit.json`, exported); + + pastebin_update(exported) .then(_ => { console.log('Finished pastebin update.'); system_message(CLIENT, `Current configuration saved.`); diff --git a/lib/utils.ts b/lib/utils.ts @@ -100,7 +100,7 @@ export const recursive_regex_to_string = o => { } if (type(o) === 'object' || type(o) === 'array') { for (const key in o) - if (o.hasOwnProperty(key)) + if (o.hasOwnProperty(key) && o != 'vc') o[key] = recursive_regex_to_string(o[key]); return o; } @@ -116,11 +116,11 @@ export const export_config = (obj: Types.GlobalConfig, { ugly = false }) => { if (obj.guilds.hasOwnProperty(guild)) ['respond', 'reject', 'replace', 'trigger'] .each(name => o.guilds[guild].rules[name] = o.guilds[guild].rules[name] - .map(JSON.stringify) + .map(JSON.dump) .unique() .map(JSON.parse)); - return JSON.stringify(o, null, ugly ? null : 4); + return JSON.dump(o, null, ugly ? null : 4); }; export const pastebin_pull = (global_conf: Types.GlobalConfig) => diff --git a/lib/web.ts b/lib/web.ts @@ -121,7 +121,7 @@ export default (GLOBAL_CONFIG : Types.GlobalConfig, handle_post) => { res.writeHead(200, { 'Content-Type': 'application/json' }); - res.write(JSON.stringify(GLOBAL_CONFIG, null, 4)); + res.write(JSON.dump(GLOBAL_CONFIG, null, 4)); res.end(); break; default: