Simp-O-Matic

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

commit a6fb6be73a62b649e1d93a084a54f368de5f347a
parent 6e04d8db16828ec6b46443b498126d02a379ebd5
Author: Demonstrandum <moi@knutsen.co>
Date:   Wed, 20 May 2020 23:56:12 +0100

Merge branch 'master' of github.com:Demonstrandum/Simp-O-Matic

Diffstat:
MHELP.md | 1+
Alib/commands/sourcecode.ts | 20++++++++++++++++++++
Mlib/extensions.ts | 6+++---
3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/HELP.md b/HELP.md @@ -121,6 +121,7 @@ - `!pat [@user-name]` — Give someone a pat on the head. - `!emojis` — Lists all the emojis on the server. - `!hangman <options>` **〈not impl.〉** — Hangman game to play with your friends. +- `!sourcecode [!command]` — shows the source code of a certain command. ▬▬▬ diff --git a/lib/commands/sourcecode.ts b/lib/commands/sourcecode.ts @@ -0,0 +1,20 @@ +import { FORMATS } from '../extensions'; +import { Message } from 'discord.js'; + +import { readFileSync as read_file } from 'fs'; + +export default (home_scope: HomeScope) => { + const { message, args } + : { message: Message, args: string[] } = home_scope; + + if (args.length == 0) { + return message.channel.send( + "View a command's source code.\n.sourcecode [!command]" + ); + } + + const command_name = args[0].replace(/^\.|\.ts/, ''); + const src = read_file(`./${command_name}.ts`).toString(); + + message.channel.send(src.format(FORMATS.code_block, 'ts')); +}; diff --git a/lib/extensions.ts b/lib/extensions.ts @@ -122,7 +122,7 @@ declare global { tail(): string; first(): string; last(off? : number): string; - format(fmt: string): string; + format(fmt: string, code_block_lang?: string): string; emojify(): string; shorten(width?: number): string; lines(): string[]; @@ -219,8 +219,8 @@ export const FORMATS: TextFormat = { hidden: '||', }; -String.prototype.format = function (fmt: string) { - return `${fmt}${this.toString()}${fmt}`; +String.prototype.format = function (fmt: string, code_block_lang?: string) { + return `${fmt}${code_block_lang ? code_block_lang + "\n" : ''}${this.toString()}${fmt}`; }; String.prototype.shorten = function (width=40) {