|
| 1 | +/* |
| 2 | +Copyright 2024 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { ICommand } from "./ICommand"; |
| 18 | +import { Conference } from "../Conference"; |
| 19 | +import {ConferenceMatrixClient} from "../ConferenceMatrixClient"; |
| 20 | +import {InviteMeCommand} from "./InviteMeCommand"; |
| 21 | + |
| 22 | +export class PowerLevelCommand implements ICommand { |
| 23 | + |
| 24 | + constructor(private readonly client: ConferenceMatrixClient, private readonly conference: Conference) { |
| 25 | + } |
| 26 | + |
| 27 | + public readonly prefixes = ["powerlevels"]; |
| 28 | + |
| 29 | + public async run(managementRoomId: string, event: any, args: string[]) { |
| 30 | + let targetId = args[0] |
| 31 | + let pl = args[2] |
| 32 | + |
| 33 | + const IM = new InviteMeCommand(this.client, this.conference); |
| 34 | + const roomGroups = await IM.roomGroups(); |
| 35 | + console.log(roomGroups) |
| 36 | + |
| 37 | + if (!args.length) { |
| 38 | + return this.client.replyHtmlNotice(managementRoomId, event, "Please specify a room ID or alias, or one of the room groups:\n" + IM.prettyGroupNameList(roomGroups)); |
| 39 | + } |
| 40 | + |
| 41 | + if (roomGroups.has(args[1])) { |
| 42 | + const group = roomGroups.get(args[1])!; |
| 43 | + for (const roomId of group) { |
| 44 | + try { |
| 45 | + await this.client.setUserPowerLevel(targetId, roomId, Number(pl)); |
| 46 | + } |
| 47 | + catch (e) { |
| 48 | + throw new Error(`Error setting power levels: in room ${roomId}, ${e.body}`) |
| 49 | + } |
| 50 | + await this.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); |
| 51 | + } |
| 52 | + } else { |
| 53 | + let targetRoomId; |
| 54 | + try { |
| 55 | + targetRoomId = await this.client.resolveRoom(args[1]); |
| 56 | + } |
| 57 | + catch (error) { |
| 58 | + throw Error(`Error resolving room ${args[1]}`, {cause:error}) |
| 59 | + } |
| 60 | + try { |
| 61 | + await this.client.setUserPowerLevel(targetRoomId, targetRoomId, Number(pl)); |
| 62 | + } |
| 63 | + catch (e) { |
| 64 | + throw new Error(`Error setting power levels in room ${targetRoomId}: ${e.body}`) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return this.client.replyHtmlNotice(managementRoomId, event, "Power levels sent") |
| 69 | + } |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | + |
0 commit comments