From ce107c5210ddee55d3d96964066d328069804430 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Tue, 3 Nov 2020 17:15:38 +0000 Subject: [PATCH] small tweaks --- src/bot.ts | 5 +++-- src/clientfactory.ts | 8 ++++++++ src/matrixeventprocessor.ts | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index ce138ac0..65e60a16 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -251,6 +251,7 @@ export class DiscordBot { }); client.on("message", async (msg: Discord.Message) => { try { + log.verbose(`Got incoming msg i:${msg.id} c:${msg.channel.id} g:${msg.guild?.id}`); MetricPeg.get.registerRequest(msg.id); await this.channelLock.wait(msg.channel.id); this.clientFactory.bindMetricsToChannel(msg.channel as Discord.TextChannel); @@ -382,7 +383,7 @@ export class DiscordBot { this.ClientFactory.bindMetricsToChannel(channel as Discord.TextChannel); const lookupResult = new ChannelLookupResult(); lookupResult.channel = channel as Discord.TextChannel; - lookupResult.botUser = this.BotUserId === client.user?.id; + lookupResult.botUser = this.bot.user?.id === client.user?.id; lookupResult.canSendEmbeds = client.user?.bot || false; // only bots can send embeds return lookupResult; } @@ -898,7 +899,7 @@ export class DiscordBot { return; // Skip *our* messages } const chan = msg.channel as Discord.TextChannel; - if (msg.author.id === this.BotUserId) { + if (msg.author.id === this.bot.user?.id) { // We don't support double bridging. log.verbose("Not reflecting bot's own messages"); MetricPeg.get.requestOutcome(msg.id, true, "dropped"); diff --git a/src/clientfactory.ts b/src/clientfactory.ts index 0289bc38..149e562b 100644 --- a/src/clientfactory.ts +++ b/src/clientfactory.ts @@ -44,8 +44,16 @@ export class DiscordClientFactory { messageCacheLifetime: 5, }); + const waitPromise = new Promise((resolve, reject) => { + this.botClient.once("shardReady", resolve); + this.botClient.once("shardError", reject); + }); + try { await this.botClient.login(this.config.botToken, true); + log.info("Waiting for shardReady signal"); + await waitPromise; + log.info("Got shardReady signal"); } catch (err) { log.error("Could not login as the bot user. This is bad!", err); throw err; diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index c3df5ffe..3a0af954 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -223,7 +223,6 @@ export class MatrixEventProcessor { public async ProcessStateEvent(event: IMatrixEvent) { log.verbose(`Got state event from ${event.room_id} ${event.type}`); - const channel = await this.discord.GetChannelFromRoomId(event.room_id) as Discord.TextChannel; const SUPPORTED_EVENTS = ["m.room.member", "m.room.name", "m.room.topic"]; if (!SUPPORTED_EVENTS.includes(event.type)) { @@ -279,6 +278,7 @@ export class MatrixEventProcessor { } msg += " on Matrix."; + const channel = await this.discord.GetChannelFromRoomId(event.room_id) as Discord.TextChannel; await this.discord.sendAsBot(msg, channel, event); await this.sendReadReceipt(event); }