Skip to content

Commit

Permalink
feat(player-manager): add events
Browse files Browse the repository at this point in the history
  • Loading branch information
larsrickert committed Jun 19, 2022
1 parent fd23641 commit 251b07c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/player-manager.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { deepmerge } from "deepmerge-ts";
import { TypedEmitter } from "tiny-typed-emitter";

import en from "./languages/en.json";
import { Player } from "./player";
import { Translations } from "./types/commands";
import { PlayerManagerOptions, PlayerOptions } from "./types/player";
import {
PlayerManagerEvents,
PlayerManagerOptions,
PlayerOptions,
} from "./types/player";

export class PlayerManager {
export class PlayerManager extends TypedEmitter<PlayerManagerEvents> {
private players: Player[] = [];

/**
* Creates a new PlayerManager for easier managing multiple guilds. Your bot should only have one PlayerManager.
*/
constructor(public readonly options?: PlayerManagerOptions) {}
constructor(public readonly options?: PlayerManagerOptions) {
super();
}

get translations(): Translations {
return this.options?.translations ?? en;
Expand All @@ -32,6 +40,12 @@ export class PlayerManager {
if (!options && optionOverrides) options = optionOverrides;

const newPlayer = new Player(guildId, options);
newPlayer
.on("trackStart", (track) => this.emit("trackStart", guildId, track))
.on("trackEnd", () => this.emit("trackEnd", guildId))
.on("error", (error) => this.emit("error", guildId, error))
.on("destroyed", () => this.emit("destroyed", guildId));

this.players.push(newPlayer);
return newPlayer;
}
Expand Down
6 changes: 6 additions & 0 deletions src/types/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export interface PlayerEvents {
error: (error: PlayerError) => void;
}

export type PlayerManagerEvents = {
[K in keyof PlayerEvents]: PlayerEvents[K] extends (...a: infer U) => infer R
? (guildId: string, ...a: U) => R
: never;
};

export interface PlayOptions {
/** Voice channel to play in. */
channel: VoiceBasedChannel;
Expand Down

0 comments on commit 251b07c

Please sign in to comment.