Skip to content

Commit

Permalink
feat: add defaultVolume & loop to createConnection, Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
UnschooledGamer committed Jul 25, 2024
1 parent f4d1637 commit 817de4c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
43 changes: 35 additions & 8 deletions build/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export interface PlayerOptions {
voiceChannel?: string;
deaf?: boolean;
mute?: boolean;
volume?: number;
loop?: string;
defaultVolume?: number;
loop?: LoopOption;
}

export type LoopOption = "none" | "track" | "queue";
Expand Down Expand Up @@ -168,18 +168,19 @@ export type nodeResponse = {
/**
* Load Type - "TRACK_LOADED", "PLAYLIST_LOADED", "SEARCH_RESULT", "NO_MATCHES", "LOAD_FAILED" for v3 and "track", "playlist", "search", "error" for v4
*/
loadType: string
loadType: string | null
/**
* Playlist Info
*/
playlistInfo?: {
playlistInfo: {
name: string;
selectedTrack: number;
};
} | null;
/**
* Plugin Info
* ## Properties may not exist(Means Empty Object) if Lavalink/Node does not return/provide them
*/
pluginInfo?: any;
pluginInfo: object;

exception: LavalinkTrackLoadException | null
}
Expand Down Expand Up @@ -227,27 +228,53 @@ export declare class Riffy extends EventEmitter {

public fetchRegion(region: string): Array<LavalinkNode>;

/**
* Creates a connection based on the provided options.
*
* @param {Object} options - The options for creating the connection.
* @param {string} options.guildId - The ID of the guild.
* @param {string} [options.region] - The region for the connection.
* @param {number} [options.defaultVolume] - The default volume of the player. **By-Default**: **100**
* @param {LoopOption} [options.loop] - The loop mode of the player.
* @throws {Error} Throws an error if Riffy is not initialized or no nodes are available.
* @return {Player} The created player.
*/
public createConnection(options: {
guildId: string;
voiceChannel: string;
textChannel: string;
deaf?: boolean;
mute?: boolean;
/**
* @description Default volume of the player
* @default default 100
*/
defaultVolume?: number

loop?: LoopOption;
/**
* @description voice region (rtc Region) used for filtering node based on it
* */
*/
region?: string;
}): Player;

public createPlayer(node: Node, options: PlayerOptions): Player;

public removeConnection(guildId: string): void;

/**
* @param {object} param0
* @param {string} param0.query used for searching as a search Query
* @param {*} param0.source A source to search the query on example:ytmsearch for youtube music
* @param {*} param0.requester the requester who's requesting
* @param {(string | Node)?} param0.node the node to request the query on either use node identifier/name or the node class itself
* @returns {nodeResponse} returned properties values are nullable if lavalink doesn't give them
* */
public resolve(params: {
query: string;
source?: string;
requester: any;
node: string | Node
node?: string | Node
}): Promise<nodeResponse>;


Expand Down
8 changes: 7 additions & 1 deletion build/structures/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ class Connection {
this.player.riffy.emit("debug", this.player.node.name, `[Rest Manager] Sending an Update Player request with data: ${JSON.stringify({ voice: this.voice })}`)
this.player.node.rest.updatePlayer({
guildId: this.player.guildId,
data: { voice: this.voice },
data: Object.assign({
voice: this.voice,
/**
* Need a better way so that we don't the volume each time.
*/
volume: this.player.volume,
}),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion build/structures/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Player extends EventEmitter {
this.filters = new Filters(this);
this.mute = options.mute ?? false;
this.deaf = options.deaf ?? false;
this.volume = options.volume ?? 100;
this.volume = options.defaultVolume ?? 100;
this.loop = options.loop ?? "none";
this.data = {};
this.queue = new Queue();
Expand Down
17 changes: 14 additions & 3 deletions build/structures/Riffy.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,25 @@ class Riffy extends EventEmitter {
return nodesByRegion;
}

/**
* Creates a connection based on the provided options.
*
* @param {Object} options - The options for creating the connection.
* @param {string} options.guildId - The ID of the guild.
* @param {string} [options.region] - The region for the connection.
* @param {number} [options.defaultVolume] - The default volume of the player. **By-Default**: **100**
* @param {LoopOption} [options.loop] - The loop mode of the player.
* @throws {Error} Throws an error if Riffy is not initialized or no nodes are available.
* @return {Player} The created player.
*/
createConnection(options) {
if (!this.initiated) throw new Error("You have to initialize Riffy in your ready event");

const player = this.players.get(options.guildId);
if (player) return player;

if (this.leastUsedNodes.length === 0) throw new Error("No nodes are available");

let node;
if (options.region) {
const region = this.fetchRegion(options.region)[0];
Expand Down Expand Up @@ -152,7 +163,7 @@ class Riffy extends EventEmitter {
* @param {*} param0.source A source to search the query on example:ytmsearch for youtube music
* @param {*} param0.requester the requester who's requesting
* @param {(string | Node)?} param0.node the node to request the query on either use node identifier/name or the node class itself
* @returns -- returned properties values are nullable if lavalink doesn't give them
* @returns {import("..").nodeResponse} returned properties values are nullable if lavalink doesn't give them
* */
async resolve({ query, source, requester, node }) {
try {
Expand Down Expand Up @@ -200,7 +211,7 @@ class Riffy extends EventEmitter {
}

this.loadType = response.loadType ?? null
this.pluginInfo = response.pluginInfo ?? null;
this.pluginInfo = response.pluginInfo ?? {};

return {
loadType: this.loadType,
Expand Down

0 comments on commit 817de4c

Please sign in to comment.