Skip to content

Commit d2ce0a5

Browse files
authored
Add a powerlevel command (#226)
1 parent f2bfadb commit d2ce0a5

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

src/commands/HelpCommand.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export class HelpCommand implements ICommand {
6868
"!conference join <room> - Makes the bot join the given room.\n" +
6969
"!conference copymods <from> <to> - Copies the moderators from one room to another.\n" +
7070
"!conference widgets <aud> - Creates all widgets for the auditorium and its talks.\n" +
71+
"!conferece powerlevels <user> <room> <powerlevel> - Assigns the given powerlevel to the given userid in the given room or room group. Run the " +
72+
"command without a room and powerlevel to see a list of room groups. User must be in room to have pl elevated\n" +
7173
"</code></pre>" +
7274
"";
7375
return this.client.replyHtmlNotice(roomId, event, htmlHelp);

src/commands/InviteMeCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class InviteMeCommand implements ICommand {
9999
/**
100100
* Render a (somewhat) pretty list of group names.
101101
*/
102-
private prettyGroupNameList(roomGroups: Map<string, Set<string>>) {
102+
public prettyGroupNameList(roomGroups: Map<string, Set<string>>) {
103103
const bySection = new Map<string, string[]>();
104104

105105
// organise the groups into sections

src/commands/PowerLevelCommand.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { PermissionsCommand } from "./commands/PermissionsCommand";
4343
import { VerifyCommand } from "./commands/VerifyCommand";
4444
import { CustomLogger } from "./CustomLogger";
4545
import { InviteMeCommand } from "./commands/InviteMeCommand";
46+
import { PowerLevelCommand } from "./commands/PowerLevelCommand";
4647
import { WidgetsCommand } from "./commands/WidgetsCommand";
4748
import { Scoreboard } from "./Scoreboard";
4849
import { Scheduler } from "./Scheduler";
@@ -242,6 +243,7 @@ export class ConferenceBot {
242243
new HelpCommand(this.client),
243244
new InviteCommand(this.client, this.conference, this.config),
244245
new InviteMeCommand(this.client, this.conference),
246+
new PowerLevelCommand(this.client, this.conference),
245247
new JoinCommand(this.client),
246248
new PermissionsCommand(this.client, this.conference),
247249
new RunCommand(this.client, this.conference, this.scheduler),

0 commit comments

Comments
 (0)