-
Notifications
You must be signed in to change notification settings - Fork 80
Selection of the ideal Lavalink node according to the location of the Discord server #88
base: master
Are you sure you want to change the base?
Conversation
Latest Erela.js
Update repo
Code update
Has this code been tested? |
yeah, I tested it - but in JavaScript => idk if I rewrote it to TypeScript correctly |
The thing you gotta realise is, Erela is our baby, hehe. We'd prefer it to be without bugs, and your codebase and amount of changes are almost too big to get a grasp of in one sitting. When we ask if your code has been tested, you respond with "only before, when I wrote it in a language I understand." What do we do with this? I suppose @acollierr17 & myself are forced to do quite some testing with this to avoid bugs if possible. |
I've already dealt with someone from you from Discord that I don't know much about TS. That man told me that someone would help me with the transport from JS to TS. I tried to make the code correct. Respectively, the code should be functional, I'm just not sure about the types of variables in the return and input methods. there were a lot of commits, since I already made one pull request. Unfortunately, I was unable to reset changes to the repository. So that's the reason for so many commits |
@@ -119,7 +124,9 @@ export class Player { | |||
if (options.textChannel) this.textChannel = options.textChannel; | |||
|
|||
const node = this.manager.nodes.get(options.node); | |||
this.node = node || this.manager.leastLoadNodes.first(); | |||
if (node) this.node = node |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
if (node) this.node = node | |
if (node) {this.node = node} |
@@ -58,6 +58,11 @@ function check(options: NodeOptions) { | |||
typeof options.retryDelay !== "number" | |||
) | |||
throw new TypeError('Node option "retryDelay" must be a number.'); | |||
|
|||
if (typeof options.region !== "undefined" && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
if (typeof options.region !== "undefined" && | |
if (typeof options.region !== "undefined" && |
return null; | ||
else if (nodes.size === 1) | ||
return nodes.first(); | ||
else if (nodes.size > 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
else if (nodes.size > 1) | |
else if (nodes.size > 1) |
return node.connected && node.options.region.toLowerCase() == region.toLowerCase() | ||
}); | ||
|
||
if (!nodes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
if (!nodes) | |
if (!nodes) |
@@ -119,7 +124,9 @@ export class Player { | |||
if (options.textChannel) this.textChannel = options.textChannel; | |||
|
|||
const node = this.manager.nodes.get(options.node); | |||
this.node = node || this.manager.leastLoadNodes.first(); | |||
if (node) this.node = node | |||
else if (options.region) this.node = this.manager.nearestNode(options.region); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
else if (options.region) this.node = this.manager.nearestNode(options.region); | |
else if (options.region) {this.node = this.manager.nearestNode(options.region);} |
return node.connected | ||
else if (Array.isArray(node.options.region)) | ||
return node.connected && node.options.region.includes(region) | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'else'.
else | |
else |
|
||
if (!nodes) | ||
return null; | ||
else if (nodes.size === 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
else if (nodes.size === 1) | |
else if (nodes.size === 1) |
this.node = node || this.manager.leastLoadNodes.first(); | ||
if (node) this.node = node | ||
else if (options.region) this.node = this.manager.nearestNode(options.region); | ||
if (!this.node) this.node = this.manager.leastLoadNodes.first() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
if (!this.node) this.node = this.manager.leastLoadNodes.first() | |
if (!this.node) {this.node = this.manager.leastLoadNodes.first()} |
.filter((node) => { | ||
if (!node.options.region) | ||
return node.connected | ||
else if (Array.isArray(node.options.region)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
else if (Array.isArray(node.options.region)) | |
else if (Array.isArray(node.options.region)) |
@@ -10,6 +10,9 @@ function check(options: PlayerOptions) { | |||
throw new TypeError( | |||
'Player option "guild" must be present and be a non-empty string.' | |||
); | |||
|
|||
if (options.region && !/^\d+$/.test(options.region)) | |||
throw new TypeError('Player option "region" must be a non-empty string.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Strings must use doublequote.
throw new TypeError('Player option "region" must be a non-empty string.'); | |
throw new TypeError("Player option \"region\" must be a non-empty string."); |
if (typeof options.region !== "undefined" && | ||
((typeof options.region !== "string" || | ||
!/.+/.test(options.region)) && (!Array.isArray(options.region) || options.region.length === 0 || !/.+/.test(options.region[0])))) | ||
throw new TypeError('Node option "region" must be a non-empty string or array.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Strings must use doublequote.
throw new TypeError('Node option "region" must be a non-empty string or array.'); | |
throw new TypeError("Node option \"region\" must be a non-empty string or array."); |
public nearestNode(region: string): Node { | ||
const nodes = this.nodes | ||
.filter((node) => { | ||
if (!node.options.region) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
if (!node.options.region) | |
if (!node.options.region) |
@@ -10,6 +10,9 @@ function check(options: PlayerOptions) { | |||
throw new TypeError( | |||
'Player option "guild" must be present and be a non-empty string.' | |||
); | |||
|
|||
if (options.region && !/^\d+$/.test(options.region)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Expected { after 'if' condition.
if (options.region && !/^\d+$/.test(options.region)) | |
if (options.region && !/^\d+$/.test(options.region)) |
It is quite likely that there will be some errors in the code. I wrote this code in JavaScript and then transcribed it into TypeScript, a language I don't quite understand.
The principle is that I added the "region" option to the node options, which can be either a string or a array of strings.
Furthermore, the Player now also has the "region" option, which is the region of the Discord server (string).
If the user specifies the Node he wants to use when creating the Player, that one is selected (as before).
If the user has provided the "region" option, the corresponding Nodes are selected.
If no Node is returned or the "region" option is not provided, the best one is selected according to the statistics of that Node (as before)