diff --git a/config/config.sample.yaml b/config/config.sample.yaml index 090b5998a5684d50189d87df721572613873abe9..1baa13ea20cf4b8bc09e46122d94388af0719be8 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 772e73092a66e77f324e375eb592b1139eccfdb4..96ebf97a003f4219e33822cee0dc4771c347967b 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 1f020159e12eb1a4a64c682c7e2d69b04d7b69c4..f3996ed0741a988b80a27742037df7a1246439e0 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) {