From 744856c0571cb710b14117f350cc35d1ec7bdbce Mon Sep 17 00:00:00 2001 From: Travis Ralston <travpc@gmail.com> Date: Sun, 25 Feb 2018 11:16:03 -0700 Subject: [PATCH] Make public methods capitalized; Await all the things --- src/bot.ts | 4 ++-- src/discordas.ts | 2 +- src/matrixroomhandler.ts | 41 ++++++++++++++++++++-------------------- src/provisioner.ts | 12 ++++++------ 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 15203be..5550402 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 8629fc8..2f49ea5 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 465750e..1916c53 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 7dd9651..c8a2e0d 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 -- GitLab