Skip to content

Commit

Permalink
feat: add listusers command
Browse files Browse the repository at this point in the history
Signed-off-by: Seth Falco <[email protected]>
  • Loading branch information
SethFalco committed Sep 20, 2022
1 parent ac5180e commit 62e0946
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.d/854.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add listuser command to Discord bot to list users from Matrix.
86 changes: 78 additions & 8 deletions src/discordcommandhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ export class DiscordCommandHandler {
permission: "MANAGE_WEBHOOKS",
run: async () => {
if (await this.discord.Provisioner.MarkApproved(chan, discordMember, true)) {
return "Thanks for your response! The matrix bridge has been approved.";
return "Thanks for your response! The Matrix bridge has been approved.";
} else {
return "Thanks for your response, however" +
" it has arrived after the deadline - sorry!";
}
},
},
ban: {
description: "Bans a user on the matrix side",
description: "Bans a user on the Matrix side",
params: ["name"],
permission: "BAN_MEMBERS",
run: this.ModerationActionGenerator(chan, "ban"),
Expand All @@ -69,36 +69,42 @@ export class DiscordCommandHandler {
permission: "MANAGE_WEBHOOKS",
run: async () => {
if (await this.discord.Provisioner.MarkApproved(chan, discordMember, false)) {
return "Thanks for your response! The matrix bridge has been declined.";
return "Thanks for your response! The Matrix bridge has been declined.";
} else {
return "Thanks for your response, however" +
" it has arrived after the deadline - sorry!";
}
},
},
kick: {
description: "Kicks a user on the matrix side",
description: "Kicks a user on the Matrix side",
params: ["name"],
permission: "KICK_MEMBERS",
run: this.ModerationActionGenerator(chan, "kick"),
},
unban: {
description: "Unbans a user on the matrix side",
description: "Unbans a user on the Matrix side",
params: ["name"],
permission: "BAN_MEMBERS",
run: this.ModerationActionGenerator(chan, "unban"),
},
unbridge: {
description: "Unbridge matrix rooms from this channel",
description: "Unbridge Matrix rooms from this channel",
params: [],
permission: ["MANAGE_WEBHOOKS", "MANAGE_CHANNELS"],
run: async () => this.UnbridgeChannel(chan),
},
listusers: {
description: "List users on the Matrix side of the bridge",
params: [],
permission: [],
run: async () => this.ListMatrixMembers(chan)
}
};

const parameters: ICommandParameters = {
name: {
description: "The display name or mxid of a matrix user",
description: "The display name or mxid of a Matrix user",
get: async (name) => {
const channelMxids = await this.discord.ChannelSyncroniser.GetRoomIdsFromChannel(msg.channel);
const mxUserId = await Util.GetMxidFromName(intent, name, channelMxids);
Expand Down Expand Up @@ -156,12 +162,76 @@ export class DiscordCommandHandler {
return "This channel has been unbridged";
} catch (err) {
if (err.message === "Channel is not bridged") {
return "This channel is not bridged to a plumbed matrix room";
return "This channel is not bridged to a plumbed Matrix room";
}
log.error("Error while unbridging room " + channel.id);
log.error(err);
return "There was an error unbridging this room. " +
"Please try again later or contact the bridge operator.";
}
}

private async ListMatrixMembers(channel: Discord.TextChannel): Promise<string> {
const chanMxids = await this.discord.ChannelSyncroniser.GetRoomIdsFromChannel(channel);
const response: string[] = [];
const errorMessages: string[] = [];

await Promise.all(chanMxids.map(async (chanMxid) => {
try {
const { underlyingClient } = this.bridge.botIntent;
const memberProfiles = await underlyingClient.getJoinedRoomMembersWithProfiles(chanMxid);

for (const key in memberProfiles) {
if (this.bridge.isNamespacedUser(key)) {
continue;
}

const memberProfile = memberProfiles[key];
const hasDisplayName = !!memberProfile.display_name;
let line: string;

if (hasDisplayName) {
line = `${memberProfile.display_name} (${key})`;
} else {
line = `${key}`;
}

response.push(`• ${line}`);
}
} catch (e) {
errorMessages.push(`Couldn't get members from ${chanMxid}`);
}
}));

if (errorMessages.length) {
const errorMessage = errorMessages.join('\n');
throw Error(errorMessage);
}

const length = response.length;
let userCount: string;

switch (length) {
case 0:
userCount = "are **no** users";
break;
case 1:
userCount = `is **1** user`;
break;
default:
const formatter = new Intl.NumberFormat('en-US');
userCount = `are **${formatter.format(length)}** users`;
}

const userCountMessage = `There ${userCount} on the Matrix side.`

if (length === 0) {
return userCountMessage;
}

const disclaimer = `Matrix users in ${channel.toString()} may not necessarily be in the other bridged channels in the server.`;
const userList = response.join('\n');

return `${userCountMessage} ${disclaimer}\n\n${userList}`;
}
}

0 comments on commit 62e0946

Please sign in to comment.