diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index f1dd0c5d..b48f289f 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -33,6 +33,7 @@ import { execListProtectedRooms } from "./ListProtectedRoomsCommand"; import { execAddProtectedRoom, execRemoveProtectedRoom } from "./AddRemoveProtectedRoomsCommand"; import { execMoveAliasCommand } from "./MoveAliasCommand"; import { execAddRoomToDirectoryCommand, execRemoveRoomFromDirectoryCommand } from "./AddRemoveRoomFromDirectoryCommand"; +import { execSetPowerLevelCommand } from "./SetPowerLevelCommand"; export const COMMAND_PREFIX = "!mjolnir"; @@ -85,6 +86,8 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir return await execAddRoomToDirectoryCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'directory' && parts.length > 3 && parts[2] === 'remove') { return await execRemoveRoomFromDirectoryCommand(roomId, event, mjolnir, parts); + } else if (parts[1] === 'powerlevel' && parts.length > 3) { + return await execSetPowerLevelCommand(roomId, event, mjolnir, parts); } else { // Help menu const menu = "" + @@ -111,6 +114,7 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir "!mjolnir move - Moves a to a new \n" + "!mjolnir directory add - Publishes a room in the server's room directory\n" + "!mjolnir directory remove - Removes a room from the server's room directory\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"; const html = `Mjolnir help:
${htmlEscape(menu)}
`; const text = `Mjolnir help:\n${menu}`; diff --git a/src/commands/SetPowerLevelCommand.ts b/src/commands/SetPowerLevelCommand.ts new file mode 100644 index 00000000..62ae1203 --- /dev/null +++ b/src/commands/SetPowerLevelCommand.ts @@ -0,0 +1,40 @@ +/* +Copyright 2020 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { Mjolnir } from "../Mjolnir"; +import { LogLevel, LogService } from "matrix-bot-sdk"; +import { logMessage } from "../LogProxy"; + +// !mjolnir powerlevel [room] +export async function execSetPowerLevelCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { + const victim = parts[2]; + const level = Math.round(Number(parts[3])); + const inRoom = parts[4]; + + let targetRooms = inRoom ? [await mjolnir.client.resolveRoom(inRoom)] : Object.keys(mjolnir.protectedRooms); + + for (const targetRoomId of targetRooms) { + try { + await mjolnir.client.setUserPowerLevel(victim, targetRoomId, level); + } catch (e) { + const message = e.message || (e.body ? e.body.error : ''); + await logMessage(LogLevel.ERROR, "SetPowerLevelCommand", `Failed to set power level of ${victim} to ${level} in ${targetRoomId}: ${message}`); + LogService.error("SetPowerLevelCommand", e); + } + } + + await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); +}