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

Config to auto-apply power levels to designated users #736

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/736.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add configuration setting (`bridge.userPowerLevels`) to auto-apply power levels to designated users on bridge startup.
7 changes: 7 additions & 0 deletions config/config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ bridge:
adminMxid: '@admin:localhost'
# The message to send to the bridge admin if the Discord token is not valid
invalidTokenMessage: 'Your Discord bot token seems to be invalid, and the bridge cannot function. Please update it in your bridge settings and restart the bridge'
# Custom power levels to assign to Matrix users in bridged rooms (supports regexes).
# Patterns are tested in order; first match is used.
#userPowerLevels:
# - userPattern: "@admin:example.com"
# pl: 100
# - userPattern: "@.*:example.com"
# pl: 50
# Authentication configuration for the discord bot.
auth:
# This MUST be a string (wrapped in quotes)
Expand Down
8 changes: 8 additions & 0 deletions config/config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ properties:
type: "number"
userLimit:
type: "number"
userPowerLevels:
type: "array"
items:
type: "object"
required: ["userPattern", "pl"]
properties:
userPattern: "string"
pl: "number"
auth:
type: "object"
required: ["botToken", "clientID"]
Expand Down
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export class DiscordBridgeConfig {
}
}

interface UserPowerLevel {
userPattern: string;
pl: number;
}

export class DiscordBridgeConfigBridge {
public domain: string;
public homeserverUrl: string;
Expand All @@ -103,6 +108,11 @@ export class DiscordBridgeConfigBridge {
public userLimit: number|null = null;
public adminMxid: string|null = null;
public invalidTokenMessage: string = 'Your Discord token is invalid';
public userPowerLevels: UserPowerLevel[] = [];

public GetCustomPowerLevelForUser(userId: string): number|undefined {
return this.userPowerLevels.find(obj => RegExp(obj.userPattern).test(userId))?.pl;
}
}

export class DiscordBridgeConfigDatabase {
Expand Down
16 changes: 16 additions & 0 deletions src/discordas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ async function run(): Promise<void> {
await appservice.begin();
log.info(`Started listening on port ${port}`);

const client = appservice.botIntent.underlyingClient;
for (const roomId of await client.getJoinedRooms()) {
for (const userId of await client.getJoinedRoomMembers(roomId)) {
let customPL;
if (!appservice.isNamespacedUser(userId) &&
(customPL = config.bridge.GetCustomPowerLevelForUser(userId)) !== undefined) {
// TODO allSettled
try {
await client.setUserPowerLevel(userId, roomId, customPL);
log.verbose(`Set power level of ${customPL} for user ${userId} in room ${roomId}`);
} catch (err) {
log.warn(`Cannot set custom power level of ${customPL} for user ${userId} in room ${roomId}: ${err}`);
}
}
}
}
}

run().catch((err) => {
Expand Down
6 changes: 6 additions & 0 deletions src/matrixeventprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ export class MatrixEventProcessor {
}
// We don't know if the user also updated their profile, but to be safe..
this.mxUserProfileCache.delete(event.sender);

const customPL = this.config.bridge.GetCustomPowerLevelForUser(event.sender);
if (customPL !== undefined) {
client.setUserPowerLevel(event.sender, event.room_id, customPL)
.catch((err) => log.warn(`Cannot set custom power level of ${customPL} for user ${event.sender} in room ${event.room_id}: ${err}`));
}
}
if (membership === "join" && isNewJoin && allowJoinLeave) {
msg += "joined the room";
Expand Down