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 more specific errors to CopyModerators command #196

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 34 additions & 5 deletions src/commands/CopyModeratorsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,21 @@ export class CopyModeratorsCommand implements ICommand {
}
const fromRoomId = await this.client.resolveRoom(args[0]);
const toRoomId = await this.client.resolveRoom(args[1]);
const fromPl: {"users"?: Record<string, any>} = await this.client.getRoomStateEvent(fromRoomId, "m.room.power_levels", "");
let toPl = await this.client.getRoomStateEvent(toRoomId, "m.room.power_levels", "");

let fromPl: { "users"?: Record<string, any> } = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

PowerLevelsEventContent may also work here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does - much nicer, thanks!

try {
fromPl = await this.client.getRoomStateEvent(fromRoomId, "m.room.power_levels", "");
}
catch (error) {
throw Error(`Error fetching or processing power level event from room ${fromRoomId}: ${error.toString()}`)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
}

try {
var toPl = await this.client.getRoomStateEvent(toRoomId, "m.room.power_levels", "");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
try {
var toPl = await this.client.getRoomStateEvent(toRoomId, "m.room.power_levels", "");
let toPl: PowerLevelsEventContent;
try {
toPl = await this.client.getRoomStateEvent(toRoomId, "m.room.power_levels", "");

Avoid using var if you can, it doesn't conform to block scoping and tends to surprise you. In this case TypeScript should let you define toPl outside and it will always be set, because you are throwing.

}
catch (error) {
throw Error(`Error fetching or processing power level event from room ${toRoomId}: ${error.toString()}`)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
}

if (!toPl) toPl = {};
if (!toPl['users']) toPl['users'] = {};
Expand All @@ -42,14 +55,30 @@ export class CopyModeratorsCommand implements ICommand {
}
}

await this.client.sendStateEvent(toRoomId, "m.room.power_levels", "", toPl);
try {
await this.client.sendStateEvent(toRoomId, "m.room.power_levels", "", toPl);
}
catch (error) {
throw Error(`Error sending new power level event into room ${toRoomId}: ${error.toString()}`)
}

const state = await this.client.getRoomState(toRoomId);
let state: any[] = []
try {
state = await this.client.getRoomState(toRoomId);
}
catch (error) {
throw Error(`Error getting room state from room ${toRoomId}: ${error.toString()}`)
}
const members = state.filter(s => s.type === "m.room.member").map(s => new MembershipEvent(s));
const effectiveJoinedUserIds = members.filter(m => m.effectiveMembership === "join").map(m => m.membershipFor);
for (const userId of Object.keys(toPl['users'])) {
if (!effectiveJoinedUserIds.includes(userId)) {
await this.client.inviteUser(userId, toRoomId);
try {
await this.client.inviteUser(userId, toRoomId);
}
catch (error) {
throw Error(`Error inviting user ${userId} to room ${toRoomId}: ${error.toString()}`)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/HelpCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class HelpCommand implements ICommand {
"<pre><code>" +
"!conference inviteme &lt;room&gt; - Asks the bot to invite you to the given room.\n" +
"!conference inviteto &lt;room&gt; &lt;user&gt; - Asks the bot to invite the given user to the given room.\n" +
"!conference join &lt;room&gt; - Makes the bot join the given room.\n"
"!conference join &lt;room&gt; - Makes the bot join the given room.\n" +
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 was just a typo I noticed that was causing the last two help commands to not be shown in the client. Drive-by cleanup.

Copy link
Contributor

Choose a reason for hiding this comment

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

I hate how JavaScript is happy to have these implied semicolons lead to effectively empty / ignored statements. Thanks for catching it :-)

"!conference copymods &lt;from&gt; &lt;to&gt; - Copies the moderators from one room to another.\n" +
"!conference widgets &lt;aud&gt; - Creates all widgets for the auditorium and its talks.\n" +
"</code></pre>" +
Expand Down