mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-23 21:52:45 -05:00
no comment still working on it
This commit is contained in:
1 parent
cea0014e18
commit
e11fca151c
87 files changed
+5710
-10182
No files matched your search
+95
-76
@@ -1,6 +1,8 @@
|
||||
/**
|
||||
* @file Dashboard.mjs — Dashboard — web dashboard backend with player status API, guild listing, and bot statistics endpoints
|
||||
* @module src.dashboard.Dashboard
|
||||
* @module dashboard/Dashboard
|
||||
* @description Web dashboard backend controller. Manages Redis pub/sub communication
|
||||
* with the dashboard frontend, handles remote function calls (join, play, pause, etc.),
|
||||
* and provides static data-conversion helpers for Discord entities.
|
||||
*/
|
||||
|
||||
import { CommandBuilder, CommandHandler, Option } from "../CommandHandler.mjs";
|
||||
@@ -13,18 +15,23 @@ import { logger } from "../constants/Logger.mjs";
|
||||
import { iterateVoiceStates } from "../constants/VoiceStateResolver.mjs";
|
||||
|
||||
/**
|
||||
* Dashboard class.
|
||||
* @class
|
||||
* @description Orchestrates the web dashboard: wires up Redis request/response handling,
|
||||
* converts Discord data for the frontend, and exposes remote control functions.
|
||||
*/
|
||||
export class Dashboard {
|
||||
/** @type {boolean} Whether the dashboard is enabled. */
|
||||
enabled = false;
|
||||
/** @type {number} Login code expiry time in milliseconds (default 6 hours). */
|
||||
expiryTime = 1000 * 60 * 60 * 6;
|
||||
|
||||
/**
|
||||
* @param {Remix} remix
|
||||
* @param {Object} opts
|
||||
* @param {boolean} opts.enabled Whether the Dashboard is enabled and connections should be attempted
|
||||
* @param {Object} opts.redis Connection options passed directly to redis createClient
|
||||
* @param {Object} [opts.mysql] MySQL connection config for login code verification
|
||||
* Create a new Dashboard instance.
|
||||
* @param {object} remix - The main bot (Remix) instance.
|
||||
* @param {object} opts - Dashboard options.
|
||||
* @param {boolean} [opts.enabled] - Whether the dashboard is enabled.
|
||||
* @param {object} [opts.redis] - Redis connection options.
|
||||
* @param {object} [opts.mysql] - MySQL connection options for the dashboard database.
|
||||
*/
|
||||
constructor(remix, opts) {
|
||||
this.enabled = opts?.enabled;
|
||||
@@ -129,8 +136,8 @@ export class Dashboard {
|
||||
|
||||
|
||||
/**
|
||||
* Set the bot ID for multi-bot Redis namespace isolation.
|
||||
* Updates the Redis platform string so each bot uses its own channel namespace.
|
||||
* Set the bot ID for the Redis platform prefix.
|
||||
* @param {string} botId - The bot's Discord user ID.
|
||||
*/
|
||||
setBotId(botId) {
|
||||
if (!this.enabled || !this.redis) return;
|
||||
@@ -139,10 +146,13 @@ export class Dashboard {
|
||||
|
||||
|
||||
/**
|
||||
* @param {Object} params
|
||||
* @param {string} params.func
|
||||
* @param {any} params.data
|
||||
* @returns {Promise<any>}
|
||||
* Execute a remote dashboard function (e.g. join, pause, volume).
|
||||
* @async
|
||||
* @param {object} params - Function call parameters.
|
||||
* @param {string} params.func - The function name to execute.
|
||||
* @param {object} [params.data] - Additional data for the function.
|
||||
* @param {string} [params.data.user] - The Discord user ID making the request.
|
||||
* @returns {Promise<object>} Result object with `message` or `error`.
|
||||
*/
|
||||
async runFunction(params) {
|
||||
let user;
|
||||
@@ -367,10 +377,12 @@ export class Dashboard {
|
||||
|
||||
|
||||
/**
|
||||
* Check if a user is a bot owner, or is in the same voice channel as the player.
|
||||
* @param {import("@fluxerjs/core").User|null} user
|
||||
* @param {Player} player
|
||||
* @returns {string|null} Error message, or null if authorized
|
||||
* Verify that the given user is allowed to control the given player.
|
||||
* Bot owners bypass the check; otherwise the user must be in the same voice channel.
|
||||
* @private
|
||||
* @param {object} user - The Discord user attempting to control the player.
|
||||
* @param {object} player - The Player instance to control.
|
||||
* @returns {string|null} An error message if unauthorized, or null if authorized.
|
||||
*/
|
||||
_authorizePlayerControl(user, player) {
|
||||
if (user && this.remix.handler?.owners?.includes?.(user.id)) return null;
|
||||
@@ -400,10 +412,13 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the user has permission to perform an action in the target guild.
|
||||
* @param {import("@fluxerjs/core").User} user
|
||||
* @param {string} guildId
|
||||
* @returns {Promise<string|null>} Error message, or null if authorized
|
||||
* Verify that the given user is a member of the specified guild.
|
||||
* Bot owners bypass the check.
|
||||
* @private
|
||||
* @async
|
||||
* @param {object} user - The Discord user to verify.
|
||||
* @param {string} guildId - The guild ID to check membership in.
|
||||
* @returns {Promise<string|null>} An error message if not a member, or null if authorized.
|
||||
*/
|
||||
async _authorizeUserInGuild(user, guildId) {
|
||||
if (this.remix.handler?.owners?.includes?.(user.id)) return null;
|
||||
@@ -420,9 +435,10 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a player by its channel ID (key in playerMap)
|
||||
* @param {string} id
|
||||
* @returns {Player|null}
|
||||
* Look up a player by channel ID, guild ID, or player ID.
|
||||
* @private
|
||||
* @param {string} id - Channel ID, guild ID, or player ID.
|
||||
* @returns {object|null} The matching Player instance, or null.
|
||||
*/
|
||||
_getPlayerById(id) {
|
||||
return this.remix.players.playerMap.get(id)
|
||||
@@ -434,7 +450,10 @@ export class Dashboard {
|
||||
|
||||
|
||||
/**
|
||||
* @param {import("@fluxerjs/core").User} user
|
||||
* Convert a Discord user object to a dashboard-safe plain object.
|
||||
* @static
|
||||
* @param {object} user - The Discord user.
|
||||
* @returns {{ id: string, username: string, displayName: string, avatar: { url: string } }}
|
||||
*/
|
||||
static convertUser(user) {
|
||||
let avatarUrl = "";
|
||||
@@ -459,7 +478,10 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} vid Track object stored in the fluxer Queue
|
||||
* Convert a video/track object to a dashboard-safe plain object.
|
||||
* @static
|
||||
* @param {object} vid - The video/track object from the player queue.
|
||||
* @returns {object|null} Sanitised video data, or null if input is falsy.
|
||||
*/
|
||||
static convertVideo(vid) {
|
||||
if (!vid) return null;
|
||||
@@ -483,7 +505,11 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("@fluxerjs/core").GuildChannel} channel
|
||||
* Convert a Discord channel to a dashboard-safe plain object, including
|
||||
* voice participant info for voice channels.
|
||||
* @static
|
||||
* @param {object} channel - The Discord channel.
|
||||
* @returns {object} Sanitised channel data.
|
||||
*/
|
||||
static convertChannel(channel) {
|
||||
const type = channel.type ?? 0;
|
||||
@@ -524,7 +550,10 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("@fluxerjs/core").Guild} guild
|
||||
* Convert a Discord guild (server) to a dashboard-safe plain object with channels.
|
||||
* @static
|
||||
* @param {object} guild - The Discord guild.
|
||||
* @returns {object} Sanitised server data including channels and voice channels.
|
||||
*/
|
||||
static convertServer(guild) {
|
||||
const channelStore = guild.channels;
|
||||
@@ -558,8 +587,10 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Lightweight server summary for player payloads.
|
||||
* @param {import("@fluxerjs/core").Guild} guild
|
||||
* Convert a Discord guild to a lightweight summary object (no channels).
|
||||
* @static
|
||||
* @param {object} guild - The Discord guild.
|
||||
* @returns {{ name: string, id: string, icon: string|null, description: string|null, ownerId: string|null }}
|
||||
*/
|
||||
static convertServerSummary(guild) {
|
||||
let iconUrl = null;
|
||||
@@ -578,7 +609,11 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Player} player
|
||||
* Convert a Player instance to a dashboard-safe serialisable object.
|
||||
* Includes queue, users, channel, and server info.
|
||||
* @static
|
||||
* @param {object} player - The Player instance.
|
||||
* @returns {object} Sanitised player data.
|
||||
*/
|
||||
static convertPlayer(player) {
|
||||
const channelId = player._channelId;
|
||||
@@ -648,7 +683,10 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Option} opt
|
||||
* Convert a command option to a dashboard-safe plain object.
|
||||
* @static
|
||||
* @param {object} opt - The command option.
|
||||
* @returns {object} Sanitised option data.
|
||||
*/
|
||||
static convertOption(opt) {
|
||||
return {
|
||||
@@ -664,8 +702,11 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CommandBuilder} com
|
||||
* @param {CommandHandler} commands
|
||||
* Convert a command (and its subcommands/options) to a dashboard-safe plain object.
|
||||
* @static
|
||||
* @param {object} com - The command object.
|
||||
* @param {object} commands - The CommandHandler instance (for usage generation).
|
||||
* @returns {object} Sanitised command data.
|
||||
*/
|
||||
static convertCommand(com, commands) {
|
||||
return {
|
||||
@@ -684,23 +725,14 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
|
||||
/** @private */
|
||||
_playerUpdateTimers = new Map();
|
||||
|
||||
/**
|
||||
* Global player update (broadcast to all dashboard listeners on {platform}:players).
|
||||
*
|
||||
* Used for two purposes:
|
||||
* 1. Lifecycle events: { type: "init" } / { type: "close" } — the backend
|
||||
* PlayerManager uses these to add/remove Player objects.
|
||||
* 2. Full state broadcast: any other type — the full serialised player is
|
||||
* included so dashboard frontends can update their UI.
|
||||
*
|
||||
* Debounced: rapid successive calls for the same player are coalesced
|
||||
* into a single publish every 500ms.
|
||||
*
|
||||
* @param {Object} details Must include a `type` field (e.g. "init", "close",
|
||||
* "startplay", "stopplay", etc.) and may include a `data` field.
|
||||
* @param {Player} player
|
||||
* Broadcast a player state update to the dashboard via Redis.
|
||||
* Debounces non-init/close updates by 500ms.
|
||||
* @param {object} details - The update payload (e.g. `{ type: 'song' }`).
|
||||
* @param {object} player - The Player instance that changed.
|
||||
*/
|
||||
playerUpdate(details, player) {
|
||||
if (!this.enabled) return;
|
||||
@@ -738,20 +770,9 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-player channel update (sent to {platform}:player_{channelId}).
|
||||
*
|
||||
* The backend PlayerManager.setupEvents() expects messages in the format:
|
||||
* { type: "<eventType>", data: <payload> }
|
||||
*
|
||||
* This method accepts details in that standard format directly.
|
||||
*
|
||||
* @param {Object} details Must include `type` and optionally `data`.
|
||||
* Examples:
|
||||
* { type: "startplay", data: convertVideo(song) }
|
||||
* { type: "pause", data: { elapsedTime: 12345 } }
|
||||
* { type: "queue", data: serialisedQueueEvent }
|
||||
* { type: "join", data: userId }
|
||||
* @param {Player} player
|
||||
* Send a lightweight update to the player-specific Redis channel.
|
||||
* @param {object} details - The update payload.
|
||||
* @param {object} player - The Player instance.
|
||||
*/
|
||||
updatePlayer(details, player) {
|
||||
if (!this.enabled) return;
|
||||
@@ -760,9 +781,9 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Global user update (sent to {platform}:users).
|
||||
* @param {Object} details
|
||||
* @param {import("@fluxerjs/core").User} user
|
||||
* Broadcast a user update to the global users Redis channel.
|
||||
* @param {object} details - The update payload.
|
||||
* @param {object} user - The Discord user.
|
||||
*/
|
||||
userUpdate(details, user) {
|
||||
if (!this.enabled) return;
|
||||
@@ -774,9 +795,9 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-user channel update (sent to {platform}:user_{userId}).
|
||||
* @param {Object} details
|
||||
* @param {import("@fluxerjs/core").User} user
|
||||
* Send a lightweight update to the user-specific Redis channel.
|
||||
* @param {object} details - The update payload.
|
||||
* @param {object} user - The Discord user.
|
||||
*/
|
||||
updateUser(details, user) {
|
||||
if (!this.enabled) return;
|
||||
@@ -786,13 +807,11 @@ export class Dashboard {
|
||||
|
||||
|
||||
/**
|
||||
* Verify a login code submitted by a user via DM.
|
||||
* The backend stores the code in MySQL; this method compares the supplied
|
||||
* code against the stored (bcrypt-hashed) value.
|
||||
*
|
||||
* @param {string} user User ID
|
||||
* @param {string} code Plain-text code supplied by the user
|
||||
* @returns {Promise<string|null>} null on success, error message on failure
|
||||
* Verify a dashboard login code for a user.
|
||||
* @async
|
||||
* @param {string} user - The Discord user ID.
|
||||
* @param {string} code - The login code to verify.
|
||||
* @returns {Promise<string|null>} An error message if verification fails, or null on success.
|
||||
*/
|
||||
async confirmLogin(user, code) {
|
||||
if (!this.enabled) return "Dashboard not enabled.";
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* @file DatabaseManager.mjs — DatabaseManager — SQLite database manager for the web dashboard's persistent storage
|
||||
* @module src.dashboard.DatabaseManager
|
||||
* @module dashboard/DatabaseManager
|
||||
* @description MySQL connection pool wrapper for the dashboard. Provides parameterised
|
||||
* query execution and bcrypt hashing utilities for login code management.
|
||||
*/
|
||||
|
||||
import { compare, genSalt, hash } from "bcryptjs";
|
||||
@@ -8,12 +9,14 @@ import { createPool } from "mysql2/promise";
|
||||
import { logger } from "../constants/Logger.mjs";
|
||||
|
||||
/**
|
||||
* DatabaseManager — MySQL pool for dashboard login codes and API tokens.
|
||||
* Adapted for use with the Fluxer bot.
|
||||
* @class
|
||||
* @description Wraps a mysql2 connection pool with convenience methods for
|
||||
* parameterised queries and bcrypt hash operations.
|
||||
*/
|
||||
export class DatabaseManager {
|
||||
/**
|
||||
* @param {import("mysql2/promise").PoolOptions} config
|
||||
* Create a new DatabaseManager.
|
||||
* @param {object} config - mysql2 pool configuration (host, user, password, database, etc.).
|
||||
*/
|
||||
constructor(config) {
|
||||
this.db = createPool({
|
||||
@@ -26,19 +29,21 @@ export class DatabaseManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a raw SQL query.
|
||||
* @param {string} query
|
||||
* @returns {Promise<[import("mysql2/promise").QueryResult, import("mysql2/promise").FieldPacket[]]>}
|
||||
* Execute a raw SQL query (no parameter binding).
|
||||
* @async
|
||||
* @param {string} query - The SQL query string.
|
||||
* @returns {Promise<Array>} The query result rows and fields.
|
||||
*/
|
||||
async query(query) {
|
||||
return this.db.query(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a parameterized SQL query.
|
||||
* @param {string} query
|
||||
* @param {any[]} [data]
|
||||
* @returns {Promise<import("mysql2/promise").QueryResult>}
|
||||
* Execute a parameterised SQL query.
|
||||
* @async
|
||||
* @param {string} query - The SQL query with `?` placeholders.
|
||||
* @param {Array} data - The values to bind.
|
||||
* @returns {Promise<Array>} The result rows.
|
||||
*/
|
||||
async execute(query, data) {
|
||||
const [res, _fields] = await this.db.execute(query, data);
|
||||
@@ -46,9 +51,10 @@ export class DatabaseManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash a plaintext string using bcrypt.
|
||||
* @param {string} plain
|
||||
* @returns {Promise<string>}
|
||||
* Generate a bcrypt hash of the given plaintext.
|
||||
* @async
|
||||
* @param {string} plain - The plaintext string.
|
||||
* @returns {Promise<string>} The hashed string.
|
||||
*/
|
||||
async hash(plain) {
|
||||
const salt = await genSalt(10);
|
||||
@@ -57,16 +63,17 @@ export class DatabaseManager {
|
||||
|
||||
/**
|
||||
* Compare a plaintext string against a bcrypt hash.
|
||||
* @param {string} plain
|
||||
* @param {string} hashed
|
||||
* @returns {Promise<boolean>}
|
||||
* @async
|
||||
* @param {string} plain - The plaintext string.
|
||||
* @param {string} hashed - The bcrypt hash.
|
||||
* @returns {Promise<boolean>} True if the plaintext matches the hash.
|
||||
*/
|
||||
async compareHash(plain, hashed) {
|
||||
return await compare(plain, hashed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully close the connection pool.
|
||||
* Close the underlying MySQL connection pool.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
close() {
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
/**
|
||||
* @file RedisHandler.mjs — RedisHandler — Redis pub/sub handler for cross-process dashboard state synchronization
|
||||
* @module src.dashboard.RedisHandler
|
||||
* @module dashboard/RedisHandler
|
||||
* @description Redis pub/sub handler for the dashboard. Maintains a main publisher client
|
||||
* and a subscriber client. Receives JSON requests on the "request" channel,
|
||||
* dispatches them to a user-supplied handler, and publishes results on "response".
|
||||
*/
|
||||
|
||||
import { createClient } from "redis";
|
||||
import { logger } from "../constants/Logger.mjs";
|
||||
|
||||
/**
|
||||
* Default retry strategy: exponential backoff starting at 500ms, max 5s, max 20 retries.
|
||||
* @param {Object} options
|
||||
* @returns {number} Delay in ms before next retry, or an Error to stop retrying
|
||||
* Default reconnection strategy for Redis sockets.
|
||||
* @param {object} options - Reconnection options from the Redis client.
|
||||
* @param {number} options.totalRetryTime - Cumulative retry time so far.
|
||||
* @param {number} options.attempt - Current retry attempt number.
|
||||
* @returns {number|Error} Delay in ms before next retry, or an Error to stop retrying.
|
||||
*/
|
||||
const DEFAULT_RETRY_STRATEGY = (options) => {
|
||||
if (options.totalRetryTime > 60_000) {
|
||||
@@ -19,22 +23,25 @@ const DEFAULT_RETRY_STRATEGY = (options) => {
|
||||
};
|
||||
|
||||
/**
|
||||
* RedisHandler class.
|
||||
* @class
|
||||
* @description Manages dual Redis connections (publisher + subscriber) for real-time
|
||||
* dashboard communication via pub/sub.
|
||||
*/
|
||||
export class RedisHandler {
|
||||
/** @type {string} Platform prefix used for Redis channel names. */
|
||||
platform = "fluxer";
|
||||
|
||||
/** @type {import("redis").RedisClientType|null} */
|
||||
/** @type {import('redis').RedisClientType|null} The main (publisher) Redis client. */
|
||||
client = null;
|
||||
/** @type {import("redis").RedisClientType|null} */
|
||||
/** @type {import('redis').RedisClientType|null} The subscriber Redis client. */
|
||||
subscriber = null;
|
||||
/** Whether the handler has been explicitly destroyed */
|
||||
/** @private @type {boolean} Whether the handler has been destroyed. */
|
||||
_destroyed = false;
|
||||
|
||||
/**
|
||||
* @param {Object} opts
|
||||
* @param {import("redis").RedisClientOptions} opts Redis client options
|
||||
* @param {string} [opts.platform] Platform identifier for channel namespacing
|
||||
* Create a new RedisHandler and immediately begin connecting.
|
||||
* @param {object} [opts={}] - Redis connection options (passed to `createClient`).
|
||||
* @param {string} [opts.platform] - Platform prefix for channels.
|
||||
*/
|
||||
constructor(opts = {}) {
|
||||
this.platform = opts.platform ?? "fluxer";
|
||||
@@ -61,8 +68,10 @@ export class RedisHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect both publisher and subscriber with reconnection support.
|
||||
* Wrapped in a method so it can be called once from the constructor.
|
||||
* Connect both the publisher and subscriber clients, then subscribe
|
||||
* to the "request" and "info" channels and start the ping interval.
|
||||
* @private
|
||||
* @async
|
||||
*/
|
||||
async _connect() {
|
||||
try {
|
||||
@@ -113,12 +122,8 @@ export class RedisHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the "connected" announcement to the backend.
|
||||
* Delayed by 5 seconds so the bot has time to initialise players,
|
||||
* recovery manager, and Moonlink before the backend starts sending
|
||||
* requests like fetchPlayers. Without this delay the backend's
|
||||
* initChannels() fires before the bot's playerMap is populated,
|
||||
* causing "Redis request timed out" errors and a retry loop.
|
||||
* Send a "connected" info message to the dashboard after a 5-second debounce.
|
||||
* @returns {void}
|
||||
*/
|
||||
readyMessage() {
|
||||
if (this._destroyed) return;
|
||||
@@ -135,9 +140,10 @@ export class RedisHandler {
|
||||
|
||||
/**
|
||||
* Publish a message to a Redis channel.
|
||||
* @param {string} channel
|
||||
* @param {string} message
|
||||
* @returns {Promise<number>} Number of subscribers that received the message
|
||||
* No-op if the handler is destroyed or the client is not ready.
|
||||
* @param {string} channel - The Redis channel to publish to.
|
||||
* @param {string} message - The message string to send.
|
||||
* @returns {Promise<number>} Number of subscribers that received the message.
|
||||
*/
|
||||
send(channel, message) {
|
||||
if (this._destroyed || !this.client?.isReady) return Promise.resolve(0);
|
||||
@@ -147,24 +153,20 @@ export class RedisHandler {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @callback RequestCallback
|
||||
* @param {Object} data
|
||||
* @param {string} data.type
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
/** @type {Function|null} The current request handler function. */
|
||||
handleRequest;
|
||||
|
||||
/**
|
||||
* @param {RequestCallback} handler
|
||||
* Set the handler function invoked for incoming "request" payloads.
|
||||
* @param {Function} handler - Async function `(data) => result`.
|
||||
*/
|
||||
setRequestHandler(handler) {
|
||||
this.handleRequest = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully close both Redis connections.
|
||||
* Should be called on process shutdown to avoid abrupt connection drops.
|
||||
* Gracefully close both Redis connections and stop all internal timers.
|
||||
* @async
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async destroy() {
|
||||
|
||||
Reference in new issue
Block a user