lunify.js
Version:
A basic api wrapper for the spotify api covering the oauth routes.
210 lines • 6.33 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Player = void 0;
const CurrentPlayback_1 = require("./CurrentPlayback");
const devices_1 = require("../../managers/devices");
__exportStar(require("./CurrentPlayback"), exports);
__exportStar(require("./Device"), exports);
class Player {
client;
user;
devices;
constructor(client, user) {
this.client = client;
this.user = user;
this.devices = new devices_1.PlayerDeviceManager(this.client, this);
}
/**
* Get current plackback (⚠️ This data is fetched from the api, not from a websocket, consider rate limits)
* @example ```ts
* const playing = player.now();
* console.log(playing)
* ```
*/
async now() {
const res = await this.client.rest.get("/me/player", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
}
});
if (!res)
return null;
return new CurrentPlayback_1.CurrentPlayback(this.client, this, res);
}
/**
* Start playing a track on users current device
* @param {string | string[]} track - A spotify track id or a list of spotify track ids
* @example ```ts
* player.start('6IRdLKIyS4p7XNiP8r6rsx');
* ```
*/
async play(trackId) {
const finalTracks = [];
if (typeof trackId === "string") {
finalTracks.push("spotify:track:" + trackId);
}
else {
for (const t of trackId)
finalTracks.push("spotify:track:" + t);
}
await this.client.rest.put("/me/player/play", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
},
body: {
uris: finalTracks
}
});
return true;
}
/**
* Resume playback on users current device with paused track
* @example ```ts
* player.resume();
* ```
* Use `.play(...)` to start playing a new track
*/
async resume() {
await this.client.rest.put("/me/player/play", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
}
});
return true;
}
/**
* Stop playback on users current device
* @example ```ts
* player.stop();
* ```
*/
async stop() {
await this.client.rest.put("/me/player/pause", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
}
});
return true;
}
/**
* Skip the current track and start playing the next one
* @example ```ts
* player.skip();
* ```
*/
async skip() {
await this.client.rest.post("/me/player/next", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
}
});
return true;
}
/**
* Start playing the track before the current one (starts playing previous track)
* @example ```ts
* player.rewind();
* ```
*/
async rewind() {
await this.client.rest.post("/me/player/previous", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
}
});
return true;
}
/**
* Seek to any position in the current playback
* @param {number} position - position in milliseconds (seconds x1000)
* @example ```ts
* player.seek(16 * 1000); // 16 seconds
* ```
*/
async seekTo(position) {
await this.client.rest.put("/me/player/seek", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
},
query: {
position_ms: position
}
});
return true;
}
/**
* Set the repeat mode for the current playback
* @param {'track' | 'context' | false} mode - track, context, false
* - track: repeat the current track
* - context: repeat the current context
* - false: turn repeat off
* @example ```ts
* player.repeat('track');
* player.repeat('context');
* player.repeat(false);
* ```
*/
async repeat(mode) {
await this.client.rest.put("/me/player/repeat", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
},
query: {
state: mode || "off"
}
});
return true;
}
/**
* Set the volume for the current playback device
* @param {number} percentage - volume in percent %
* @example ```ts
* player.volumeTo(69); // 69%
* ```
*/
async volumeTo(percentage) {
await this.client.rest.put("/me/player/volume", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
},
query: {
volume_percent: percentage
}
});
return true;
}
/**
* Toggle shuffle on or off for playback
* @param {number} state - volume in percent %
* @example ```ts
* player.shuffle(true);
* ```
*/
async shuffle(state) {
await this.client.rest.put("/me/player/shuffle", {
headers: {
Authorization: await this.user.oauth.getAuthorization()
},
query: {
state
}
});
return true;
}
}
exports.Player = Player;
//# sourceMappingURL=index.js.map