commit 140d762a3c335c53120b1bd716a850130ccf9a28
parent cc59a92cb732aba33f50c026990ecae5e2fa406c
Author: Demonstrandum <moi@knutsen.co>
Date: Thu, 21 May 2020 00:25:35 +0100
Split messages into chunks for large files.
Diffstat:
2 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/HELP.md b/HELP.md
@@ -121,7 +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.
+- `!code [!command]` — Shows the source code of a certain command.
▬▬▬
diff --git a/lib/commands/code.ts b/lib/commands/code.ts
@@ -1,5 +1,8 @@
import { readFileSync as read_file } from 'fs';
+import { FORMATS } from '../extensions';
+import { glue_strings } from '../utils';
+
export default (homescope : HomeScope) => {
const { message, args, CONFIG } = homescope;
const p = CONFIG.commands.prefix;
@@ -16,10 +19,21 @@ export default (homescope : HomeScope) => {
const filename = `${process.cwd()}/lib/commands/${command}.ts`;
try {
- const source = read_file(filename);
- const msg = `Source code for \`${p}${command}\`:`
- + "\n```typescript\n" + source + "\n```";
- message.channel.send(msg);
+ const source = read_file(filename).toString();
+ const msg = `Source code for \`${p}${command}\`:`;
+
+ if (source.length > 1900) {
+ const chunks = glue_strings(source.split('\n'), 1950);
+
+ for (const chunk of chunks)
+ message.channel.send(
+ chunk.format(FORMATS.code_block, 'typescript'));
+
+ message.channel.send(msg);
+ } else {
+ message.channel.send(`${msg}\n`
+ + source.format(FORMATS.code_block, 'typescript'));
+ }
} catch {
message.answer(`Source for \`${p}${command}\``
+ ` (\`${filename}\`), was not found.`);