Skip to content

Commit

Permalink
Merge pull request #33 from EvarinDev/dev
Browse files Browse the repository at this point in the history
Added MoveNode
  • Loading branch information
EvarinDev authored Sep 8, 2024
2 parents 2600a71 + a37fcf1 commit d3fb47b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/.github/** @FAYStarNext
/.github/** @EvarinDev
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 EwarinDev
Copyright (c) 2024 EvarinDev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
- [Installing](#installing)
- [Features](#features)
- [🎈 Usage ](#-usage-)
- [⛏️ Built Using ](#️-built-using-)
- [Credits](#credits)

## 🧐 About <a name = "about"></a>
Expand Down Expand Up @@ -82,7 +81,7 @@ let manager = new Manager({
resumeStatus: true,
},
],
cache: {
caches: {
enabled: true,
time: 60000,
},
Expand Down Expand Up @@ -160,12 +159,12 @@ client.on("ready" , () => {
client.login(process.env.TOKEN);
```

## ⛏️ Built Using <a name = "built_using"></a>

- [WebSocket](https://github.com/websockets/ws) - WebSocket Client
## Credits

- [Erela.Js](https://github.com/MenuDocs/erela.js)
- [MagmaStream](https://github.com/Blackfort-Hosting/magmastream/)

## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=EwarinDev/DiscordStatusProfile&type=Date)](https://star-history.com/#EwarinDev/DiscordStatusProfile&Date)

See also the list of [contributors](https://github.com/FAYStarNext/Sunday.ts/contributors) who participated in this project.
12 changes: 7 additions & 5 deletions example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ client.on("messageCreate", async (message) => {
if (message.author.bot) return;

const [command, ...args] = message.content.slice(0).split(/\s+/g);
if (command === 'play') {
await handlePlayCommand(message, args);
switch (command) {
case 'play':
return handlePlayCommand(message, args);
case 'moveNode':
let player = manager.players.get(message.guild?.id as string);
player?.moveNode(args[0]);
return message.reply(`Node ${player?.node.options.identifier} moved to ${args[0]}`);
}
});
manager.on("SearchCacheClear" , (data) => {
console.log(`Cache cleared: ${data}`);
});
client.on("raw", (data) => manager.updateVoiceState(data));
client.on("ready" , () => {
manager.init(client.user?.id as string);
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
"name": "sunday.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"version": "1.1.3",
"version": "1.1.4",
"description": "Sunday a lavalink wrapper",
"license": "MIT",
"author": "EwarinDev",
"author": "EvarinDev",
"scripts": {
"build:js": "npx babel src --out-dir dist --extensions \".ts,.tsx\" --source-maps inline",
"lint": "npx eslint src/**/*.ts",
"test:manager": "bun test/manager.ts",
"test:player": "bun test/player.ts"
},
"devDependencies": {
"@eslint/js": "^9.9.1",
"@eslint/js": "^9.10.0",
"@types/bun": "latest",
"eslint": "^9.9.1",
"eslint": "^9.10.0",
"globals": "^15.9.0",
"typescript-eslint": "^8.4.0",
"@babel/cli": "^7.25.6",
Expand All @@ -28,9 +28,9 @@
"access": "public"
},
"bugs": {
"url": "https://github.com/EwarinDev/Sunday.ts/issues"
"url": "https://github.com/EvarinDev/Sunday.ts/issues"
},
"homepage": "https://github.com/EwarinDev/Sunday.ts#readme",
"homepage": "https://github.com/EvarinDev/Sunday.ts#readme",
"peerDependencies": {
"typescript": "^5.5.4"
},
Expand Down
12 changes: 6 additions & 6 deletions src/Structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Manager extends TypedEmitter<ManagerEvents> {
public caches = new Collection<string, SearchResult>();

/** Returns the nodes that has the least load. */
private get leastLoadNode(): Collection<string, Node> {
public get leastLoadNode(): Collection<string, Node> {
return this.nodes
.filter((node) => node.connected)
.sort((a, b) => {
Expand Down Expand Up @@ -119,7 +119,7 @@ export class Manager extends TypedEmitter<ManagerEvents> {
shards: 1,
autoPlay: true,
usePriority: false,
clientName: "Sunday.ts (https://github.com/EwarinDev/Sunday.ts)",
clientName: "Sunday.ts (https://github.com/EvarinDev/Sunday.ts)",
defaultSearchPlatform: "youtube",
useNode: "leastPlayers",
...options,
Expand All @@ -138,6 +138,9 @@ export class Manager extends TypedEmitter<ManagerEvents> {
this.nodes.set(node.options.identifier, node);
}
}
setInterval(() => {
this.caches.clear();
}, this.options.caches.time);
}

/**
Expand Down Expand Up @@ -190,10 +193,7 @@ export class Manager extends TypedEmitter<ManagerEvents> {

try {
const res = (await node.rest.get(`/v4/loadtracks?identifier=${encodeURIComponent(search)}`)) as LavalinkResponse;

if (!res) {
throw new Error("Query not found.");
}
if (!res) throw new Error("Query not found.");

let searchData = [];
let playlistData: PlaylistRawData | undefined;
Expand Down
63 changes: 63 additions & 0 deletions src/Structures/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,69 @@ export class Player {
return this;
}

/**
* Moves the player to a different node.
*
* @param {string} [node] - The ID of the node to move to.
* @returns {this} - The player instance.
*/
public async moveNode(node?: string): Promise<this> {
node = node || this.manager.leastLoadNode.first().options.identifier || this.manager.nodes.filter((n) => n.connected).first().options.identifier;
if (!this.manager.nodes.has(node)) throw new RangeError("No nodes available.");
if (this.node.options.identifier === node) return this;

const destroyOldNode = async (node: Node) => {
this.state = "MOVING";

if (this.manager.nodes.get(node.options.identifier) && this.manager.nodes.get(node.options.identifier).connected) await node.rest.destroyPlayer(this.guild);

setTimeout(() => (this.state = "CONNECTED"), 5000);
};

const currentNode = this.node;
const destinationNode = this.manager.nodes.get(node);
let position = this.position;

if (currentNode.connected) {
const fetchedPlayer: any = await currentNode.rest.get(`/v4/sessions/${currentNode.sessionId}/players/${this.guild}`);
position = fetchedPlayer.track.info.position;
}

await destinationNode.rest.updatePlayer({
guildId: this.guild,
data: {
encodedTrack: this.queue.current?.track,
position: position,
volume: this.volume,
paused: this.paused,
filters: {
distortion: this.filters.distortion,
equalizer: this.filters.equalizer,
karaoke: this.filters.karaoke,
rotation: this.filters.rotation,
timescale: this.filters.timescale,
vibrato: this.filters.vibrato,
volume: this.filters.volume,
},
},
});

await destinationNode.rest.updatePlayer({
guildId: this.guild,
data: {
voice: {
token: this.voiceState.event.token,
endpoint: this.voiceState.event.endpoint,
sessionId: this!.voiceState?.sessionId!,
},
},
});

this.node = destinationNode;
destroyOldNode(currentNode);
return this;
}

/** Disconnect from the voice channel. */
public disconnect(): this {
if (this.voiceChannel === null) return this;
Expand Down
2 changes: 1 addition & 1 deletion src/Structures/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export type Sizes = "0" | "1" | "2" | "3" | "default" | "mqdefault" | "hqdefault

export type LoadType = "track" | "playlist" | "search" | "empty" | "error";

export type State = "CONNECTED" | "CONNECTING" | "DISCONNECTED" | "DISCONNECTING" | "DESTROYING";
export type State = "CONNECTED" | "CONNECTING" | "DISCONNECTED" | "DISCONNECTING" | "DESTROYING" | "MOVING";

export type PlayerEvents = TrackStartEvent | TrackEndEvent | TrackStuckEvent | TrackExceptionEvent | WebSocketClosedEvent;

Expand Down

0 comments on commit d3fb47b

Please sign in to comment.