Use the new help style on the appservice commands and add missing descriptions for the arguments (#69)

* Use the new help style on the appservice commands and add missing descriptions for the arguments

* Make the first letter of the table name uppercase in the help table
This commit is contained in:
Marcel authored and GitHub committed 2023-09-04 12:30:50 +01:00
1 parent e3925afc3b
commit 2ea8ddf864
4 files changed
+46 -26

No files matched your search

+2
View File
@@ -18,6 +18,7 @@ defineInterfaceCommand({
{
name: 'user',
acceptor: findPresentationType('UserID'),
description: 'The user that should be allowed to provision a bot'
}
]),
command: async function (this: AppserviceContext, _keywords: ParsedKeywords, user: UserID): Promise<CommandResult<void>> {
@@ -39,6 +40,7 @@ defineInterfaceCommand({
{
name: 'user',
acceptor: findPresentationType('UserID'),
description: 'The user which shall not be allowed to provision bots anymore'
}
]),
command: async function (this: AppserviceContext, _keywords: ParsedKeywords, user: UserID): Promise<CommandResult<void>> {
@@ -5,9 +5,9 @@
import { WeakEvent } from 'matrix-appservice-bridge';
import { readCommand } from '../../commands/interface-manager/CommandReader';
import { defineCommandTable, defineInterfaceCommand, findCommandTable } from '../../commands/interface-manager/InterfaceCommand';
import { defineCommandTable, defineInterfaceCommand, findCommandTable, findTableCommand } from '../../commands/interface-manager/InterfaceCommand';
import { defineMatrixInterfaceAdaptor, findMatrixInterfaceAdaptor, MatrixContext } from '../../commands/interface-manager/MatrixInterfaceAdaptor';
import { ArgumentStream, parameters } from '../../commands/interface-manager/ParameterParsing';
import { ArgumentStream, RestDescription, findPresentationType, parameters } from '../../commands/interface-manager/ParameterParsing';
import { MjolnirAppService } from '../AppService';
import { CommandResult } from '../../commands/interface-manager/Validation';
import { renderHelp } from '../../commands/interface-manager/MatrixHelpRenderer';
@@ -26,23 +26,23 @@ import './AccessCommands';
import { AppserviceBotEmitter } from './AppserviceBotEmitter';
const helpCommand = defineInterfaceCommand({
parameters: parameters([]),
defineInterfaceCommand({
parameters: parameters([], new RestDescription('command parts', findPresentationType("any"))),
table: "appservice bot",
command: async function() { return CommandResult.Ok(findCommandTable("appservice bot").getAllCommands()) },
command: async function () { return CommandResult.Ok(findCommandTable("appservice bot")) },
designator: ["help"],
summary: "Display this message"
})
defineMatrixInterfaceAdaptor({
interfaceCommand: helpCommand,
interfaceCommand: findTableCommand("appservice bot", "help"),
renderer: renderHelp
})
export class AppserviceCommandHandler {
private readonly commandTable = findCommandTable("appservice bot");
constructor (
constructor(
private readonly appservice: MjolnirAppService
) {
+5 -4
View File
@@ -28,7 +28,7 @@ const listUnstarted = defineInterfaceCommand<AppserviceBaseExecutor>({
designator: ["list", "unstarted"],
table: "appservice bot",
parameters: parameters([]),
command: async function() {
command: async function () {
return CommandResult.Ok(this.appservice.mjolnirManager.getUnstartedMjolnirs());
},
summary: "List any Mjolnir that failed to start."
@@ -38,7 +38,7 @@ const listUnstarted = defineInterfaceCommand<AppserviceBaseExecutor>({
// and be used similar to like #=1 and #1.
defineMatrixInterfaceAdaptor({
interfaceCommand: listUnstarted,
renderer: async function(this: MatrixInterfaceAdaptor<MatrixContext, BaseFunction>, client: MatrixSendClient, commandRoomId: string, event: any, result: CommandResult<UnstartedMjolnir[]>) {
renderer: async function (this: MatrixInterfaceAdaptor<MatrixContext, BaseFunction>, client: MatrixSendClient, commandRoomId: string, event: any, result: CommandResult<UnstartedMjolnir[]>) {
tickCrossRenderer.call(this, client, commandRoomId, event, result); // don't await, it doesn't really matter.
if (result.isErr()) {
return; // just let the default handler deal with it.
@@ -53,7 +53,7 @@ defineMatrixInterfaceAdaptor({
{mjolnir.mjolnirRecord.owner},
<code>{mjolnir.mxid.toString()}</code>
<code>{mjolnir.failCode}</code>:
<br/>
<br />
{mjolnir.cause}
</li>
})}
@@ -77,9 +77,10 @@ const restart = defineInterfaceCommand<AppserviceBaseExecutor>({
{
name: "mjolnir",
acceptor: findPresentationType("UserID"),
description: 'The userid of the mjolnir to restart'
}
]),
command: async(context, _keywords, mjolnirId: UserID): Promise<CommandResult<true>> => {
command: async (context, _keywords, mjolnirId: UserID): Promise<CommandResult<true>> => {
const mjolnirManager = context.appservice.mjolnirManager;
const mjolnir = mjolnirManager.findUnstartedMjolnir(mjolnirId.localpart);
if (mjolnir?.mjolnirRecord === undefined) {
@@ -3,7 +3,7 @@
*/
import { MatrixSendClient } from "../../MatrixEmitter";
import { BaseFunction, InterfaceCommand } from "./InterfaceCommand";
import { BaseFunction, CommandTable, InterfaceCommand } from "./InterfaceCommand";
import { MatrixContext, MatrixInterfaceAdaptor } from "./MatrixInterfaceAdaptor";
import { ArgumentParseError, ParameterDescription, RestDescription } from "./ParameterParsing";
import { CommandError, CommandResult } from "./Validation";
@@ -29,17 +29,17 @@ function restArgument(rest: RestDescription): string {
export function renderParameterDescription(description: ParameterDescription): DocumentNode {
return <fragment>
{description.name} - {description.description ?? 'no description'}<br/>
{description.name} - {description.description ?? 'no description'}<br />
</fragment>
}
export function renderCommandSummary(command: InterfaceCommand<BaseFunction>): DocumentNode {
return <details>
<summary>
<code>{renderCommandHelp(command)}</code> - {command.summary}
<code>{renderCommandHelp(command)}</code> - {command.summary}
</summary>
{command.description
? <fragment><b>Description:</b><br/>{command.description}<br/></fragment>
? <fragment><b>Description:</b><br />{command.description}<br /></fragment>
: []
}
{command.argumentListParser.descriptions.length > 0
@@ -63,13 +63,30 @@ export function renderCommandHelp(command: InterfaceCommand<BaseFunction>): stri
].join(' ');
}
export async function renderHelp(client: MatrixSendClient, commandRoomId: string, event: any, result: CommandResult<InterfaceCommand<BaseFunction>[], CommandError>): Promise<void> {
const commands = result.ok;
let text = ''
for (const command of commands) {
text += `${renderCommandHelp(command)}\n`;
function renderTableHelp(table: CommandTable): DocumentNode {
let tableName = table.name;
if (typeof table.name === 'string') {
tableName = table.name.charAt(0).toUpperCase() + table.name.slice(1);
}
await client.replyNotice(commandRoomId, event, text);
return <root>
<details>
<summary><b>{tableName} commands:</b></summary>
{table.getExportedCommands().map(renderCommandSummary)}
{table.getImportedTables().map(renderTableHelp)}
</details>
</root>
}
export async function renderHelp(client: MatrixSendClient, commandRoomId: string, event: any, result: CommandResult<CommandTable, CommandError>): Promise<void> {
if (result.isErr()) {
throw new TypeError("This command isn't supposed to fail");
}
await renderMatrixAndSend(
renderTableHelp(result.ok),
commandRoomId,
event,
client
);
}
export async function tickCrossRenderer(this: MatrixInterfaceAdaptor<MatrixContext, BaseFunction>, client: MatrixSendClient, commandRoomId: string, event: any, result: CommandResult<unknown, CommandError>): Promise<void> {
@@ -122,17 +139,17 @@ function formattedArgumentHint(command: InterfaceCommand<BaseFunction>, error: A
function renderArgumentParseError(command: InterfaceCommand<BaseFunction>, error: ArgumentParseError): DocumentNode {
return <root>
There was a problem when parsing the <code>{error.parameter.name}</code> parameter for this command.<br/>
{renderCommandHelp(command)}<br/>
{error.message}<br/>
There was a problem when parsing the <code>{error.parameter.name}</code> parameter for this command.<br />
{renderCommandHelp(command)}<br />
{error.message}<br />
<pre>{formattedArgumentHint(command, error)}</pre>
</root>
}
function renderCommandException(command: InterfaceCommand<BaseFunction>, error: CommandException): DocumentNode {
return <root>
There was an unexpected error when processing this command:<br/>
{error.message}<br/>
There was an unexpected error when processing this command:<br />
{error.message}<br />
Details can be found by providing the reference <code>{error.uuid}</code>
to an administrator.
</root>