Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/content/docs/basics/player.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Player

| Option | Default | Description |
| -------------------- | ------------ | -------------------------------------------------------------------------------------- |
| `nodes` | required | Array of node options |
| `nodes` | optional | Array of options for node creation on init |
| `forwardVoiceUpdate` | required | Callback to send voice gateway payloads to Discord |
| `autoInit` | `true` | Call `init()` automatically on Discord `READY` |
| `autoSync` | `true` | Push local queue state to Lavalink when a node reconnects without resuming |
Expand Down
11 changes: 2 additions & 9 deletions src/main/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Playlist, Queue, QueueManager, Track } from "@/queue";
import { EventEmitter } from "node:events";

import type {
CreateNodeOptions,
CreateQueueOptions,
MergeUnionType,
PlayOptions,
Expand Down Expand Up @@ -38,7 +37,6 @@ export class Player<
#initPromise: Promise<void> | null = null;

#clientId: string | null = null;
#nodes: CreateNodeOptions[] | null = null;

readonly options: PlayerInstanceOptions;
readonly plugins: PluginRecord<Plugins>;
Expand All @@ -52,12 +50,8 @@ export class Player<

const _options = { ...DefaultPlayerOptions, ...options };

if (_options.nodes.length === 0) throw new Error("Missing node create options");
if (typeof _options.forwardVoiceUpdate !== "function") throw new Error("Missing voice update function");

this.#nodes = _options.nodes;
delete (_options as Partial<typeof _options>).nodes;

this.options = _options;
this.plugins = {} as PluginRecord<Plugins>;

Expand Down Expand Up @@ -95,18 +89,17 @@ export class Player<
return this.#clientId;
}

async init(clientId: string) {
async init(clientId: string, nodes = this.options.nodes!) {
if (this.#initPromise !== null) return this.#initPromise;
if (this.#initialized) return;
const resolver = Promise.withResolvers<void>();
this.#initPromise = resolver.promise;
this.#clientId = clientId;
try {
for (const node of this.#nodes!) this.nodes.create(node);
nodes?.forEach((node) => this.nodes.create(node));
for (const name in this.plugins) (this.plugins as { [x: string]: PlayerPlugin })[name]!.init(this);
await this.nodes.connect();
this.#initialized = true;
this.#nodes = null;
(this as Player).emit("init");
resolver.resolve();
} catch (err) {
Expand Down
9 changes: 3 additions & 6 deletions src/types/main/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export type PluginRecord<Plugins extends PlayerPlugin[]> = {
*/
export interface PlayerOptions<Plugins extends PlayerPlugin[] = PlayerPlugin[]> {
/**
* Options for creating node(s)
* Options for creating nodes on init
*/
nodes: CreateNodeOptions[];
nodes?: CreateNodeOptions[];

/**
* Plugins to initialize after creating nodes
Expand Down Expand Up @@ -114,10 +114,7 @@ export interface PlayerOptions<Plugins extends PlayerPlugin[] = PlayerPlugin[]>
fetchRelatedTracks?: (queue: Queue, track: Track) => Promise<Track[]>;
}

export type PlayerInstanceOptions = Omit<
RequiredProp<PlayerOptions, keyof typeof DefaultPlayerOptions>,
"nodes" | "plugins"
>;
export type PlayerInstanceOptions = Omit<RequiredProp<PlayerOptions, keyof typeof DefaultPlayerOptions>, "plugins">;

/**
* Voice state update payload
Expand Down
Loading