Skip to content

Commit

Permalink
Changing stuff on autoresume load from file
Browse files Browse the repository at this point in the history
- made Node.ts protected async queueEnd() public
- changed Manager.ts public loadPlayerStates to async Promise<> and implemented some new changes like queueEnd / trackEnd on load etc.
  • Loading branch information
Vexify4103 authored and SxMAbel committed Oct 14, 2024
1 parent ea779b2 commit 4e3c1c6
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 40 deletions.
138 changes: 103 additions & 35 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,22 @@ export class Manager extends EventEmitter {
private initiated = false;

/** Loads player states from the JSON file. */
public loadPlayerStates(nodeId: string): void {
public async loadPlayerStates(nodeId: string): Promise<void> {
// Changed to async and added Promise<void>
const node = this.nodes.get(nodeId);
if (!node) throw new Error(`Could not find node: ${nodeId}`);

const info = (await node.rest.getAllPlayers()) as LavaPlayer[];

const playerStatesDir = path.join(process.cwd(), "node_modules", "magmastream", "dist", "sessionData", "players");

if (!fs.existsSync(playerStatesDir)) {
fs.mkdirSync(playerStatesDir, { recursive: true });
console.log(`Created directory at ${playerStatesDir}`);
}

const playerFiles = fs.readdirSync(playerStatesDir);

const createTrackData = (song): TrackData => ({
encoded: song.track,
info: {
Expand All @@ -72,21 +87,17 @@ export class Manager extends EventEmitter {
pluginInfo: song.pluginInfo,
});

const playerStatesDir = path.join(process.cwd(), "node_modules", "magmastream", "dist", "sessionData", "players");

if (!fs.existsSync(playerStatesDir)) {
fs.mkdirSync(playerStatesDir, { recursive: true });
console.log(`Created directory at ${playerStatesDir}`);
}

const playerFiles = fs.readdirSync(playerStatesDir);

for (const file of playerFiles) {
const filePath = path.join(playerStatesDir, file);
const data = fs.readFileSync(filePath, "utf-8");
const state = JSON.parse(data);

if (state && typeof state === "object" && state.guild && state.node.options.identifier === nodeId) {
const lavaPlayer = info.find((player) => player.guildId === state.guild);
if (!lavaPlayer) {
this.destroy(state.guild);
continue;
}
const playerOptions: PlayerOptions = {
guild: state.options.guild,
textChannel: state.options.textChannel,
Expand All @@ -96,36 +107,76 @@ export class Manager extends EventEmitter {
};

this.create(playerOptions);
}

const player = this.get(state.options.guild);
const tracks = [];
if (state.queue.current !== null) {
const currentTrack = state.queue.current;
tracks.push(TrackUtils.build(createTrackData(currentTrack), currentTrack.requester));
const player = this.get(state.options.guild);
if (!lavaPlayer.state.connected) {
try {
player.connect();
} catch (error) {
console.log(error);
continue;
}
}

for (const key in state.queue) {
if (!isNaN(Number(key)) && key !== "current" && key !== "previous" && key !== "manager") {
const song = state.queue[key];
tracks.push(TrackUtils.build(createTrackData(song), song.requester));
const tracks = [];

if (!lavaPlayer.track) {
if (state.queue.current !== null) {
for (const key in state.queue) {
if (!isNaN(Number(key)) && key !== "current" && key !== "previous" && key !== "manager") {
const song = state.queue[key];
tracks.push(TrackUtils.build(createTrackData(song), song.requester));
}
}

if (tracks.length > 0) {
if (player.state !== "CONNECTED") player.connect();
player.queue.add(tracks);
if (!state.paused && player.state === "CONNECTED") player.play();
else console.log(player.state);
} else {
const payload = {
reason: "finished",
};
node.queueEnd(player, state.queue.current, payload as TrackEndEvent);
continue;
}
} else {
if (state.queue.previous !== null) {
const payload = {
reason: "finished",
};
node.queueEnd(player, state.queue.previous, payload as TrackEndEvent);
} else this.destroy(state.guild);
}
} else {
const currentTrack = state.queue.current;
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];
tracks.push(TrackUtils.build(createTrackData(song), song.requester));
}
}
if (player.state !== "CONNECTED") player.connect();
player.queue.add(tracks);
}

player.queue.add(tracks);
}
if (state.paused) player.pause(true);
player.setTrackRepeat(state.trackRepeat);
player.setQueueRepeat(state.queueRepeat);
if (state.dynamicRepeat) {
player.setDynamicRepeat(state.dynamicRepeat, state.dynamicLoopInterval._idleTimeout);
}
if (state.isAutoplay) {
player.setAutoplay(state.isAutoplay, state.data.Internal_BotUser);
if (state.paused) player.pause(true);
player.setTrackRepeat(state.trackRepeat);
player.setQueueRepeat(state.queueRepeat);
if (state.dynamicRepeat) {
player.setDynamicRepeat(state.dynamicRepeat, state.dynamicLoopInterval._idleTimeout);
}
if (state.isAutoplay) {
player.setAutoplay(state.isAutoplay, state.data.Internal_BotUser);
}
console.log(`Loaded player state for ${state.options.guild}.`);
}
console.log(`Loaded player state for ${state.options.guild}.`);
}

console.log("Loaded player states from player files.");
console.log("Finished loading player states from player files.");
}

/** Gets each player's JSON file */
Expand Down Expand Up @@ -157,7 +208,7 @@ export class Manager extends EventEmitter {

const serialize = (obj: unknown): unknown => {
if (obj && typeof obj === "object") {
if (seen.has(obj)) return; // Prevent circular references
if (seen.has(obj)) return;

seen.add(obj);
}
Expand All @@ -177,7 +228,6 @@ export class Manager extends EventEmitter {
};
}

// return value === undefined ? null : serialize(value);
return serialize(value);
})
);
Expand Down Expand Up @@ -253,7 +303,6 @@ 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 lastProcessedGuilds: Set<string> = new Set();
private lastSaveTimes: Map<string, number> = new Map();
private saveInterval: number = 1000;
Expand Down Expand Up @@ -752,6 +801,25 @@ export interface LavalinkResponse {
data: TrackData[] | PlaylistRawData;
}

interface LavaPlayer {
guildId: string;
track: TrackData | Track;
volume: number;
paused: boolean;
state: {
time: number;
position: number;
connected: boolean;
ping: number;
};
voice: {
token: string;
endpoint: string;
sessionId: string;
};
filters: Record<string, unknown>;
}

export interface SearchResult {
/** The load type of the result. */
loadType: LoadType;
Expand Down
10 changes: 5 additions & 5 deletions src/structures/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export class Node {
this.manager.emit("nodeCreate", this);
this.rest = new Rest(this);

this.createSessionIdsFile(); // Create the session IDs file on initialization
this.loadSessionIds(); // Load session IDs on initialization
this.createSessionIdsFile();
this.loadSessionIds();
}

/** Creates the sessionIds.json file if it doesn't exist. */
Expand All @@ -146,8 +146,8 @@ export class Node {

/** Updates the session ID in the sessionIds.json file. */
public updateSessionId(): void {
sessionIdsMap.set(this.options.identifier, this.sessionId); // Store session ID
fs.writeFileSync(sessionIdsFilePath, JSON.stringify(Object.fromEntries(sessionIdsMap))); // Update JSON file
sessionIdsMap.set(this.options.identifier, this.sessionId);
fs.writeFileSync(sessionIdsFilePath, JSON.stringify(Object.fromEntries(sessionIdsMap)));
console.log(`Updated session ID for ${this.options.identifier} to ${this.sessionId}`);
}

Expand Down Expand Up @@ -515,7 +515,7 @@ export class Node {
if (this.manager.options.autoPlay) player.play();
}

protected async queueEnd(player: Player, track: Track, payload: TrackEndEvent): Promise<void> {
public async queueEnd(player: Player, track: Track, payload: TrackEndEvent): Promise<void> {
player.queue.previous = player.queue.current;
player.queue.current = null;

Expand Down

0 comments on commit 4e3c1c6

Please sign in to comment.