From a7adca977b01354472023c9f6b214303ad3c3d1c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tadeusz=20So=C5=9Bnierz?= <tadeusz@sosnierz.com>
Date: Fri, 12 Nov 2021 16:08:02 +0100
Subject: [PATCH] Make invalid token message configurable

---
 config/config.sample.yaml |  2 ++
 src/config.ts             |  1 +
 src/discordas.ts          | 14 ++++++++------
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/config/config.sample.yaml b/config/config.sample.yaml
index 090b599..1baa13e 100644
--- a/config/config.sample.yaml
+++ b/config/config.sample.yaml
@@ -40,6 +40,8 @@ bridge:
   determineCodeLanguage: false
   # MXID of an admin user that will be PMd if the bridge experiences problems. Optional
   adminMxid: '@admin:localhost'
+  # The message to send to the bridge admin if the Discord token is not valid
+  invalidTokenMessage: 'Your Discord bot token seems to be invalid, and the bridge cannot function. Please update it in your bridge settings and restart the bridge'
 # Authentication configuration for the discord bot.
 auth:
   # This MUST be a string (wrapped in quotes)
diff --git a/src/config.ts b/src/config.ts
index 772e730..96ebf97 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -101,6 +101,7 @@ class DiscordBridgeConfigBridge {
     public activityTracker: UserActivityTrackerConfig = UserActivityTrackerConfig.DEFAULT;
     public userLimit: number|null = null;
     public adminMxid: string|null = null;
+    public invalidTokenMessage: string = 'Your Discord token is invalid';
 }
 
 export class DiscordBridgeConfigDatabase {
diff --git a/src/discordas.ts b/src/discordas.ts
index 1f02015..f3996ed 100644
--- a/src/discordas.ts
+++ b/src/discordas.ts
@@ -215,7 +215,7 @@ async function run(): Promise<void> {
     roomhandler.bindThirdparty();
 
     try {
-        await startDiscordBot(discordbot, appservice.botClient, config.bridge.adminMxid);
+        await startDiscordBot(discordbot, appservice.botClient, config);
         log.info("Discordbot started successfully");
     } catch (err) {
         log.error(err);
@@ -271,17 +271,19 @@ async function notifyBridgeAdmin(client: MatrixClient, adminMxid: string, messag
 let adminNotified = false;
 
 async function startDiscordBot(
-    discordbot: DiscordBot,
-    client: MatrixClient,
-    adminMxid: string|null,
+    discordbot:    DiscordBot,
+    client:        MatrixClient,
+    config:        DiscordBridgeConfig,
     falloffSeconds = 5
 ) {
+    const adminMxid = config.bridge.adminMxid;
+
     try {
         await discordbot.init();
         await discordbot.run();
     } catch (err) {
         if (err.code === 'TOKEN_INVALID' && adminMxid && !adminNotified) {
-            await notifyBridgeAdmin(client, adminMxid, `Your Discord bot token seems to be invalid, and the bridge cannot function. Please update it in your bridge settings and restart the bridge`);
+            await notifyBridgeAdmin(client, adminMxid, config.bridge.invalidTokenMessage);
             adminNotified = true;
         }
 
@@ -289,7 +291,7 @@ async function startDiscordBot(
         const newFalloffSeconds = Math.min(falloffSeconds * 2, 5 * 60);
         log.error(`Failed do start Discordbot: ${err.code}. Will try again in ${newFalloffSeconds} seconds`);
         await new Promise((r, _) => setTimeout(r, newFalloffSeconds * 1000));
-        return startDiscordBot(discordbot, client, adminMxid, newFalloffSeconds);
+        return startDiscordBot(discordbot, client, config, newFalloffSeconds);
     }
 
     if (adminMxid && adminNotified) {
-- 
GitLab