Added commands

This commit is contained in:
Ian Wijma 2023-11-05 00:35:57 +11:00
parent 64f1d76778
commit 35f3635096
8 changed files with 132 additions and 3 deletions

11
src/commands/mute.js Normal file
View File

@ -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);
}
};

View File

@ -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);
}
};

11
src/commands/pause.js Normal file
View File

@ -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);
}
};

11
src/commands/play.js Normal file
View File

@ -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);
}
};

11
src/commands/previous.js Normal file
View File

@ -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);
}
};

34
src/commands/repeat.js Normal file
View File

@ -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);
}
};

37
src/commands/shuffle.js Normal file
View File

@ -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);
}
};

11
src/commands/volume.js Normal file
View File

@ -0,0 +1,11 @@
const { spotify} = require('../utilities/spotify')
exports.command = ['volume <volume>', 'vol <volume>']
exports.handler = async ({ volume }) => {
try {
await spotify.ensure();
await spotify.setVolume(Math.min(Math.max(volume, 0), 100));
} catch (e) {
console.error(e.message);
}
};