Skip to content
Extraits de code Groupes Projets
Valider 09a36313 rédigé par Tadeusz Sośnierz's avatar Tadeusz Sośnierz
Parcourir les fichiers

Notify a configurable bridge admin if the Discord token is invalid on startup

parent 732be164
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -38,6 +38,8 @@ bridge: ...@@ -38,6 +38,8 @@ bridge:
disableInviteNotifications: false disableInviteNotifications: false
# Auto-determine the language of code blocks (this can be CPU-intensive) # Auto-determine the language of code blocks (this can be CPU-intensive)
determineCodeLanguage: false determineCodeLanguage: false
# MXID of an admin user that will be PMd if the bridge experiences problems. Optional
adminMxid: '@admin:localhost'
# Authentication configuration for the discord bot. # Authentication configuration for the discord bot.
auth: auth:
# This MUST be a string (wrapped in quotes) # This MUST be a string (wrapped in quotes)
...@@ -113,4 +115,4 @@ ghosts: ...@@ -113,4 +115,4 @@ ghosts:
metrics: metrics:
enable: false enable: false
port: 9001 port: 9001
host: "127.0.0.1" host: "127.0.0.1"
\ No newline at end of file
...@@ -100,6 +100,7 @@ class DiscordBridgeConfigBridge { ...@@ -100,6 +100,7 @@ class DiscordBridgeConfigBridge {
public determineCodeLanguage: boolean = false; public determineCodeLanguage: boolean = false;
public activityTracker: UserActivityTrackerConfig = UserActivityTrackerConfig.DEFAULT; public activityTracker: UserActivityTrackerConfig = UserActivityTrackerConfig.DEFAULT;
public userLimit: number|null = null; public userLimit: number|null = null;
public adminMxid: string|null = null;
} }
export class DiscordBridgeConfigDatabase { export class DiscordBridgeConfigDatabase {
......
...@@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { Appservice, IAppserviceRegistration, LogService } from "matrix-bot-sdk"; import { Appservice, IAppserviceRegistration, LogService, MatrixClient } from "matrix-bot-sdk";
import * as yaml from "js-yaml"; import * as yaml from "js-yaml";
import * as fs from "fs"; import * as fs from "fs";
import { DiscordBridgeConfig } from "./config"; import { DiscordBridgeConfig } from "./config";
...@@ -30,6 +30,8 @@ import { Response } from "express"; ...@@ -30,6 +30,8 @@ import { Response } from "express";
const log = new Log("DiscordAS"); const log = new Log("DiscordAS");
const ADMIN_ROOMS_KEY = 'uk.half-shot.discord.admin-rooms';
const commandOptions = [ const commandOptions = [
{ name: "config", alias: "c", type: String }, { name: "config", alias: "c", type: String },
{ name: "url", alias: "u", type: String }, { name: "url", alias: "u", type: String },
...@@ -215,7 +217,7 @@ async function run(): Promise<void> { ...@@ -215,7 +217,7 @@ async function run(): Promise<void> {
roomhandler.bindThirdparty(); roomhandler.bindThirdparty();
try { try {
await startDiscordBot(discordbot); await startDiscordBot(discordbot, appservice.botClient, config.bridge.adminMxid);
log.info("Discordbot started successfully"); log.info("Discordbot started successfully");
} catch (err) { } catch (err) {
log.error(err); log.error(err);
...@@ -228,16 +230,49 @@ async function run(): Promise<void> { ...@@ -228,16 +230,49 @@ async function run(): Promise<void> {
} }
async function startDiscordBot(discordbot: DiscordBot, falloffSeconds = 5) { interface AdminRooms {
[mxid: string]: string;
}
async function notifyBridgeAdmin(client: MatrixClient, adminMxid: string, message: string) {
log.verbose(`Looking up admin room for ${adminMxid}`);
let adminRooms = await client.getAccountData<AdminRooms>(ADMIN_ROOMS_KEY).catch(() => null) || {};
if (!adminRooms[adminMxid]) {
log.verbose(`Creating an admin room for ${adminMxid}`);
adminRooms[adminMxid] = await client.createRoom();
await client.inviteUser(adminMxid, adminRooms[adminMxid]);
await client.setAccountData(ADMIN_ROOMS_KEY, adminRooms);
}
await client.sendText(adminRooms[adminMxid], message)
}
let adminNotified = false;
async function startDiscordBot(
discordbot: DiscordBot,
client: MatrixClient,
adminMxid: string|null,
falloffSeconds = 5
) {
try { try {
await discordbot.init(); await discordbot.init();
await discordbot.run(); await discordbot.run();
} catch (err) { } 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`);
adminNotified = true;
}
// no more than 5 minutes // no more than 5 minutes
const newFalloffSeconds = Math.min(falloffSeconds * 2, 5 * 60); const newFalloffSeconds = Math.min(falloffSeconds * 2, 5 * 60);
log.error(`Failed do start Discordbot: ${err.code}. Will try again in ${newFalloffSeconds} seconds`); log.error(`Failed do start Discordbot: ${err.code}. Will try again in ${newFalloffSeconds} seconds`);
await new Promise((r, _) => setTimeout(r, newFalloffSeconds * 1000)); await new Promise((r, _) => setTimeout(r, newFalloffSeconds * 1000));
return startDiscordBot(discordbot, newFalloffSeconds); return startDiscordBot(discordbot, client, adminMxid, newFalloffSeconds);
}
if (adminMxid && adminNotified) {
await notifyBridgeAdmin(client, adminMxid, `The token situation is now resolved and the bridge is running correctly`);
adminNotified = false;
} }
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter