Skip to content

Commit

Permalink
feat: add help to Module (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Feb 4, 2022
1 parent d38a7ba commit 6222bbf
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Handler } from './handlers'
export interface Module {
name: string
handlers: Handler[]
help?: string
}

export function isModule(value: unknown): value is Module {
Expand Down
80 changes: 79 additions & 1 deletion src/module_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { promises as fs } from 'fs'

import { Api, TelegramClient } from 'telegram'
import { NewMessageEvent } from 'telegram/events'
import escape from 'html-escape'

import { Module, isModule } from './module'
import { CommandHandler } from './handlers'
Expand Down Expand Up @@ -116,8 +117,85 @@ export function managerModule(manager: ModuleManager): Module {
enabled == 1 ? '' : 's'
} enabled.`
})
}),
new CommandHandler('modules', async (_client, event) => {
const all = Array.from(manager.modules.values())
const nonDisableables = all
.filter(([, disableable]) => !disableable)
.map(([module]) => module.name)
const installed = all
.filter(([, disableable]) => disableable)
.map(([module]) => module.name)
let message = '**Built-in**\n'
for (const module of nonDisableables) {
message += escape(module) + '\n'
}
message += '\n**Installed**\n'
if (installed.length == 0) {
message += 'No modules installed.'
} else {
for (const module of installed) {
message += escape(module) + '\n'
}
}
await event.message.reply({ message })
}),
new CommandHandler('help', async (_client, event, args) => {
const name = args[0]
if (!name) {
await event.message.edit({
text:
event.message.text + '\n' + 'Pass a module name as an argument.'
})
return
}
const module = manager.modules.get(name)
if (!module) {
await event.message.edit({
text: event.message.text + '\n' + 'This module is not installed.'
})
return
}
if (!module[0].help) {
await event.message.edit({
text: event.message.text + '\n' + 'This module has no help.'
})
return
}
await event.message.reply({ message: module[0].help })
})
]
],
help: `
**Introduction**
The module manager lets you list the installed modules, get help for them and install external modules.
**Commands**
- install
Installs an external module from the replied module file.
- uninstall
Uninstalls one or more external modules.
- disable
Disables one or more external modules.
- enable
Enables one or more external modules.
- modules
Lists the installed modules.
- help
Sends the help message of a module if existing.
`
}
}

Expand Down
34 changes: 33 additions & 1 deletion src/modules/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,39 @@ const util: Module = {
file: new CustomFile('result.txt', buffer.length, '', buffer)
})
})
]
],
help: `
**Introduction**
The util module includes some useful commands to interact with the system and get basic information of the surrounding.
**Commands**
- ping
Tells how much a ping of Telegram servers takes.
- shell, sh, cmd, exec
Runs a shell command and sends its output. Any input will be passed to the stdin of the process.
- uptime
Displays the uptime of the bot in (hh:)mm:ss format.
- version, v
Displays the versions of Xor, GramJS and Node.
- whois
Fetches and displays basic information about the current chat, the provided identifier as the first argument and/or the replied message. The provided identifier can be a username, a phone number, a user/chat ID or a chat invite ID.
- eval
Runs and sends the output of JavaScript code. As of now, it passes the GramJS client as \`client\`, the \`NewMessageEvent\` as \`event\` and the GramJS API namespace as \`Api\`.
`
}

export default util

0 comments on commit 6222bbf

Please sign in to comment.