Skip to content

Commit

Permalink
Fix Playback
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Sep 14, 2024
1 parent f6c02b2 commit 42653cc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Discord/Handlers/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CommandHancler {
'Red'
);
if (error instanceof SpotifyManagerError) embed.setDescription(error.message);
if (error instanceof Error) {
if (!(error instanceof SpotifyManagerError) && error instanceof Error) {
if (!this.discord.client) return;
this.discord.client.users.send(this.discord.Application.config.ownerId, {
embeds: [
Expand Down
3 changes: 3 additions & 0 deletions src/Private/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class RequestHandler {
if ('GET' !== options.method) {
return new RequestData({}, res.headers, { status: res.status, options, url: endpoint, cached: false });
}
if (204 === res.status || 'me/player' === endpoint) {
throw new SpotifyManagerError(this.Application.errors.NOTHING_PLAYING);
}
const parsedRes = (await res.json()) as Record<string, any>;
if (401 === res.status || 403 === res.status) throw new SpotifyManagerError(this.Application.errors.NOT_LOGGED_IN);
const requestData = new RequestData(parsedRes, res.headers, {
Expand Down
13 changes: 1 addition & 12 deletions src/Spotify/Private/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,45 @@ class RequestHandler {

async getStatus(): Promise<Playback> {
const res = await this.spotify.Application.requestHandler.request('/me/player', { noCache: true });
if (204 === res.statusCode) throw new SpotifyManagerError(this.spotify.Application.errors.NOT_LOGGED_IN);
if (204 === res.statusCode) throw new SpotifyManagerError(this.spotify.Application.errors.NOTHING_PLAYING);
return new Playback(await res.data);
}

async skip(): Promise<void> {
const playbackStatus = await this.getStatus();
if (!playbackStatus.playing) throw new SpotifyManagerError(this.spotify.Application.errors.NOTHING_PLAYING);
await this.spotify.Application.requestHandler.request('/me/player/next', {
noCache: true,
method: 'POST'
});
}

async pause(): Promise<void> {
const playbackStatus = await this.getStatus();
if (!playbackStatus.playing) throw new SpotifyManagerError(this.spotify.Application.errors.NOTHING_PLAYING);
await this.spotify.Application.requestHandler.request('/me/player/pause', {
noCache: true,
method: 'PUT'
});
}

async play(): Promise<void> {
const playbackStatus = await this.getStatus();
if (!playbackStatus.playing) throw new SpotifyManagerError(this.spotify.Application.errors.NOTHING_PLAYING);
await this.spotify.Application.requestHandler.request('/me/player/play', {
noCache: true,
method: 'PUT'
});
}

async previous(): Promise<void> {
const playbackStatus = await this.getStatus();
if (!playbackStatus.playing) throw new SpotifyManagerError(this.spotify.Application.errors.NOTHING_PLAYING);
await this.spotify.Application.requestHandler.request('/me/player/previous', {
noCache: true,
method: 'POST'
});
}

async getQueue(): Promise<Queue> {
const playbackStatus = await this.getStatus();
if (!playbackStatus.playing) throw new SpotifyManagerError(this.spotify.Application.errors.NOTHING_PLAYING);
const res = await this.spotify.Application.requestHandler.request('/me/player/queue', { noCache: true });
return new Queue(await res.data);
}

async shuffle(): Promise<void> {
const playbackStatus = await this.getStatus();
if (!playbackStatus.playing) throw new SpotifyManagerError(this.spotify.Application.errors.NOTHING_PLAYING);
await this.spotify.Application.requestHandler.request(`/me/player/shuffle?state=${!playbackStatus.shuffleState}`, {
noCache: true,
method: 'PUT'
Expand Down

0 comments on commit 42653cc

Please sign in to comment.