From 98b7ca2a731264232afc7b7a53ea5423ba21530c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Tue, 12 Jan 2021 11:32:29 +0100 Subject: [PATCH 01/13] Added removeDuplicates function --- src/structures/Queue.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/structures/Queue.ts b/src/structures/Queue.ts index 7d272eb..3f5db7a 100644 --- a/src/structures/Queue.ts +++ b/src/structures/Queue.ts @@ -115,4 +115,19 @@ export class Queue extends Array { [this[i], this[j]] = [this[j], this[i]]; } } + + /** Remove duplicates from the queue. */ + public removeDuplicates(): void { + if (this.totalSize == 0) + throw new RangeError(`The queue must not be empty`); + else if (this.totalSize < 2) + throw new RangeError(`The queue must contain more than one song`); + const tracks = this.reduce((accumulator, currentValue) => { + if (!accumulator.find(obj => obj["title"] === currentValue["title"])) + accumulator.push(currentValue); + return accumulator; + }, []); + this.clear(); + this.add(tracks); + } } From 14da02435f1b9b8053fd560e2aa51c6681cd556e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Tue, 12 Jan 2021 14:05:47 +0100 Subject: [PATCH 02/13] Added isUnique and getUnique functions --- src/structures/Utils.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/structures/Utils.ts b/src/structures/Utils.ts index 1b3711a..fc41f2a 100644 --- a/src/structures/Utils.ts +++ b/src/structures/Utils.ts @@ -79,6 +79,39 @@ export abstract class TrackUtils { throw new RangeError("Provided argument must be present."); return track[TRACK_SYMBOL] === true; } + + /** + * Checks if the provided track is unique. + * @param track + * @param queue + */ + static isUnique(track: unknown, queue: unknown): boolean { + if (typeof track === "undefined") + throw new RangeError("Provided argument (track) must be present."); + if (typeof queue === "undefined") + throw new RangeError("Provided argument (queue) must be present."); + if (Array.isArray(track)) + throw new RangeError('Track must be a "Track", not "Track[]".'); + if (queue.size == 0) + return true; + return queue.filter(p => { + if (p.uri === undefined) + return p.title === track.title + else + return p.uri === track.uri + }).length === 0; + } + + /** + * Returns only unique tracks. + * @param track + * @param queue + */ + static getUnique(track: unknown, queue: unknown): Track[] { + if (!Array.isArray(track)) + throw new RangeError('Track must be "Track[]", not "Track".'); + return track.filter(track => !queue.find(ext => TrackUtils.isUnresolvedTrack(track) ? track.title === ext.title : track.identifier === ext.identifier)); + } /** * Builds a Track from the raw data from Lavalink and a optional requester. @@ -397,4 +430,4 @@ export interface PlayerUpdate { time: number; }; guildId: string; -} \ No newline at end of file +} From 422734586e3cb4b4bbfd3e6dcea45ffe6f6757df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Tue, 12 Jan 2021 14:24:48 +0100 Subject: [PATCH 03/13] Changed queue type to Queue --- src/structures/Utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/Utils.ts b/src/structures/Utils.ts index fc41f2a..5b35674 100644 --- a/src/structures/Utils.ts +++ b/src/structures/Utils.ts @@ -85,7 +85,7 @@ export abstract class TrackUtils { * @param track * @param queue */ - static isUnique(track: unknown, queue: unknown): boolean { + static isUnique(track: unknown, queue: Queue): boolean { if (typeof track === "undefined") throw new RangeError("Provided argument (track) must be present."); if (typeof queue === "undefined") @@ -107,7 +107,7 @@ export abstract class TrackUtils { * @param track * @param queue */ - static getUnique(track: unknown, queue: unknown): Track[] { + static getUnique(track: unknown, queue: Queue): Track[] { if (!Array.isArray(track)) throw new RangeError('Track must be "Track[]", not "Track".'); return track.filter(track => !queue.find(ext => TrackUtils.isUnresolvedTrack(track) ? track.title === ext.title : track.identifier === ext.identifier)); From 1f66ee8b6f4893f21d744b55fd37eacb93b092cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Tue, 12 Jan 2021 14:25:39 +0100 Subject: [PATCH 04/13] Added code analysis --- .github/workflows/codeql-analysis.yml | 67 +++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..982c1b5 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,67 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ master ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ master ] + schedule: + - cron: '17 12 * * 0' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + language: [ 'javascript' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + # Learn more: + # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 From e7120afcc6ecfc44f0cb0b41c0a4af53ee3d4a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Tue, 12 Jan 2021 14:29:51 +0100 Subject: [PATCH 05/13] track should be Track --- src/structures/Utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/Utils.ts b/src/structures/Utils.ts index 5b35674..103d129 100644 --- a/src/structures/Utils.ts +++ b/src/structures/Utils.ts @@ -85,7 +85,7 @@ export abstract class TrackUtils { * @param track * @param queue */ - static isUnique(track: unknown, queue: Queue): boolean { + static isUnique(track: Track, queue: Queue): boolean { if (typeof track === "undefined") throw new RangeError("Provided argument (track) must be present."); if (typeof queue === "undefined") From d03e5e7102f7b5cf63e08fd3e73fce88daf23bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Wed, 13 Jan 2021 18:48:08 +0100 Subject: [PATCH 06/13] Added several isUnresolvedTrack checks --- src/structures/Utils.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/structures/Utils.ts b/src/structures/Utils.ts index 103d129..d9721e5 100644 --- a/src/structures/Utils.ts +++ b/src/structures/Utils.ts @@ -94,12 +94,7 @@ export abstract class TrackUtils { throw new RangeError('Track must be a "Track", not "Track[]".'); if (queue.size == 0) return true; - return queue.filter(p => { - if (p.uri === undefined) - return p.title === track.title - else - return p.uri === track.uri - }).length === 0; + return queue.filter(p => TrackUtils.isUnresolvedTrack(track) || TrackUtils.isUnresolvedTrack(p) ? track.title === p.title : track.identifier === p.identifier).length === 0; } /** @@ -110,7 +105,7 @@ export abstract class TrackUtils { static getUnique(track: unknown, queue: Queue): Track[] { if (!Array.isArray(track)) throw new RangeError('Track must be "Track[]", not "Track".'); - return track.filter(track => !queue.find(ext => TrackUtils.isUnresolvedTrack(track) ? track.title === ext.title : track.identifier === ext.identifier)); + return track.filter(track => !queue.find(ext => TrackUtils.isUnresolvedTrack(track) || TrackUtils.isUnresolvedTrack(ext) ? track.title === ext.title : track.identifier === ext.identifier)); } /** From 1cd32655680eca4608743547b3ec02338e5a2fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Sat, 20 Feb 2021 21:10:33 +0100 Subject: [PATCH 07/13] Added Region methods --- src/structures/Manager.ts | 45 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/structures/Manager.ts b/src/structures/Manager.ts index e83678d..df16232 100644 --- a/src/structures/Manager.ts +++ b/src/structures/Manager.ts @@ -228,8 +228,49 @@ export class Manager extends EventEmitter { }); } - /** - * Initiates the Manager class. + /** + * Returns the Node in same region as server. + * @param region + */ + public nearestNode(region): Node { + const nodes = this.nodes + .filter((node) => { + if (!node.options.region) + return node.connected + else if (Array.isArray(node.options.region)) + return node.connected && node.options.region.includes(region) + else + return node.connected && node.options.region.toLowerCase() == region.toLowerCase() + }); + + if (!nodes) + return null; + else if (nodes.size === 1) + return nodes.first(); + else if (nodes.size > 1) + return this.leastLoadNodesByRegion(nodes).first(); + } + + /** + * Returns the least system load Nodes from provided Nodes. + * @param nodes + */ + public leastLoadNodesByRegion(nodes): Collection { + return nodes + .filter((node) => node.connected) + .sort((a, b) => { + const aload = a.stats.cpu + ? (a.stats.cpu.systemLoad / a.stats.cpu.cores) * 100 + : 0; + const bload = b.stats.cpu + ? (b.stats.cpu.systemLoad / b.stats.cpu.cores) * 100 + : 0; + return aload - bload; + }); + } + + /** + * Initiates the Manager class. * @param options */ constructor(options: ManagerOptions) { From 07c822418e54c55c313bb52f903b3f4581b60928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Sat, 20 Feb 2021 21:15:56 +0100 Subject: [PATCH 08/13] Added regions --- src/structures/Node.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/structures/Node.ts b/src/structures/Node.ts index bc89226..3ebf37c 100644 --- a/src/structures/Node.ts +++ b/src/structures/Node.ts @@ -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" && + ((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.'); } export class Node { @@ -390,6 +395,8 @@ export interface NodeOptions { retryAmount?: number; /** The retryDelay for the node. */ retryDelay?: number; + /** Regions for which the node can be used */ + region?: string | string[]; } export interface NodeStats { From 49ce7191771919fac743e6c58a42a85e372f2d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Sat, 20 Feb 2021 21:23:32 +0100 Subject: [PATCH 09/13] Added region --- src/structures/Player.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/structures/Player.ts b/src/structures/Player.ts index bc904f9..de3bc1e 100644 --- a/src/structures/Player.ts +++ b/src/structures/Player.ts @@ -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.'); if (options.textChannel && !/^\d+$/.test(options.textChannel)) throw new TypeError( @@ -62,6 +65,8 @@ export class Player { public node: Node; /** The guild the player. */ public guild: string; + /** The region of Discord server. */ + public region: string | null = null; /** The voice channel for the player. */ public voiceChannel: string | null = null; /** The text channel for the player. */ @@ -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); + if (!this.node) this.node = this.manager.leastLoadNodes.first() if (!this.node) throw new RangeError("No available nodes."); @@ -448,6 +455,8 @@ export class Player { export interface PlayerOptions { /** The guild the Player belongs to. */ guild: string; + /** The region of Discord server. */ + region?: string; /** The text channel the Player belongs to. */ textChannel: string; /** The voice channel the Player belongs to. */ From fd7e2593bb26a74f7324fe43dee04cfb36287c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Sat, 20 Feb 2021 21:27:56 +0100 Subject: [PATCH 10/13] Fixes --- src/structures/Manager.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/Manager.ts b/src/structures/Manager.ts index df16232..efe391c 100644 --- a/src/structures/Manager.ts +++ b/src/structures/Manager.ts @@ -232,7 +232,7 @@ export class Manager extends EventEmitter { * Returns the Node in same region as server. * @param region */ - public nearestNode(region): Node { + public nearestNode(region: string): Node { const nodes = this.nodes .filter((node) => { if (!node.options.region) @@ -255,7 +255,7 @@ export class Manager extends EventEmitter { * Returns the least system load Nodes from provided Nodes. * @param nodes */ - public leastLoadNodesByRegion(nodes): Collection { + public leastLoadNodesByRegion(nodes: Collection): Collection { return nodes .filter((node) => node.connected) .sort((a, b) => { From 1b0d809b47149e488c1e1ff75e67e1bb2af45eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Sat, 20 Feb 2021 21:32:52 +0100 Subject: [PATCH 11/13] lmao --- src/structures/Queue.ts | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/structures/Queue.ts b/src/structures/Queue.ts index 3f5db7a..7d272eb 100644 --- a/src/structures/Queue.ts +++ b/src/structures/Queue.ts @@ -115,19 +115,4 @@ export class Queue extends Array { [this[i], this[j]] = [this[j], this[i]]; } } - - /** Remove duplicates from the queue. */ - public removeDuplicates(): void { - if (this.totalSize == 0) - throw new RangeError(`The queue must not be empty`); - else if (this.totalSize < 2) - throw new RangeError(`The queue must contain more than one song`); - const tracks = this.reduce((accumulator, currentValue) => { - if (!accumulator.find(obj => obj["title"] === currentValue["title"])) - accumulator.push(currentValue); - return accumulator; - }, []); - this.clear(); - this.add(tracks); - } } From 8d6b7cac135d015c0ba5a850d80be05ade68e397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Sat, 20 Feb 2021 21:33:19 +0100 Subject: [PATCH 12/13] lmao #2 --- src/structures/Utils.ts | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/structures/Utils.ts b/src/structures/Utils.ts index d9721e5..2670bbd 100644 --- a/src/structures/Utils.ts +++ b/src/structures/Utils.ts @@ -79,34 +79,6 @@ export abstract class TrackUtils { throw new RangeError("Provided argument must be present."); return track[TRACK_SYMBOL] === true; } - - /** - * Checks if the provided track is unique. - * @param track - * @param queue - */ - static isUnique(track: Track, queue: Queue): boolean { - if (typeof track === "undefined") - throw new RangeError("Provided argument (track) must be present."); - if (typeof queue === "undefined") - throw new RangeError("Provided argument (queue) must be present."); - if (Array.isArray(track)) - throw new RangeError('Track must be a "Track", not "Track[]".'); - if (queue.size == 0) - return true; - return queue.filter(p => TrackUtils.isUnresolvedTrack(track) || TrackUtils.isUnresolvedTrack(p) ? track.title === p.title : track.identifier === p.identifier).length === 0; - } - - /** - * Returns only unique tracks. - * @param track - * @param queue - */ - static getUnique(track: unknown, queue: Queue): Track[] { - if (!Array.isArray(track)) - throw new RangeError('Track must be "Track[]", not "Track".'); - return track.filter(track => !queue.find(ext => TrackUtils.isUnresolvedTrack(track) || TrackUtils.isUnresolvedTrack(ext) ? track.title === ext.title : track.identifier === ext.identifier)); - } /** * Builds a Track from the raw data from Lavalink and a optional requester. From bb2614e5e1493d1a2f5f50b0143d8002c13049bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= <59953812+karelkryda@users.noreply.github.com> Date: Sat, 20 Feb 2021 21:35:31 +0100 Subject: [PATCH 13/13] Delete codeql-analysis.yml --- .github/workflows/codeql-analysis.yml | 67 --------------------------- 1 file changed, 67 deletions(-) delete mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 982c1b5..0000000 --- a/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,67 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL" - -on: - push: - branches: [ master ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ master ] - schedule: - - cron: '17 12 * * 0' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - language: [ 'javascript' ] - # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] - # Learn more: - # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main - - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 - - # ℹī¸ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl - - # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1