Skip to content

Commit

Permalink
Add a powerlevel command (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Shay authored Feb 2, 2024
1 parent f2bfadb commit d2ce0a5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/commands/HelpCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class HelpCommand implements ICommand {
"!conference join <room> - Makes the bot join the given room.\n" +
"!conference copymods <from> <to> - Copies the moderators from one room to another.\n" +
"!conference widgets <aud> - Creates all widgets for the auditorium and its talks.\n" +
"!conferece powerlevels <user> <room> <powerlevel> - Assigns the given powerlevel to the given userid in the given room or room group. Run the " +
"command without a room and powerlevel to see a list of room groups. User must be in room to have pl elevated\n" +
"</code></pre>" +
"";
return this.client.replyHtmlNotice(roomId, event, htmlHelp);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/InviteMeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class InviteMeCommand implements ICommand {
/**
* Render a (somewhat) pretty list of group names.
*/
private prettyGroupNameList(roomGroups: Map<string, Set<string>>) {
public prettyGroupNameList(roomGroups: Map<string, Set<string>>) {
const bySection = new Map<string, string[]>();

// organise the groups into sections
Expand Down
74 changes: 74 additions & 0 deletions src/commands/PowerLevelCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2024 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 { ICommand } from "./ICommand";
import { Conference } from "../Conference";
import {ConferenceMatrixClient} from "../ConferenceMatrixClient";
import {InviteMeCommand} from "./InviteMeCommand";

export class PowerLevelCommand implements ICommand {

constructor(private readonly client: ConferenceMatrixClient, private readonly conference: Conference) {
}

public readonly prefixes = ["powerlevels"];

public async run(managementRoomId: string, event: any, args: string[]) {
let targetId = args[0]
let pl = args[2]

const IM = new InviteMeCommand(this.client, this.conference);
const roomGroups = await IM.roomGroups();
console.log(roomGroups)

if (!args.length) {
return this.client.replyHtmlNotice(managementRoomId, event, "Please specify a room ID or alias, or one of the room groups:\n" + IM.prettyGroupNameList(roomGroups));
}

if (roomGroups.has(args[1])) {
const group = roomGroups.get(args[1])!;
for (const roomId of group) {
try {
await this.client.setUserPowerLevel(targetId, roomId, Number(pl));
}
catch (e) {
throw new Error(`Error setting power levels: in room ${roomId}, ${e.body}`)
}
await this.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
}
} else {
let targetRoomId;
try {
targetRoomId = await this.client.resolveRoom(args[1]);
}
catch (error) {
throw Error(`Error resolving room ${args[1]}`, {cause:error})
}
try {
await this.client.setUserPowerLevel(targetRoomId, targetRoomId, Number(pl));
}
catch (e) {
throw new Error(`Error setting power levels in room ${targetRoomId}: ${e.body}`)
}
}

return this.client.replyHtmlNotice(managementRoomId, event, "Power levels sent")
}

}



2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { PermissionsCommand } from "./commands/PermissionsCommand";
import { VerifyCommand } from "./commands/VerifyCommand";
import { CustomLogger } from "./CustomLogger";
import { InviteMeCommand } from "./commands/InviteMeCommand";
import { PowerLevelCommand } from "./commands/PowerLevelCommand";
import { WidgetsCommand } from "./commands/WidgetsCommand";
import { Scoreboard } from "./Scoreboard";
import { Scheduler } from "./Scheduler";
Expand Down Expand Up @@ -242,6 +243,7 @@ export class ConferenceBot {
new HelpCommand(this.client),
new InviteCommand(this.client, this.conference, this.config),
new InviteMeCommand(this.client, this.conference),
new PowerLevelCommand(this.client, this.conference),
new JoinCommand(this.client),
new PermissionsCommand(this.client, this.conference),
new RunCommand(this.client, this.conference, this.scheduler),
Expand Down

0 comments on commit d2ce0a5

Please sign in to comment.