Skip to content

Commit

Permalink
refactor(player-manager): Add methods find and remove
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove method `exists` in favor of `find`
  • Loading branch information
larsrickert committed May 24, 2022
1 parent a3b1ad0 commit 0458b8f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/engines/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export const fileEngine: PlayerEngine = {
return null;
}

return Promise.resolve({
return {
stream: track.url,
type: StreamType.Arbitrary,
});
};
},
};
2 changes: 0 additions & 2 deletions src/engines/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export async function detectTrackSource(
if (query.startsWith("https://open.spotify.com/")) return "spotify";
if (query.startsWith("https://www.youtube.com/")) return "youtube";

console.log(fileRoot, query);

if (!fileRoot || isSubPath(fileRoot, query)) {
// check if query is file path
try {
Expand Down
3 changes: 1 addition & 2 deletions src/engines/spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { youtubeEngine } from "./youtube";
* Spotify does not provide a web api to stream tracks so the track will be streamed from YouTube instead.
*/
export const spotifyEngine: PlayerEngine = {
search: async (query, options) => {
search: async (query) => {
const isPlaylist = query.startsWith("https://open.spotify.com/playlist");
if (isPlaylist) return await searchPlaylist(query);

Expand Down Expand Up @@ -57,7 +57,6 @@ function mapSpotifyTrack(track: Tracks): Track {
return {
title: track.name,
url: track.external_urls.spotify,
thumbnailUrl: "",
duration: Math.round(track.duration_ms / 1000),
artist: track.artists?.map((a) => a.name).join(", "),
source: "spotify",
Expand Down
2 changes: 1 addition & 1 deletion src/engines/youtube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PlayerEngine, Playlist, SearchResult, Track } from "../types/engines";
* Player engine to search/stream tracks from YouTube.
*/
export const youtubeEngine: PlayerEngine = {
search: async (query, playerOptions, searchOptions) => {
search: async (query, _, searchOptions) => {
const isPlaylist =
query.startsWith("https://www.youtube.com/watch") &&
query.includes("list=");
Expand Down
18 changes: 15 additions & 3 deletions src/player-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,20 @@ export class PlayerManager {
return newPlayer;
}

/** Checks if a player for the given guildId exists. Does not create a player. */
exists(guildId: string): boolean {
return !!this.players.find((player) => player.guildId === guildId);
/**
* Gets the player for the given guildId if it exists. Does not create a player.
*/
find(guildId: string): Player | undefined {
return this.players.find((player) => player.guildId === guildId);
}

/**
* Removes and destroys the player for the given guildId (if any). Will stop playing audio and leave voice channel if connected.
*/
remove(guildId: string): void {
const player = this.find(guildId);
if (!player) return;
player.stop();
this.players = this.players.filter((player) => player.guildId !== guildId);
}
}

0 comments on commit 0458b8f

Please sign in to comment.