Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a powerlevel command #226

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably terrible - is there a better way to import functions from another class ala Python?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably normally pull out the function so it's not in the class itself, but 'meh' for now

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
Loading