Skip to content

Commit

Permalink
EVEN MOAAARR CHANGES YIPPEE
Browse files Browse the repository at this point in the history
- EVEN MOAAARR CHANGES YIPPEE
  • Loading branch information
Vexify4103 authored and SxMAbel committed Oct 14, 2024
1 parent 1ec960b commit 8d336e3
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 67 deletions.
132 changes: 90 additions & 42 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ export class Manager extends EventEmitter {

/** Loads player states from the JSON file. */
public loadPlayerStates(nodeId: string): Promise<void> {
/** Function to create track data */
const createTrackData = (song): TrackData => ({
encoded: song.track,
info: {
identifier: song.identifier,
isSeekable: song.isSeekable,
author: song.author,
length: song.duration,
isrc: song.isrc,
isStream: song.isStream,
title: song.title,
uri: song.uri,
artworkUrl: song.artworkUrl,
sourceName: song.sourceName,
},
pluginInfo: song.pluginInfo,
});

return new Promise((resolve, reject) => {
if (fs.existsSync(playerStatesFilePath)) {
const data = fs.readFileSync(playerStatesFilePath, "utf-8");
Expand All @@ -88,55 +106,25 @@ export class Manager extends EventEmitter {
player.seek(state.position);
player.setTrackRepeat(state.trackRepeat);
player.setQueueRepeat(state.queueRepeat);
// player.setDynamicRepeat(state.dynamicRepeat, 6969)??? why milliseconds?
if (state.dynamicRepeat) {
player.setDynamicRepeat(state.dynamicRepeat, state.dynamicLoopInterval._idleTimeout);
}
if (state.isAutoplay) {
player.setAutoplay(state.isAutoplay, state.data.Internal_BotUser);
}
const tracks = [];
if (state.queue.current !== null) {
const currentTrack = state.queue.current;

const trackData: TrackData = {
encoded: currentTrack.track,
info: {
identifier: currentTrack.identifier,
isSeekable: currentTrack.isSeekable,
author: currentTrack.author,
length: currentTrack.duration,
isrc: currentTrack.isrc,
isStream: currentTrack.isStream,
title: currentTrack.title,
uri: currentTrack.uri,
artworkUrl: currentTrack.artworkUrl,
sourceName: currentTrack.sourceName,
},
pluginInfo: currentTrack.pluginInfo,
};

player.queue.add(TrackUtils.build(trackData, currentTrack.requester));
tracks.push(TrackUtils.build(createTrackData(currentTrack), currentTrack.requester));

for (const key in state.queue) {
if (!isNaN(Number(key)) && key !== "current" && key !== "previous" && key !== "manager") {
const song = state.queue[key];
const trackData: TrackData = {
encoded: song.track,
info: {
identifier: song.identifier,
isSeekable: song.isSeekable,
author: song.author,
length: song.duration,
isrc: song.isrc,
isStream: song.isStream,
title: song.title,
uri: song.uri,
artworkUrl: song.artworkUrl,
sourceName: song.sourceName,
},
pluginInfo: song.pluginInfo,
};

player.queue.add(TrackUtils.build(trackData, song.requester));
tracks.push(TrackUtils.build(createTrackData(song), song.requester));
}
}

player.queue.add(tracks);
}
}

Expand All @@ -148,6 +136,7 @@ export class Manager extends EventEmitter {
});
}

/** Saves player states to the JSON file. */
public savePlayerStates(players: Map<string, Player>): void {
const playerStates: Record<string, Player> = {};

Expand Down Expand Up @@ -250,14 +239,61 @@ export class Manager extends EventEmitter {
return this.options.usePriority ? this.priorityNode : this.options.useNode === "leastLoad" ? this.leastLoadNode.first() : this.leastPlayersNode.first();
}

/** work in progress */
// private lastSaveTimes: Map<string, number> = new Map();
// private saveInterval: number = 1000;

/** Register savePlayerStates events */
private registerPlayerStateEvents(): void {
const events = ["playerStateUpdate", "playerDestroy", "queueEnd", "trackStart", "trackEnd"];
for (const event of events as (keyof ManagerEvents)[]) {
this.on(event, () => this.savePlayerStates(this.players));
const events: (keyof ManagerEvents)[] = ["playerStateUpdate", "playerDestroy"];
for (const event of events) {
// this.on(event, (player: Player) => this.handleEvent(event, player));
this.on(event, () => this.handleEvent(event));
}
}

private handleEvent(event: keyof ManagerEvents): void {
// private handleEvent(event: keyof ManagerEvents, player: Player): void {
switch (event) {
case "playerDestroy":
// this.handlePlayerDestroy(player);
this.handlePlayerDestroy();
break;
case "playerStateUpdate":
// this.handlePlayerStateUpdate(player);
this.handlePlayerStateUpdate();
break;
default:
this.savePlayerStates(this.players);
break;
}
}

private handlePlayerDestroy(): void {
this.savePlayerStates(this.players);
}
/** work in progress */
// private handlePlayerDestroy(player: Player): void {
// this.lastSaveTimes.delete(player.guild);
// this.savePlayerStates(this.players);
// }

private handlePlayerStateUpdate(): void {
this.savePlayerStates(this.players);
}
/** work in progress */
// private handlePlayerStateUpdate(player: Player): void {
// const currentTime = Date.now();
// const guildId = player.guild;

// this.savePlayerStates(this.players);

// if (!this.lastSaveTimes.has(guildId) || currentTime - this.lastSaveTimes.get(guildId)! >= this.saveInterval) {
// this.savePlayerStates(this.players);
// this.lastSaveTimes.set(guildId, currentTime);
// }
// }

/**
* Initiates the Manager class.
* @param options
Expand Down Expand Up @@ -664,6 +700,18 @@ export interface ManagerOptions {

export type SearchPlatform = "deezer" | "soundcloud" | "youtube music" | "youtube" | "spotify" | "jiosaavn" | "tidal" | "applemusic" | "bandcamp";

export type PlayerStateEventType =
| "connectionChange"
| "playerCreate"
| "playerDestroy"
| "channelChange"
| "volumeChange"
| "pauseChange"
| "queueChange"
| "trackChange"
| "repeatChange"
| "autoplayChange";

export interface SearchQuery {
/** The source to search from. */
source?: SearchPlatform | string;
Expand Down Expand Up @@ -715,7 +763,7 @@ export interface ManagerEvents {
nodeRaw: [payload: unknown];
playerCreate: [player: Player];
playerDestroy: [player: Player];
playerStateUpdate: [oldPlayer: Player, newPlayer: Player];
playerStateUpdate: [oldPlayer: Player, newPlayer: Player, changeType: PlayerStateEventType];
playerMove: [player: Player, initChannel: string, newChannel: string];
playerDisconnect: [player: Player, oldChannel: string];
queueEnd: [player: Player, track: Track | UnresolvedTrack, payload: TrackEndEvent];
Expand Down
5 changes: 5 additions & 0 deletions src/structures/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,18 @@ export class Node {
}

protected trackStart(player: Player, track: Track, payload: TrackStartEvent): void {
const oldPlayer = player;
player.playing = true;
player.paused = false;
this.manager.emit("trackStart", player, track, payload);
this.manager.emit("playerStateUpdate", oldPlayer, player, "trackChange");
}

protected async trackEnd(player: Player, track: Track, payload: TrackEndEvent): Promise<void> {
const { reason } = payload;

const oldPlayer = player;

// If the track failed to load or was cleaned up
if (["loadFailed", "cleanup"].includes(reason)) {
this.handleFailedTrack(player, track, payload);
Expand All @@ -359,6 +363,7 @@ export class Node {
else {
await this.queueEnd(player, track, payload);
}
this.manager.emit("playerStateUpdate", oldPlayer, player, "trackChange");
}

public extractSpotifyTrackID(url: string): string | null {
Expand Down
28 changes: 14 additions & 14 deletions src/structures/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class Player {

this.state = "CONNECTED";

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "connectionChange");
return this;
}

Expand All @@ -162,7 +162,7 @@ export class Player {
this.voiceChannel = null;
this.state = "DISCONNECTED";

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "connectionChange");
return this;
}

Expand All @@ -178,7 +178,7 @@ export class Player {
this.node.rest.destroyPlayer(this.guild);
this.manager.emit("playerDestroy", this);
this.manager.players.delete(this.guild);
this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "playerDestroy");
}

/**
Expand All @@ -193,7 +193,7 @@ export class Player {
this.voiceChannel = channel;
this.connect();

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "channelChange");
return this;
}

Expand All @@ -208,7 +208,7 @@ export class Player {

this.textChannel = channel;

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "channelChange");
return this;
}

Expand Down Expand Up @@ -297,7 +297,7 @@ export class Player {
this.isAutoplay = autoplayState;
this.set("Internal_BotUser", botUser);

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "autoplayChange");
return this;
}

Expand Down Expand Up @@ -407,7 +407,7 @@ export class Player {
});

this.volume = volume;
this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "volumeChange");

return this;
}
Expand Down Expand Up @@ -453,7 +453,7 @@ export class Player {
this.dynamicRepeat = false;
}

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "repeatChange");
return this;
}

Expand All @@ -476,7 +476,7 @@ export class Player {
this.dynamicRepeat = false;
}

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "repeatChange");
return this;
}

Expand Down Expand Up @@ -516,7 +516,7 @@ export class Player {
this.dynamicRepeat = false;
}

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "repeatChange");
return this;
}

Expand Down Expand Up @@ -551,7 +551,7 @@ export class Player {
},
});

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "trackChange");
return this;
}

Expand All @@ -576,7 +576,7 @@ export class Player {
},
});

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "pauseChange");
return this;
}

Expand All @@ -586,7 +586,7 @@ export class Player {
this.queue.unshift(this.queue.previous);
this.stop();

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "trackChange");
return this;
}

Expand Down Expand Up @@ -614,7 +614,7 @@ export class Player {
},
});

this.manager.emit("playerStateUpdate", oldPlayer, this);
this.manager.emit("playerStateUpdate", oldPlayer, this, "trackChange");
return this;
}
}
Expand Down
Loading

0 comments on commit 8d336e3

Please sign in to comment.