From ad4d49f763e28796ce586def0aa3b1765065b9e3 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sat, 14 Mar 2020 12:41:00 -0500 Subject: [PATCH] Add shuffle command --- package.json | 1 + src/commands/shuffle.ts | 29 ++++++++++++++++ src/inversify.config.ts | 2 ++ src/packages.d.ts | 1 + src/services/queue.ts | 11 ++++++ yarn.lock | 76 +++++------------------------------------ 6 files changed, 52 insertions(+), 68 deletions(-) create mode 100644 src/commands/shuffle.ts diff --git a/package.json b/package.json index dee3c86..0e669f1 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ }, "dependencies": { "@discordjs/opus": "^0.1.0", + "array-shuffle": "^1.0.1", "delay": "^4.3.0", "discord.js": "^12.0.2", "dotenv": "^8.2.0", diff --git a/src/commands/shuffle.ts b/src/commands/shuffle.ts new file mode 100644 index 0000000..4769f50 --- /dev/null +++ b/src/commands/shuffle.ts @@ -0,0 +1,29 @@ +import {Message} from 'discord.js'; +import {TYPES} from '../types'; +import {inject, injectable} from 'inversify'; +import Queue from '../services/queue'; +import Command from '.'; + +@injectable() +export default class implements Command { + public name = 'shuffle'; + public description = 'shuffle current queue'; + private readonly queue: Queue; + + constructor(@inject(TYPES.Services.Queue) queue: Queue) { + this.queue = queue; + } + + public async execute(msg: Message, _: string []): Promise { + const queue = this.queue.get(msg.guild!.id); + + if (queue.length <= 2) { + await msg.channel.send('error: not enough songs to shuffle'); + return; + } + + this.queue.shuffle(msg.guild!.id); + + await msg.channel.send('`' + JSON.stringify(this.queue.get(msg.guild!.id).slice(0, 10)) + '`'); + } +} diff --git a/src/inversify.config.ts b/src/inversify.config.ts index f6f178b..275077c 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -25,6 +25,7 @@ import Config from './commands/config'; import Play from './commands/play'; import QueueCommad from './commands/queue'; import Seek from './commands/seek'; +import Shuffle from './commands/shuffle'; let container = new Container(); @@ -41,6 +42,7 @@ container.bind(TYPES.Command).to(Config).inSingletonScope(); container.bind(TYPES.Command).to(Play).inSingletonScope(); container.bind(TYPES.Command).to(QueueCommad).inSingletonScope(); container.bind(TYPES.Command).to(Seek).inSingletonScope(); +container.bind(TYPES.Command).to(Shuffle).inSingletonScope(); // Config values container.bind(TYPES.Config.DISCORD_TOKEN).toConstantValue(DISCORD_TOKEN); diff --git a/src/packages.d.ts b/src/packages.d.ts index 6364cbd..7e199a3 100644 --- a/src/packages.d.ts +++ b/src/packages.d.ts @@ -1,2 +1,3 @@ declare module 'node-emoji'; declare module 'ytsr'; +declare module 'array-shuffle'; diff --git a/src/services/queue.ts b/src/services/queue.ts index 85777e5..be59c7f 100644 --- a/src/services/queue.ts +++ b/src/services/queue.ts @@ -1,4 +1,5 @@ import {injectable} from 'inversify'; +import shuffle from 'array-shuffle'; export interface QueuedPlaylist { title: string; @@ -84,6 +85,16 @@ export default class { } } + shuffle(guildId: string): void { + const queue = this.guildQueues.get(guildId); + + if (!queue) { + throw new Error('Queue doesn\'t exist yet.'); + } + + this.guildQueues.set(guildId, [queue[0], ...shuffle(queue.slice(1))]); + } + size(guildId: string): number { return this.get(guildId).length; } diff --git a/yarn.lock b/yarn.lock index e35e0b0..e270864 100644 --- a/yarn.lock +++ b/yarn.lock @@ -65,13 +65,6 @@ "@types/node" "*" "@types/responselike" "*" -"@types/cloneable-readable@^2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@types/cloneable-readable/-/cloneable-readable-2.0.0.tgz#b5bc6602d4771a5db9d80f3867427e9da5f68a63" - integrity sha512-Q9fTsA3hEbOXmGIZ7StMunr/SCvtdfXDfJcDadYk/MPbS3Xh/fWCsdhW26NVx1XNNcX3SkdBqPkfbOiD6p3q2Q== - dependencies: - "@types/node" "*" - "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" @@ -121,13 +114,6 @@ resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA== -"@types/pump@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@types/pump/-/pump-1.1.0.tgz#ed5214af511da32b6ee85c8d33ad3d59bb79ad8f" - integrity sha512-YGGbsqf5o7sF8gGANP8ZYxgaRGlFgEAImx5tCvA4YKRCfqbsDQZO48UmWynZzSjbhn0ZWSlsWOcb5NwvOx8KcQ== - dependencies: - "@types/node" "*" - "@types/responselike@*": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.0.tgz#251f4fe7d154d2bad125abe1b429b23afd262e29" @@ -321,6 +307,11 @@ argparse@^1.0.7: dependencies: sprintf-js "~1.0.2" +array-shuffle@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/array-shuffle/-/array-shuffle-1.0.1.tgz#7ea4882a356b4bca5f545e0b6e52eaf6d971557a" + integrity sha1-fqSIKjVrS8pfVF4LblLq9tlxVXo= + asn1@~0.2.3: version "0.2.4" resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.4.tgz#8d2475dfab553bb33e77b54e59e880bb8ce23136" @@ -531,14 +522,6 @@ clone-response@^1.0.2: dependencies: mimic-response "^1.0.0" -cloneable-readable@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/cloneable-readable/-/cloneable-readable-2.0.1.tgz#fc2240beddbe5621b872acad8104dcc86574e225" - integrity sha512-1ke/wckhpSevGPQzKb+qGHMsuFrSUKQlsKh0PTmscmfAzw8MgONqrg5a0e0Un1YO/cOSS4wAepfXSGus5RoonQ== - dependencies: - inherits "^2.0.1" - readable-stream "^3.3.0" - cls-bluebird@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/cls-bluebird/-/cls-bluebird-2.1.0.tgz#37ef1e080a8ffb55c2f4164f536f1919e7968aee" @@ -1373,7 +1356,7 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" -inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3: +inherits@2, inherits@~2.0.3: version "2.0.4" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== @@ -1520,11 +1503,6 @@ is-typedarray@~1.0.0: resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a" integrity sha1-5HnICFjfDBsR3dppQPlgEfzaSpo= -isarray@0.0.1: - version "0.0.1" - resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf" - integrity sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8= - isarray@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" @@ -1692,13 +1670,6 @@ make-error@^1.1.1: resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== -memory-streams@^0.1.3: - version "0.1.3" - resolved "https://registry.yarnpkg.com/memory-streams/-/memory-streams-0.1.3.tgz#d9b0017b4b87f1d92f55f2745c9caacb1dc93ceb" - integrity sha512-qVQ/CjkMyMInPaaRMrwWNDvf6boRZXaT/DbQeMYcCWuXPEBf1v8qChOc9OlEVQp2uOvRXa1Qu30fLmKhY6NipA== - dependencies: - readable-stream "~1.0.2" - methods@^1.1.1: version "1.1.2" resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee" @@ -2233,25 +2204,6 @@ readable-stream@^2.0.6, readable-stream@^2.3.5: string_decoder "~1.1.1" util-deprecate "~1.0.1" -readable-stream@^3.3.0: - version "3.6.0" - resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.0.tgz#337bbda3adc0706bd3e024426a286d4b4b2c9198" - integrity sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA== - dependencies: - inherits "^2.0.3" - string_decoder "^1.1.1" - util-deprecate "^1.0.1" - -readable-stream@~1.0.2: - version "1.0.34" - resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c" - integrity sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw= - dependencies: - core-util-is "~1.0.0" - inherits "~2.0.1" - isarray "0.0.1" - string_decoder "~0.10.x" - readdirp@~3.3.0: version "3.3.0" resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.3.0.tgz#984458d13a1e42e2e9f5841b129e162f369aff17" @@ -2375,7 +2327,7 @@ rxjs@^6.5.3: dependencies: tslib "^1.9.0" -safe-buffer@^5.0.1, safe-buffer@^5.1.2, safe-buffer@~5.2.0: +safe-buffer@^5.0.1, safe-buffer@^5.1.2: version "5.2.0" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.0.tgz#b74daec49b1148f88c64b68d49b1e815c1f2f519" integrity sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg== @@ -2590,18 +2542,6 @@ string-width@^4.1.0: is-fullwidth-code-point "^3.0.0" strip-ansi "^6.0.0" -string_decoder@^1.1.1: - version "1.3.0" - resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.3.0.tgz#42f114594a46cf1a8e30b0a84f56c78c3edac21e" - integrity sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA== - dependencies: - safe-buffer "~5.2.0" - -string_decoder@~0.10.x: - version "0.10.31" - resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94" - integrity sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ= - string_decoder@~1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8" @@ -2882,7 +2822,7 @@ url-parse-lax@^1.0.0: dependencies: prepend-http "^1.0.1" -util-deprecate@^1.0.1, util-deprecate@~1.0.1: +util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=