Simp-O-Matic

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

commit 025087c876267f27d93cb29360c64e579129bce1
parent 19fe771ffab58262acfa08e6535830f41b8f3886
Author: Demonstrandum <moi@knutsen.co>
Date:   Sun, 22 Mar 2020 22:21:48 +0000

Make prefixes in help output match servers chosen prefix

Diffstat:
Mlib/commands/8ball.ts | 7++++---
Mlib/commands/help.ts | 26++++++++++++++++++--------
Mlib/commands/ship.ts | 12+++++-------
Mlib/main.ts | 4++--
Mlib/rule.ts | 12+++++-------
Mlib/utils.ts | 14++++++++++++++
6 files changed, 48 insertions(+), 27 deletions(-)

diff --git a/lib/commands/8ball.ts b/lib/commands/8ball.ts @@ -1,9 +1,10 @@ +import { prefix_friendly, help_info } from '../utils'; + export default (home_scope: HomeScope) => { - const { message, args, HELP_SECTIONS, KNOWN_COMMANDS } = home_scope; + const { message, args, CONFIG } = home_scope; if (args.length === 0 || args[0] === 'help') { - message.channel.send( - HELP_SECTIONS[KNOWN_COMMANDS.indexOf('8ball')].trim()); + message.channel.send(help_info('8ball', CONFIG.commands.prefix)); return; } diff --git a/lib/commands/help.ts b/lib/commands/help.ts @@ -1,19 +1,29 @@ +import { prefix_friendly } from '../utils'; + + export default (home_scope: HomeScope) => { const { message, args, HELP_SECTIONS, KNOWN_COMMANDS, CONFIG, ALL_HELP, HELP_KEY, HELP_SOURCE, expand_alias } = home_scope; + const p = CONFIG.commands.prefix; + + const [help_key, help_source] = [HELP_KEY, HELP_SOURCE] + .map(s => prefix_friendly(s, p)); + const [help_sections, all_help] = [HELP_SECTIONS, ALL_HELP] + .map(e => e.map(s => prefix_friendly(s, p))); + if (args.length === 0 || args[0] === 'help') { - message.channel.send(HELP_SECTIONS[0]); + message.channel.send(help_sections[0]); return; } if (args[0] === 'key') - return message.channel.send(HELP_KEY); + return message.channel.send(help_key); if (args[0] === 'source') - return message.channel.send(HELP_SOURCE); + return message.channel.send(help_source); if (args[0] === 'all') { - for (const msg of ALL_HELP) + for (const msg of all_help) message.channel.send(msg); return; } @@ -21,15 +31,15 @@ export default (home_scope: HomeScope) => { // Assume the user is now asking for help with a command: // Sanitise: let command : string = args[0].trim(); - if (command.head() === CONFIG.commands.prefix) + if (command.head() === p) command = command.tail(); command = expand_alias(command, args, message).toLowerCase(); const help_index = KNOWN_COMMANDS.indexOf(command); if (help_index === -1) - return message.answer(`No such command/help-page (\`${command}\`).`); + return message.answer(`No such command/help-page (\`${p}${command}\`).`); - message.answer(`**Help (\`${command}\`):**\n` - + HELP_SECTIONS[help_index].trim()); + message.answer(`**Help (\`${p}${command}\`):**\n` + + help_sections[help_index].trim()); }; diff --git a/lib/commands/ship.ts b/lib/commands/ship.ts @@ -1,8 +1,9 @@ import { createHash } from 'crypto'; import { FORMATS } from '../extensions'; -import { Message, MessageAttachment, MessageEmbed } from 'discord.js'; +import { help_info } from '../utils'; +import { MessageAttachment, MessageEmbed } from 'discord.js'; import Jimp from 'jimp'; const TEMPLATE = "./lib/resources/templates/ship.png"; @@ -23,7 +24,7 @@ const RESPONSES = [ { range: [97, 100], message: "Lovey-dovey couple!! :kissing_heart: :heart: :two_hearts:" }, ]; -const BIG_ZERO = BigInt(0) +const BIG_ZERO = BigInt(0); const read512bitsBigIntBigEndian = (buffer : Buffer) : bigint => { let val = BIG_ZERO; @@ -33,15 +34,12 @@ const read512bitsBigIntBigEndian = (buffer : Buffer) : bigint => { }; export default (home_scope : HomeScope) => { - const { message, args, - HELP_SECTIONS, - KNOWN_COMMANDS } = home_scope; + const { message, args, CONFIG } = home_scope; if (args.length === 0 || args[0] === 'help' || message.mentions.users.size === 0 || message.mentions.users.size > 2) { - message.channel.send( - HELP_SECTIONS[KNOWN_COMMANDS.indexOf('ship')].trim()); + message.channel.send(help_info('ship', CONFIG.commands.prefix)); return; } const users = [message.mentions.users.size === 1 diff --git a/lib/main.ts b/lib/main.ts @@ -52,7 +52,7 @@ const SECRETS = JSON.parse(shell('sh ./generate_secrets.sh').toString()); const [HELP_KEY, HELP, HELP_SOURCE] = read_file('./HELP.md') .toString().split('▬▬▬'); -const HELP_SECTIONS = HELP.toString() +export const HELP_SECTIONS = HELP.toString() .replace(/\n -/g, '\n \u25b8') .replace(/\n- /g, '@@@\n\u2b25 ') .split('@@@') @@ -68,7 +68,7 @@ const ALL_HELP = glue_strings([ '\n▬▬▬\n', HELP_SOURCE ]); -const KNOWN_COMMANDS = HELP_SECTIONS.map(e => +export const KNOWN_COMMANDS = HELP_SECTIONS.map(e => e.slice(5).replace(/(\s.*)|(`.*)/g, '').toLowerCase()); const GIT_URL = 'https://github.com/Demonstrandum/Simp-O-Matic'; diff --git a/lib/rule.ts b/lib/rule.ts @@ -1,9 +1,7 @@ -import { glue_strings } from './utils'; +import { glue_strings, help_info } from './utils'; -export const rule = (rule_kind: string) => home_scope => { - const { message, args, - CONFIG, KNOWN_COMMANDS, - HELP_SECTIONS } = home_scope; +export const rule = (rule_kind: string) => (home_scope: HomeScope) => { + const { message, args, CONFIG } = home_scope; const rules_array = CONFIG.rules[rule_kind]; if (args.length === 0 || args[0] === 'ls') { @@ -29,7 +27,7 @@ export const rule = (rule_kind: string) => home_scope => { const index = Number(match[1]) - 1; if (index >= rules_array.length) - return message(`Cannot delete rule at index ${index + 1}...` + return message.answer(`Cannot delete rule at index ${index + 1}...` + ` There are only ${rules_array.length} ${rule_kind} rules.`); message.answer(`Rule matching \`${rules_array[index].match}\`` @@ -94,6 +92,6 @@ export const rule = (rule_kind: string) => home_scope => { } else { message.answer('Insufficient or nonsensical arguments provided.'); message.reply(`Here's how you use the command:\n` - + HELP_SECTIONS[KNOWN_COMMANDS.indexOf(rule_kind)]); + + help_info(rule_kind, CONFIG.commands.prefix)); } }; diff --git a/lib/utils.ts b/lib/utils.ts @@ -1,6 +1,9 @@ import { inspect } from 'util'; import deep_clone from 'deepcopy'; + +import { HELP_SECTIONS, KNOWN_COMMANDS } from './main'; import './extensions'; +import prefix from './commands/prefix'; export const deep_copy = deep_clone; @@ -18,6 +21,17 @@ export const glue_strings = (arr: any[], limit: number = 2000) => { return new_strings; }; +export const prefix_friendly = (str: string, prefix: string) => str + .replace(/`/g, '@@@`').split('@@@') + .map((s, i) => (i % 2 === 1) + ? s.replace(/([^!])!([^!])/g, `$1${prefix}$2`) + : s) + .join(''); + +export const help_info = (command: string, prefix: string) => prefix_friendly( + HELP_SECTIONS[KNOWN_COMMANDS.indexOf(command)].trim(), + prefix); + export const access = (obj: any, shiftable: string[]) => (shiftable.length === 0) ? obj