lavacord
Version:
A simple by design lavalink wrapper made for any discord library.
221 lines (218 loc) • 7.07 kB
JavaScript
'use strict';
var events = require('events');
var Rest_cjs = require('./Rest.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var Player = class extends events.EventEmitter {
/**
* Creates a new player instance.
*
* @param node - The Lavalink node this player is connected to.
* @param guildId - The guild ID that this player is associated with.
*/
constructor(node, guildId) {
super();
this.node = node;
this.guildId = guildId;
}
static {
__name(this, "Player");
}
/**
* The current state of this Player
*
* @remarks
* Contains information about the player state from Lavalink, including position, filters, etc.
*/
state = {
time: 0,
position: 0,
connected: false,
ping: 0
};
/**
* The timestamp when the current track started playing
*
* @remarks
* This is a client-side timestamp, not synchronized with Lavalink.
* Can be used to calculate approximate playback position.
*/
timestamp = null;
/**
* Whether the audio playback is currently paused
*/
paused = false;
/**
* The current volume level (0-1000)
*/
volume = 100;
/**
* The current track in Lavalink's base64 string form
*
* @remarks
* This is null when no track is loaded or when playback has ended.
*/
track = null;
/**
* The voice connection state from Lavalink API
*/
voice = null;
/**
* The current audio filters applied to this player
*
* @remarks
* This includes effects like equalizer, karaoke, etc.
*/
filters = {};
/**
* Updates the current player on the Lavalink node.
* @see {@link https://lavalink.dev/api/rest#update-player}
*
* @param options - The update options to apply to the player.
* @param noReplace - If true, the event will be dropped if there's a currently playing track.
* @returns {Promise<APIPlayer>} The updated player information from Lavalink.
*/
async update(options, noReplace = false) {
const d = await Rest_cjs.Rest.updatePlayer(this.node, this.guildId, options, noReplace);
if ("track" in d) this.track = d.track;
if ("volume" in d) this.volume = d.volume;
if ("paused" in d) this.paused = d.paused;
if ("state" in d) this.state = d.state;
if ("filters" in d) this.filters = d.filters;
if ("voice" in d) this.voice = d.voice;
return d;
}
/**
* Plays a track using its base64 encoded string.
*
* @param track - The base64 encoded track string from Lavalink.
* @param options - Additional options for playback.
* @returns A promise resolving to the updated player information.
*/
async play(track, options) {
const { userData, noReplace, ...opts } = options || {};
const d = await this.update({ track: { encoded: track, userData }, ...opts }, noReplace ?? false);
this.timestamp = Date.now();
return d;
}
/**
* Stops the currently playing track.
*
* @remarks
* This will trigger a "TrackEndEvent" with reason "STOPPED".
*
* @returns A promise resolving to the updated player information.
*/
stop() {
return this.update({ track: { encoded: null } });
}
/**
* Pauses or resumes the current track.
*
* @param pause - Whether to pause (true) or resume (false) playback.
* @returns A promise resolving to the updated player information.
*/
async pause(pause) {
const d = await this.update({ paused: pause });
if (this.listenerCount("pause")) this.emit("pause", pause);
if (this.manager.listenerCount("playerPause")) this.manager.emit("playerPause", this, pause);
return d;
}
/**
* Changes the volume of the current playback.
*
* @param volume - The volume level as a number between 0 and 1000
* @returns A promise resolving to the updated player information.
*/
async setVolume(volume) {
const d = await this.update({ volume });
if (this.listenerCount("volume")) this.emit("volume", volume);
if (this.manager.listenerCount("playerVolume")) this.manager.emit("playerVolume", this, volume);
return d;
}
/**
* Seeks to a specific position in the current track.
*
* @param position - The position to seek to in milliseconds.
* @returns A promise resolving to the updated player information.
*/
async seek(position) {
const d = await this.update({ position });
if (this.listenerCount("seek")) this.emit("seek", position);
if (this.manager.listenerCount("playerSeek")) this.manager.emit("playerSeek", this, position);
return d;
}
/**
* Applies audio filters to the current playback.
*
* @param options - The filter options to apply.
* @returns A promise resolving to the updated player information.
*/
async setFilters(options) {
const d = await this.update({ filters: options });
if (this.listenerCount("filters")) this.emit("filters", options);
if (this.manager.listenerCount("playerFilters")) this.manager.emit("playerFilters", this, options);
return d;
}
/**
* Sets the equalizer effect for the current playback.
*
* @param bands - An array of equalizer bands to adjust.
* @returns A promise resolving to the updated player information.
*
* @remarks
* Each band is an object with 'band' (0-14) and 'gain' (-0.25 to 1.0) properties.
*/
async setEqualizer(bands) {
return this.setFilters({ equalizer: bands });
}
/**
* Destroys the player on the Lavalink node.
*
* @remarks
* This sends a destroy signal to Lavalink to clean up resources for this guild ID.
* It doesn't affect the Discord voice connection - use {@link Manager.leave} for that.
*
* @returns {Promise<DestroyPlayerResult>} A promise resolving to the destroy result.
*/
async destroy() {
return Rest_cjs.Rest.destroyPlayer(this.node, this.guildId);
}
/**
* Provides voice server update information to Lavalink to establish a connection.
*
* @param data - The voice update state containing session ID and voice server information.
* @returns A promise resolving to the updated player information.
*/
connect(data) {
return this.update({
voice: {
token: data.event.token,
endpoint: data.event.endpoint,
sessionId: data.sessionId,
channelId: data.channelId
}
});
}
/**
* Switches the player to a different voice channel.
*
* @param channel - The ID of the voice channel to switch to.
* @param options - Options for joining the channel (selfMute, selfDeaf).
* @returns Does not return anything, but sends a WebSocket message to the Lavalink node.
*/
switchChannel(channel, options = {}) {
return this.manager.sendWS(this.guildId, channel, options);
}
/**
* Gets the manager instance that created this player.
*
* @returns {Manager} The manager instance.
*/
get manager() {
return this.node.manager;
}
};
exports.Player = Player;
//# sourceMappingURL=Player.cjs.map
//# sourceMappingURL=Player.cjs.map