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.
This commit is contained in:
Alyxia Sother 2021-05-21 11:54:20 +00:00
parent 1e673a3969
commit ac81b6a103
No known key found for this signature in database
GPG Key ID: 355968D14144B739
2 changed files with 43 additions and 39 deletions

View File

@ -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);
}
}
}

View File

@ -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 = {};