Skip to content

Commit

Permalink
chore: migrate help messages to grm_parse (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Jul 27, 2022
1 parent 0692373 commit 3e4a33e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
11 changes: 7 additions & 4 deletions module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Handler } from "./handlers/mod.ts";
import { FormattedString } from "./deps.ts";

export interface Module {
name: string;
handlers: Handler[];
help?: string;
help?: string | FormattedString;
}

export function isModule(value: unknown): value is Module {
Expand All @@ -17,9 +18,11 @@ export function isModule(value: unknown): value is Module {
}

export function getHelp(mod: Module) {
if (mod.help !== undefined) {
mod.help = mod.help.trim();
if (mod.help.length > 0 && mod.help.length <= 4096) {
if (typeof mod.help !== "undefined") {
const length = typeof mod.help === "string"
? mod.help.length
: mod.help.text.length;
if (length > 0 && length <= 4096) {
return mod.help;
}
}
Expand Down
15 changes: 8 additions & 7 deletions module_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Api, NewMessageEvent, TelegramClient } from "$grm";
import { join, resolve, toFileUrl } from "./deps.ts";
import { bold, fmt, join, resolve, toFileUrl } from "./deps.ts";
import { CommandHandler, End } from "./handlers/mod.ts";
import { updateMessage } from "./helpers.ts";
import { getHelp, isModule, Module } from "./module.ts";
Expand Down Expand Up @@ -147,15 +147,17 @@ export function managerModule(manager: ModuleManager): Module {
await updateMessage(event, "This module has no help.");
return;
}
await event.message.reply({ message, parseMode: "markdown" });
await event.message.reply({
...(typeof message === "string" ? { message } : message.send),
parseMode: "markdown",
});
}),
],
help: `
**Introduction**
help: fmt`${bold("Introduction")}
The module manager lets you list the installed modules, get help for them and install external modules.
**Commands**
${bold("Commands")}
- install
Expand All @@ -179,8 +181,7 @@ Lists the installed modules.
- help
Sends the help message of a module if existing.
`,
Sends the help message of a module if existing.`,
};
}

Expand Down
20 changes: 16 additions & 4 deletions modules/admin/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Api } from "$grm";
import { CommandHandler, Methods, Module, updateMessage } from "$xor";
import {
bold,
CommandHandler,
fmt,
italic,
Methods,
Module,
updateMessage,
} from "$xor";
import { getUser, wrapRpcErrors } from "./helpers.ts";

const admin: Module = {
Expand Down Expand Up @@ -163,11 +171,11 @@ const admin: Module = {
});
}),
],
help: `**Introduction**
help: fmt`${bold("Introduction")}
This module aims to make administering chats easy.
**Commands**
${bold("Commands")}
- promote
Expand All @@ -189,7 +197,11 @@ Unbans the requested user from the chat.
Adds the requested user to the chat.
**Note: The commands above work by replying to a user or passing their username or ID as the first argument.**
${
italic(
"Note: The commands above work by replying to a user or passing their username or ID as the first argument.",
)
}
- pin
Expand Down
14 changes: 11 additions & 3 deletions modules/files/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Api, CustomFile } from "$grm";
import { Buffer } from "$grm-deps";
import { code, CommandHandler, fmt, join, Module, updateMessage } from "$xor";
import {
bold,
code,
CommandHandler,
fmt,
join,
Module,
updateMessage,
} from "$xor";
import { parseAttributes } from "./helpers.ts";

const files: Module = {
Expand Down Expand Up @@ -143,11 +151,11 @@ const files: Module = {
},
),
],
help: `**Introduction**
help: fmt`${bold("Introduction")}
The files module can be used for operations like file download, upload, listing and more.
**Commands**
${bold("Commands")}
- download, dl
Expand Down
4 changes: 2 additions & 2 deletions modules/util/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ V8 ${Deno.version.v8}`,
);
}),
],
help: `**Introduction**
help: fmt`${bold("Introduction")}
The util module includes some useful commands to interact with the system and get basic information of the surrounding.
**Commands**
${bold("Commands")}
- ping
Expand Down

0 comments on commit 3e4a33e

Please sign in to comment.