Skip to content

Commit

Permalink
fix: Requester (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
1Lucas1apk authored Mar 25, 2024
2 parents 25249c6 + 89aea1c commit 14a4bde
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 41 deletions.
5 changes: 4 additions & 1 deletion dist/src/@Entities/MoonlinkNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class MoonlinkNode {
this.regions = node.regions;
this.http = `http${node.secure ? "s" : ""}://${this.address}/v4/`;
this.rest = new (index_1.Structure.get("MoonlinkRestFul"))(this);
if (node.sessionId)
this.sessionId = node.sessionId;
this.connect();
}
get address() {
Expand Down Expand Up @@ -229,7 +231,8 @@ class MoonlinkNode {
let player = this._manager.players.get(payload.guildId);
switch (payload.type) {
case "TrackStartEvent": {
player.current = new (index_1.Structure.get("MoonlinkTrack"))(payload.track);
if (!player.current)
player.current = new (index_1.Structure.get("MoonlinkTrack"))(payload.track);
player.playing = true;
player.paused = false;
this._manager.emit("trackStart", player, player.current);
Expand Down
11 changes: 6 additions & 5 deletions dist/src/@Managers/PlayerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ class PlayerManager {
};
}
async attemptConnection(guildId) {
if (!this.cache[guildId] ||
!this.voices[guildId] ||
(!this.voices[guildId]?.token &&
!this.voices[guildId]?.endpoint &&
!this.voices[guildId]?.sessionId))
if (!this.cache[guildId])
return false;
if (this.voices[guildId] &&
!this.voices[guildId]?.token &&
!this.voices[guildId]?.endpoint &&
!this.voices[guildId]?.sessionId)
return false;
if (this._manager.options?.balancingPlayersByRegion) {
const voiceRegion = this.voices[guildId]?.endpoint?.match(/([a-zA-Z-]+)\d+/)?.[1];
Expand Down
2 changes: 2 additions & 0 deletions dist/src/@Typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface INode {
regions?: string[];
retryAmount?: number;
retryDelay?: number;
sessionId?: string;
}
export interface IOptions {
clientId?: string;
Expand All @@ -104,6 +105,7 @@ export interface IOptions {
resume?: boolean;
plugins?: Plugin[];
http2?: boolean;
doNotSaveToFiles?: boolean;
switchPlayersAnotherNode?: boolean;
destroyPlayersStopped?: boolean;
balancingPlayersByRegion?: boolean;
Expand Down
1 change: 1 addition & 0 deletions dist/src/@Utils/MoonlinkTrack.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export declare class MoonlinkTrack {
time?: number;
constructor(data?: MoonlinkTrackOptions, requester?: string | any);
get calculateRealTimePosition(): number;
setRequester(data: any): void;
}
3 changes: 3 additions & 0 deletions dist/src/@Utils/MoonlinkTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ class MoonlinkTrack {
}
return this.position;
}
setRequester(data) {
this.requester = data;
}
}
exports.MoonlinkTrack = MoonlinkTrack;
10 changes: 6 additions & 4 deletions src/@Entities/MoonlinkNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class MoonlinkNode {
this.regions = node.regions;
this.http = `http${node.secure ? "s" : ""}://${this.address}/v4/`;
this.rest = new (Structure.get("MoonlinkRestFul"))(this);

if (node.sessionId) this.sessionId = node.sessionId;
this.connect();
}

Expand Down Expand Up @@ -355,9 +355,11 @@ export class MoonlinkNode {
let player: MoonlinkPlayer = this._manager.players.get(payload.guildId);
switch (payload.type) {
case "TrackStartEvent": {
player.current = new (Structure.get("MoonlinkTrack"))(
payload.track
);
if (!player.current)
player.current = new (Structure.get("MoonlinkTrack"))(
payload.track
);

player.playing = true;
player.paused = false;
this._manager.emit("trackStart", player, player.current);
Expand Down
63 changes: 33 additions & 30 deletions src/@Managers/PlayerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,40 @@ export class PlayerManager {
};
}
public async attemptConnection(guildId: string): Promise<boolean> {
if (!this.cache[guildId]) return false;
if (
!this.cache[guildId] ||
!this.voices[guildId] ||
(!this.voices[guildId]?.token &&
!this.voices[guildId]?.endpoint &&
!this.voices[guildId]?.sessionId)
)
return false;

if (this._manager.options?.balancingPlayersByRegion) {
const voiceRegion =
this.voices[guildId]?.endpoint?.match(/([a-zA-Z-]+)\d+/)?.[1];

if (!this.cache[guildId].voiceRegion) {
const connectedNodes = [
...this._manager.nodes.map.values()
].filter(node => node.state == "READY");
const matchingNode = connectedNodes.find(node =>
node.regions.includes(voiceRegion)
);

this.cache[guildId].voiceRegion = voiceRegion;

if (matchingNode) {
this.cache[guildId].node = matchingNode;
this.voices[guildId] &&
!this.voices[guildId]?.token &&
!this.voices[guildId]?.endpoint &&
!this.voices[guildId]?.sessionId
) return false;
if (this._manager.options?.balancingPlayersByRegion) {
const voiceRegion =
this.voices[guildId]?.endpoint?.match(
/([a-zA-Z-]+)\d+/
)?.[1];

if (!this.cache[guildId].voiceRegion) {
const connectedNodes = [
...this._manager.nodes.map.values()
].filter(node => node.state == "READY");
const matchingNode = connectedNodes.find(node =>
node.regions.includes(voiceRegion)
);

this.cache[guildId].voiceRegion = voiceRegion;

if (matchingNode) {
this.cache[guildId].node = matchingNode;
}
}
} else if (!this.cache[guildId].voiceRegion) {
const voiceRegion =
this.voices[guildId]?.endpoint?.match(
/([a-zA-Z-]+)\d+/
)?.[1];
this.cache[guildId].voiceRegion = voiceRegion;
}
} else if (!this.cache[guildId].voiceRegion) {
const voiceRegion =
this.voices[guildId]?.endpoint?.match(/([a-zA-Z-]+)\d+/)?.[1];
this.cache[guildId].voiceRegion = voiceRegion;
}

await this.cache[guildId].node.rest.update({
guildId,
Expand Down Expand Up @@ -220,6 +222,7 @@ export class PlayerManager {
}
public delete(guildId): void {
delete this.cache[guildId];
if(Structure.db.get(`players.${guildId}`))Structure.db.delete(`players.${guildId}`);
if (Structure.db.get(`players.${guildId}`))
Structure.db.delete(`players.${guildId}`);
}
}
2 changes: 2 additions & 0 deletions src/@Typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export interface INode {
regions?: string[];
retryAmount?: number;
retryDelay?: number;
sessionId?: string;
}

export interface IOptions {
Expand All @@ -138,6 +139,7 @@ export interface IOptions {
resume?: boolean;
plugins?: Plugin[];
http2?: boolean;
doNotSaveToFiles?: boolean;
switchPlayersAnotherNode?: boolean;
destroyPlayersStopped?: boolean;
balancingPlayersByRegion?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/@Utils/MoonlinkDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ export class MoonlinkDatabase {
throw new Error("@Moonlink(Database) - Failed to save data");
}
}
}
}
3 changes: 3 additions & 0 deletions src/@Utils/MoonlinkTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ export class MoonlinkTrack {

return this.position;
}
setRequester(data: any): void {
this.requester = data;
}
}

0 comments on commit 14a4bde

Please sign in to comment.