From 35f3635096b426a82e3d9db4e2b95c8e691ad661 Mon Sep 17 00:00:00 2001 From: Ian Wijma Date: Sun, 5 Nov 2023 00:35:57 +1100 Subject: [PATCH] Added commands --- src/commands/mute.js | 11 +++++++++++ src/commands/next.js | 9 ++++++--- src/commands/pause.js | 11 +++++++++++ src/commands/play.js | 11 +++++++++++ src/commands/previous.js | 11 +++++++++++ src/commands/repeat.js | 34 ++++++++++++++++++++++++++++++++++ src/commands/shuffle.js | 37 +++++++++++++++++++++++++++++++++++++ src/commands/volume.js | 11 +++++++++++ 8 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 src/commands/mute.js create mode 100644 src/commands/pause.js create mode 100644 src/commands/play.js create mode 100644 src/commands/previous.js create mode 100644 src/commands/repeat.js create mode 100644 src/commands/shuffle.js create mode 100644 src/commands/volume.js diff --git a/src/commands/mute.js b/src/commands/mute.js new file mode 100644 index 0000000..8fffa68 --- /dev/null +++ b/src/commands/mute.js @@ -0,0 +1,11 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = 'mute' +exports.handler = async () => { + try { + await spotify.ensure(); + await spotify.setVolume(0); + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file diff --git a/src/commands/next.js b/src/commands/next.js index 9186347..4cf66b9 100644 --- a/src/commands/next.js +++ b/src/commands/next.js @@ -2,7 +2,10 @@ const { spotify} = require('../utilities/spotify') exports.command = 'next' exports.handler = async () => { - spotify.ensure() - .then(spotify => spotify.skipToNext()) - .catch(e => console.error(e.message)) + try { + await spotify.ensure(); + await spotify.skipToNext(); + } catch (e) { + console.error(e.message); + } }; \ No newline at end of file diff --git a/src/commands/pause.js b/src/commands/pause.js new file mode 100644 index 0000000..55e8248 --- /dev/null +++ b/src/commands/pause.js @@ -0,0 +1,11 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = 'pause' +exports.handler = async () => { + try { + await spotify.ensure(); + await spotify.pause(); + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file diff --git a/src/commands/play.js b/src/commands/play.js new file mode 100644 index 0000000..180ccb8 --- /dev/null +++ b/src/commands/play.js @@ -0,0 +1,11 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = 'play' +exports.handler = async () => { + try { + await spotify.ensure(); + await spotify.play(); + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file diff --git a/src/commands/previous.js b/src/commands/previous.js new file mode 100644 index 0000000..55f8317 --- /dev/null +++ b/src/commands/previous.js @@ -0,0 +1,11 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = ['previous', 'prev'] +exports.handler = async () => { + try { + await spotify.ensure(); + await spotify.skipToPrevious(); + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file diff --git a/src/commands/repeat.js b/src/commands/repeat.js new file mode 100644 index 0000000..fefc245 --- /dev/null +++ b/src/commands/repeat.js @@ -0,0 +1,34 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = 'repeat' +exports.handler = argv => argv + .option('all', { + type: 'boolean', + description: 'Repeat every track once', + }) + .option('one', { + type: 'boolean', + description: 'Repeat one track', + }) + .option('off', { + type: 'boolean', + description: 'Turn repeat off', + }) +exports.handler = async ({ all, one }) => { + let state = 'off'; + switch (true) { + case all: + state = 'context'; + break; + case one: + state = 'track'; + break; + } + + try { + await spotify.ensure(); + await spotify.setRepeat(state); + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file diff --git a/src/commands/shuffle.js b/src/commands/shuffle.js new file mode 100644 index 0000000..1071d2f --- /dev/null +++ b/src/commands/shuffle.js @@ -0,0 +1,37 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = 'shuffle' +exports.handler = argv => argv + .option('on', { + type: 'boolean', + description: 'Enables shuffle', + }) + .option('off', { + type: 'boolean', + description: 'Disables shuffle', + }) +exports.handler = async ({ on, off }) => { + try { + let state; + + if (!on && !off) { + const { body } = await spotify.getMyCurrentPlaybackState(); + const { shuffle_state } = body + state = !shuffle_state; // toggle state + } + + switch (true) { + case on: + state = true; + break; + case off: + state = false; + break; + } + + await spotify.ensure(); + await spotify.setShuffle(state); + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file diff --git a/src/commands/volume.js b/src/commands/volume.js new file mode 100644 index 0000000..ddc733e --- /dev/null +++ b/src/commands/volume.js @@ -0,0 +1,11 @@ +const { spotify} = require('../utilities/spotify') + +exports.command = ['volume ', 'vol '] +exports.handler = async ({ volume }) => { + try { + await spotify.ensure(); + await spotify.setVolume(Math.min(Math.max(volume, 0), 100)); + } catch (e) { + console.error(e.message); + } +}; \ No newline at end of file