Skip to content

Commit

Permalink
feat: add PlayerManager
Browse files Browse the repository at this point in the history
  • Loading branch information
larsrickert committed May 3, 2022
1 parent 8390eec commit a1d8ed9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/includes/player-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { PlayerOptions } from "../types/player";
import { Player } from "./player";

export class PlayerManager {
private players: Player[] = [];

/**
* Creates a new PlayerManager for easier managing multiple guilds. Your bot should only have one PlayerManager.
*
* @param options Options that should be applied for all guilds. Guild specific options can be overridden when calling `getPlayer`.
*/
constructor(private readonly options?: PlayerOptions) {}

/**
* Gets an existing player for the given guildId or creates one if it does not exist.
*
* @param optionOverrides Option overrides for the guild player. Will be merged with options passed to the PlayerManager.
*/
getPlayer(guildId: string, optionOverrides?: Partial<PlayerOptions>): Player {
const player = this.players.find((p) => p.guildId === guildId);
if (player) return player;

const newPlayer = new Player(guildId, {
...this.options,
...optionOverrides,
});
this.players.push(newPlayer);
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);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./includes/player";
export * from "./includes/player-manager";
export * from "./types/player";
export * from "./types/tracks";

0 comments on commit a1d8ed9

Please sign in to comment.