diff --git a/config/config.sample.yaml b/config/config.sample.yaml index 99a86e78f3f092807b342f1bbe17d3dcc4f92e98..5f8afd9c6d82ec142044da5c0b55642f38f26f1a 100644 --- a/config/config.sample.yaml +++ b/config/config.sample.yaml @@ -5,6 +5,7 @@ bridge: disablePresence: false disableTypingNotifications: false disableDiscordMentions: false + disableDeletionForwarding: false auth: clientID: "12345" # Get from discord secret: "blah" diff --git a/config/config.schema.yaml b/config/config.schema.yaml index 1f587bd0c9a579cbb7abe3bf39d1c94d80c225ef..bdb2bb5cc5c1bf24ccf82adeb82037c8800dc4e2 100644 --- a/config/config.schema.yaml +++ b/config/config.schema.yaml @@ -18,6 +18,8 @@ properties: type: "boolean" disableDiscordMentions: type: "boolean" + disableDeletionForwarding: + type: "boolean" auth: type: "object" required: ["botToken"] diff --git a/src/bot.ts b/src/bot.ts index 668ecde21e6022d826dbe56b7d7aec4bca71d6d0..f1b9bf7717cf1b1dd5c6bf11aa6123cc0afddbf7 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -38,8 +38,8 @@ export class DiscordBot { this.sentMessages = []; this.clientFactory = new DiscordClientFactory(store, config.auth); this.msgProcessor = new MessageProcessor( - new MessageProcessorOpts(this.config.bridge.domain), - this, + new MessageProcessorOpts(this.config.bridge.domain), + this, ); this.presenceHandler = new PresenceHandler(this); } @@ -225,7 +225,7 @@ export class DiscordBot { log.error("DiscordBot", "Couldn't send message. ", err); } if (!Array.isArray(msg)) { - msg = [msg]; + msg = [msg]; } msg.forEach((m: Discord.Message) => { log.verbose("DiscordBot", "Sent ", m); @@ -242,33 +242,36 @@ export class DiscordBot { } public async ProcessMatrixRedact(event: any) { + if (this.config.bridge.disableDeletionForwarding) { + return; + } log.info("DiscordBot", `Got redact request for ${event.redacts}`); log.verbose("DiscordBot", `Event:`, event); const storeEvent = await this.store.Get(DbEvent, {matrix_id: event.redacts + ";" + event.room_id}); if (!storeEvent.Result) { - log.warn("DiscordBot", `Could not redact because the event was not in the store.`); - return; + log.warn("DiscordBot", `Could not redact because the event was not in the store.`); + return; } log.info("DiscordBot", `Redact event matched ${storeEvent.ResultCount} entries`); while (storeEvent.Next()) { - log.info("DiscordBot", `Deleting discord msg ${storeEvent.DiscordId}`); - if (!this.bot.guilds.has(storeEvent.GuildId)) { - log.warn("DiscordBot", `Could not redact because the guild could not be found.`); - return; - } - if (!this.bot.guilds.get(storeEvent.GuildId).channels.has(storeEvent.ChannelId)) { - log.warn("DiscordBot", `Could not redact because the guild could not be found.`); - return; - } - const channel = <Discord.TextChannel> this.bot.guilds.get(storeEvent.GuildId) - .channels.get(storeEvent.ChannelId); - const msg = await channel.fetchMessage(storeEvent.DiscordId); - try { - await msg.delete(); - log.info("DiscordBot", `Deleted message`); - } catch (ex) { - log.warn("DiscordBot", `Failed to delete message`, ex); - } + log.info("DiscordBot", `Deleting discord msg ${storeEvent.DiscordId}`); + if (!this.bot.guilds.has(storeEvent.GuildId)) { + log.warn("DiscordBot", `Could not redact because the guild could not be found.`); + return; + } + if (!this.bot.guilds.get(storeEvent.GuildId).channels.has(storeEvent.ChannelId)) { + log.warn("DiscordBot", `Could not redact because the guild could not be found.`); + return; + } + const channel = <Discord.TextChannel> this.bot.guilds.get(storeEvent.GuildId) + .channels.get(storeEvent.ChannelId); + const msg = await channel.fetchMessage(storeEvent.DiscordId); + try { + await msg.delete(); + log.info("DiscordBot", `Deleted message`); + } catch (ex) { + log.warn("DiscordBot", `Failed to delete message`, ex); + } } } @@ -307,22 +310,22 @@ export class DiscordBot { } public async GetGuildEmoji(guild: Discord.Guild, id: string): Promise<string> { - const dbEmoji: DbGuildEmoji = await this.store.Get(DbGuildEmoji, {emoji_id: id}); - if (!dbEmoji.Result) { - // Fetch the emoji - if (!guild.emojis.has(id)) { - throw new Error("The guild does not contain the emoji"); - } - const emoji: Discord.Emoji = guild.emojis.get(id); - const intent = this.bridge.getIntent(); - const mxcUrl = (await Util.UploadContentFromUrl(emoji.url, intent, emoji.name)).mxcUrl; - dbEmoji.EmojiId = emoji.id; - dbEmoji.GuildId = guild.id; - dbEmoji.Name = emoji.name; - dbEmoji.MxcUrl = mxcUrl; - await this.store.Insert(dbEmoji); + const dbEmoji: DbGuildEmoji = await this.store.Get(DbGuildEmoji, {emoji_id: id}); + if (!dbEmoji.Result) { + // Fetch the emoji + if (!guild.emojis.has(id)) { + throw new Error("The guild does not contain the emoji"); } - return dbEmoji.MxcUrl; + const emoji: Discord.Emoji = guild.emojis.get(id); + const intent = this.bridge.getIntent(); + const mxcUrl = (await Util.UploadContentFromUrl(emoji.url, intent, emoji.name)).mxcUrl; + dbEmoji.EmojiId = emoji.id; + dbEmoji.GuildId = guild.id; + dbEmoji.Name = emoji.name; + dbEmoji.MxcUrl = mxcUrl; + await this.store.Insert(dbEmoji); + } + return dbEmoji.MxcUrl; } private GetFilenameForMediaEvent(content): string { @@ -578,4 +581,4 @@ export class DiscordBot { await client.redactEvent(matrixIds[1], matrixIds[0]); } } -} + } diff --git a/src/config.ts b/src/config.ts index b105e16c48affd8c71b605b29c90e0c3af40e928..cf144ca32abef2f57339e72c4430e56bde908079 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,6 +15,7 @@ class DiscordBridgeConfigBridge { public disablePresence: boolean; public disableTypingNotifications: boolean; public disableDiscordMentions: boolean; + public disableDeletionForwarding: boolean; } class DiscordBridgeConfigDatabase {