From ac81b6a10339f1d3b45b6d4baf74127d3a86c748 Mon Sep 17 00:00:00 2001 From: Alyxia Sother Date: Fri, 21 May 2021 11:54:20 +0000 Subject: [PATCH] WIP: Make Lavalink optional At the moment, there's a broken instance of the Lavalink system running. When executing `.play`, our home-hosted `musicbot` will start playing, but the production TravBot instance will error. I haven't implemented this as a choice in the setup yet, that's for another time. Right now, all I need is for it to be optional. --- src/modules/lavalink.ts | 80 +++++++++++++++++++++-------------------- src/structures.ts | 2 ++ 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/modules/lavalink.ts b/src/modules/lavalink.ts index 3a81f02..5eecaf3 100644 --- a/src/modules/lavalink.ts +++ b/src/modules/lavalink.ts @@ -2,48 +2,50 @@ import attachClientToLavalink from "discord.js-lavalink-lib"; import {Config} from "../structures"; import {client} from "../index"; -// Although the example showed to do "client.music = LavaLink(...)" and "(client as any).music = Lavalink(...)" was done to match that, nowhere in the library is client.music ever actually used nor does the function return anything. In other words, client.music is undefined and is never used. -attachClientToLavalink(client, { - lavalink: { - restnode: { - host: "localhost", - port: 2333, - password: "youshallnotpass" - }, - nodes: [ - { +if (Config.lavalink) { + // Although the example showed to do "client.music = LavaLink(...)" and "(client as any).music = Lavalink(...)" was done to match that, nowhere in the library is client.music ever actually used nor does the function return anything. In other words, client.music is undefined and is never used. + attachClientToLavalink(client, { + lavalink: { + restnode: { host: "localhost", port: 2333, password: "youshallnotpass" - } - ] - }, - prefix: Config.prefix, - helpCmd: "mhelp", - admins: ["717352467280691331"] -}); + }, + nodes: [ + { + host: "localhost", + port: 2333, + password: "youshallnotpass" + } + ] + }, + prefix: Config.prefix, + helpCmd: "mhelp", + admins: ["717352467280691331"] + }); -// Disable the unhandledRejection listener by Lavalink because it captures every single unhandled -// rejection and adds its message with it. Then replace it with a better, more selective error handler. -for (const listener of process.listeners("unhandledRejection")) { - if (listener.toString().includes("discord.js-lavalink-musicbot")) { - process.off("unhandledRejection", listener); - } -} - -process.on("unhandledRejection", (reason: any) => { - if (reason?.code === "ECONNREFUSED") { - // This is console.warn instead of console.error because on development environments, unless Lavalink is being tested, it won't interfere with the bot's functionality. - console.warn( - `[discord.js-lavalink-musicbot] Caught unhandled rejection: ${reason.stack}\nIf this is causing issues, head to the support server at https://discord.gg/dNN4azK` - ); - } -}); - -// It's unsafe to process uncaughtException because after an uncaught exception, the system -// becomes corrupted. So disable Lavalink from adding a hook to it. -for (const listener of process.listeners("uncaughtException")) { - if (listener.toString().includes("discord.js-lavalink-musicbot")) { - process.off("uncaughtException", listener); + // Disable the unhandledRejection listener by Lavalink because it captures every single unhandled + // rejection and adds its message with it. Then replace it with a better, more selective error handler. + for (const listener of process.listeners("unhandledRejection")) { + if (listener.toString().includes("discord.js-lavalink-musicbot")) { + process.off("unhandledRejection", listener); + } + } + + process.on("unhandledRejection", (reason: any) => { + if (reason?.code === "ECONNREFUSED") { + // This is console.warn instead of console.error because on development environments, unless Lavalink is being tested, it won't interfere with the bot's functionality. + console.warn( + `[discord.js-lavalink-musicbot] Caught unhandled rejection: ${reason.stack}\nIf this is causing issues, head to the support server at https://discord.gg/dNN4azK` + ); + } + }); + + // It's unsafe to process uncaughtException because after an uncaught exception, the system + // becomes corrupted. So disable Lavalink from adding a hook to it. + for (const listener of process.listeners("uncaughtException")) { + if (listener.toString().includes("discord.js-lavalink-musicbot")) { + process.off("uncaughtException", listener); + } } } diff --git a/src/structures.ts b/src/structures.ts index f40fb29..d7b3bc6 100644 --- a/src/structures.ts +++ b/src/structures.ts @@ -13,6 +13,7 @@ class ConfigStructure extends GenericStructure { public owner: string; public admins: string[]; public support: string[]; + public lavalink: boolean | null; public systemLogsChannel: string | null; public webhooks: {[id: string]: string}; // id-token pairs @@ -23,6 +24,7 @@ class ConfigStructure extends GenericStructure { this.owner = select(data.owner, "", String); this.admins = select(data.admins, [], String, true); this.support = select(data.support, [], String, true); + this.lavalink = select(data.lavalink, null, Boolean); this.systemLogsChannel = select(data.systemLogsChannel, null, String); this.webhooks = {};