From f5763803d93f30f6b5327dfbc775e5366c5f226f Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 21 Feb 2020 09:58:22 -0700 Subject: [PATCH] Add a command to resolve a room alias to a room ID --- src/commands/AliasCommands.ts | 14 ++++++++++++++ src/commands/CommandHandler.ts | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/commands/AliasCommands.ts b/src/commands/AliasCommands.ts index c209c1c..5ba5851 100644 --- a/src/commands/AliasCommands.ts +++ b/src/commands/AliasCommands.ts @@ -16,6 +16,7 @@ limitations under the License. import { Mjolnir } from "../Mjolnir"; import { RichReply } from "matrix-bot-sdk"; +import * as htmlEscape from "escape-html"; // !mjolnir move export async function execMoveAliasCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { @@ -72,3 +73,16 @@ export async function execRemoveAliasCommand(roomId: string, event: any, mjolnir await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); } + +// !mjolnir resolve +export async function execResolveCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { + const toResolve = parts[2]; + + const resolvedRoomId = await mjolnir.client.resolveRoom(toResolve); + + const message = `Room ID for ${toResolve} is ${resolvedRoomId}`; + const html = `Room ID for ${htmlEscape(toResolve)} is ${htmlEscape(resolvedRoomId)}`; + const reply = RichReply.createFor(roomId, event, message, html); + reply["msgtype"] = "m.notice"; + await mjolnir.client.sendMessage(roomId, reply); +} diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index cefe4d0..07603c0 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -34,7 +34,7 @@ import { execAddProtectedRoom, execRemoveProtectedRoom } from "./AddRemoveProtec import { execAddRoomToDirectoryCommand, execRemoveRoomFromDirectoryCommand } from "./AddRemoveRoomFromDirectoryCommand"; import { execSetPowerLevelCommand } from "./SetPowerLevelCommand"; import { execShutdownRoomCommand } from "./ShutdownRoomCommand"; -import { execAddAliasCommand, execMoveAliasCommand, execRemoveAliasCommand } from "./AliasCommands"; +import { execAddAliasCommand, execMoveAliasCommand, execRemoveAliasCommand, execResolveCommand } from "./AliasCommands"; export const COMMAND_PREFIX = "!mjolnir"; @@ -91,6 +91,8 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir return await execAddAliasCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'alias' && parts.length > 3 && parts[2] === 'remove') { return await execRemoveAliasCommand(roomId, event, mjolnir, parts); + } else if (parts[1] === 'resolve' && parts.length > 2) { + return await execResolveCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'powerlevel' && parts.length > 3) { return await execSetPowerLevelCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'shutdown' && parts[2] === 'room' && parts.length > 3) { @@ -124,6 +126,7 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir "!mjolnir directory remove - Removes a room from the server's room directory\n" + "!mjolnir alias add - Adds to \n" + "!mjolnir alias remove - Deletes the room alias from whatever room it is attached to\n" + + "!mjolnir resolve - Resolves a room alias to a room ID\n" + "!mjolnir shutdown room - Uses the bot's account to shut down a room, preventing access to the room on this server\n" + "!mjolnir powerlevel [room alias/ID] - Sets the power level of the user in the specified room (or all protected rooms)\n" + "!mjolnir help - This menu\n";