diff --git a/src/bot.ts b/src/bot.ts index 15203be96af08399b56ca7c930092b89e63a4b93..5550402d01ee3315096799b91832c57ea6406bf0 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -548,10 +548,10 @@ export class DiscordBot { } // Check if there's an ongoing bridge request - if ((msg.content === "!approve" || msg.content === "!deny") && this.provisioner.hasPendingRequest(chan)) { + if ((msg.content === "!approve" || msg.content === "!deny") && this.provisioner.HasPendingRequest(chan)) { try { const isApproved = msg.content === "!approve"; - const successfullyBridged = await this.provisioner.markApproved(chan, msg.member, isApproved); + const successfullyBridged = await this.provisioner.MarkApproved(chan, msg.member, isApproved); if (successfullyBridged && isApproved) { msg.channel.sendMessage("Thanks for your response! The matrix bridge has been approved"); } else if (successfullyBridged && !isApproved) { diff --git a/src/discordas.ts b/src/discordas.ts index 8629fc8d545332019a9bb38581e929b1af04a133..2f49ea54f89734a25ce36816c23be0b1344df426 100644 --- a/src/discordas.ts +++ b/src/discordas.ts @@ -70,7 +70,7 @@ function run (port: number, config: DiscordBridgeConfig) { homeserverUrl: config.bridge.homeserverUrl, registration, }); - provisioner.setBridge(bridge); + provisioner.SetBridge(bridge); roomhandler.setBridge(bridge); discordbot.setBridge(bridge); log.info("discordas", "Initing bridge."); diff --git a/src/matrixroomhandler.ts b/src/matrixroomhandler.ts index 465750e461c99f250c4a06ab6dcdc1ca07ae23df..1916c53b4ad4b8584ba7fdfc07b5a186dfa22207 100644 --- a/src/matrixroomhandler.ts +++ b/src/matrixroomhandler.ts @@ -190,23 +190,23 @@ export class MatrixRoomHandler { const guildId = args[0]; const channelId = args[1]; - let channel: Discord.TextChannel = null; - return this.discord.LookupRoom(guildId, channelId).then((result) => { + try { + const discordResult = await this.discord.LookupRoom(guildId, channelId); + const channel = <Discord.TextChannel> discordResult.channel; + log.info("MatrixRoomHandler", `Bridging matrix room ${event.room_id} to ${guildId}/${channelId}`); - channel = result.channel; this.bridge.getIntent().sendMessage(event.room_id, { msgtype: "m.notice", body: "I'm asking permission from the guild administrators to make this bridge.", }); - return this.provisioner.askBridgePermission(channel, event.sender); - }).then(() => { - return this.provisioner.bridgeMatrixRoom(channel, event.room_id); - }).then(() => { + + await this.provisioner.AskBridgePermission(channel, event.sender); + await this.provisioner.BridgeMatrixRoom(channel, event.room_id); return this.bridge.getIntent().sendMessage(event.room_id, { msgtype: "m.notice", body: "I have bridged this room to your channel", }); - }).catch((err) => { + } catch (err) { if (err.message === "Timed out waiting for a response from the Discord owners" || err.message === "The bridge has been declined by the Discord guild") { return this.bridge.getIntent().sendMessage(event.room_id, { @@ -217,11 +217,11 @@ export class MatrixRoomHandler { log.error("MatrixRoomHandler", `Error bridging ${event.room_id} to ${guildId}/${channelId}`); log.error("MatrixRoomHandler", err); - this.bridge.getIntent().sendMessage(event.room_id, { + return this.bridge.getIntent().sendMessage(event.room_id, { msgtype: "m.notice", body: "There was a problem bridging that channel - has the guild owner approved the bridge?", }); - }); + } } else if (command === "unbridge") { const remoteRoom = context.rooms.remote; @@ -239,19 +239,20 @@ export class MatrixRoomHandler { }); } - this.provisioner.unbridgeRoom(remoteRoom).then(() => { - this.bridge.getIntent().sendMessage(event.room_id, { + try { + await this.provisioner.UnbridgeRoom(remoteRoom); + return this.bridge.getIntent().sendMessage(event.room_id, { msgtype: "m.notice", body: "This room has been unbridged", }); - }).catch((err) => { - log.error("MatrixRoomHandler", "Error while unbridging room " + event.room_id); - log.error("MatrixRoomHandler", err); - this.bridge.getItent().sendMessage(event.room_id, { - msgtype: "m.notice", - body: "There was an error unbridging this room. Please try again later or contact the bridge operator.", - }); - }); + } catch (err) { + log.error("MatrixRoomHandler", "Error while unbridging room " + event.room_id); + log.error("MatrixRoomHandler", err); + return this.bridge.getItent().sendMessage(event.room_id, { + msgtype: "m.notice", + body: "There was an error unbridging this room. Please try again later or contact the bridge operator.", + }); + } } else if (command === "help") { // Unknown command or no command given to get help on, so we'll just give them the help this.bridge.getIntent().sendMessage(event.room_id, { diff --git a/src/provisioner.ts b/src/provisioner.ts index 7dd965181edfb7344d4aa59a917a8365b8ba6c4e..c8a2e0d5cb559b0f79173f493058ca3979a381af 100644 --- a/src/provisioner.ts +++ b/src/provisioner.ts @@ -13,11 +13,11 @@ export class Provisioner { private bridge: Bridge; private pendingRequests: { [channelId: string]: (approved: boolean) => void } = {}; // [channelId]: resolver fn - public setBridge(bridge: Bridge): void { + public SetBridge(bridge: Bridge): void { this.bridge = bridge; } - public bridgeMatrixRoom(channel: Discord.TextChannel, roomId: string) { + public BridgeMatrixRoom(channel: Discord.TextChannel, roomId: string) { const remote = new RemoteRoom(`discord_${channel.guild.id}_${channel.id}_bridged`); remote.set("discord_type", "text"); remote.set("discord_guild", channel.guild.id); @@ -29,11 +29,11 @@ export class Provisioner { this.bridge.getRoomStore().setMatrixRoom(local); // Needs to be done after linking } - public unbridgeRoom(remoteRoom: RemoteRoom) { + public UnbridgeRoom(remoteRoom: RemoteRoom) { return this.bridge.getRoomStore().removeEntriesByRemoteRoomId(remoteRoom.getId()); } - public askBridgePermission(channel: Discord.TextChannel, requestor: string): Promise<any> { + public AskBridgePermission(channel: Discord.TextChannel, requestor: string): Promise<any> { return new Promise((resolve, reject) => { const channelId = channel.guild.id + "/" + channel.id; @@ -64,12 +64,12 @@ export class Provisioner { }); } - public hasPendingRequest(channel: Discord.TextChannel): boolean { + public HasPendingRequest(channel: Discord.TextChannel): boolean { const channelId = channel.guild.id + "/" + channel.id; return !!this.pendingRequests[channelId]; } - public markApproved(channel: Discord.TextChannel, member: Discord.GuildMember, allow: boolean): Promise<boolean> { + public MarkApproved(channel: Discord.TextChannel, member: Discord.GuildMember, allow: boolean): Promise<boolean> { const channelId = channel.guild.id + "/" + channel.id; if (!this.pendingRequests[channelId]) { return Promise.resolve(false); // no change, so false