Skip to content

Commit

Permalink
fix: use BasePlayer instead
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi committed Mar 13, 2022
1 parent 4da9f1e commit facea59
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
36 changes: 36 additions & 0 deletions src/Structures/BasePlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { GatewayVoiceServerUpdateDispatch, GatewayVoiceStateUpdateDispatch } from 'discord-api-types/gateway/v9';
import { WebsocketOpEnum } from 'lavalink-api-types';
import { KirishimaPlayerOptions, Kirishima, KirishimaNode, createVoiceChannelJoinPayload } from '..';

export class BasePlayer {
public voiceServer: GatewayVoiceServerUpdateDispatch['d'] | undefined;
public voiceState: GatewayVoiceStateUpdateDispatch['d'] | undefined;

public constructor(public options: KirishimaPlayerOptions, public kirishima: Kirishima, public node: KirishimaNode) {}

public async connect(): Promise<BasePlayer> {
await this.kirishima.options.send(this.options.guildId, createVoiceChannelJoinPayload(this.options));
return this;
}

public async disconnect(): Promise<BasePlayer> {
await this.kirishima.options.send(this.options.guildId, createVoiceChannelJoinPayload(this.options, true));
return this;
}

public async setServerUpdate(packet: GatewayVoiceServerUpdateDispatch) {
this.voiceServer = packet.d;
if (!this.voiceState?.session_id) return;

await this.node.ws.send({
op: WebsocketOpEnum.VOICE_UPDATE,
guildId: this.voiceServer.guild_id,
sessionId: this.voiceState.session_id,
event: this.voiceServer
});
}

public setStateUpdate(packet: GatewayVoiceStateUpdateDispatch) {
this.voiceState = packet.d;
}
}
4 changes: 2 additions & 2 deletions src/Structures/Kirishima.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { KirishimaNodeOptions, KirishimaOptions, KirishimaPlayerOptions } f
import crypto from 'node:crypto';

import { KirishimaNode } from './KirishimaNode';
import { GatewayOpcodes, GatewayVoiceServerUpdateDispatch, GatewayVoiceStateUpdateDispatch } from 'discord-api-types/gateway/v9';
import { GatewayVoiceServerUpdateDispatch, GatewayVoiceStateUpdateDispatch } from 'discord-api-types/gateway/v9';
import Collection from '@discordjs/collection';
import { KirishimaPlayer } from './KirishimaPlayer';
import { LoadTrackResponse } from 'lavalink-api-types';
Expand Down Expand Up @@ -101,7 +101,7 @@ export class Kirishima extends EventEmitter {
public async spawnPlayer(options: KirishimaPlayerOptions, node?: KirishimaNode) {
node ??= this.resolveNode();
const player = await this.options.spawnPlayer!(options.guildId, options, node!);
return (player as KirishimaPlayer).connect();
return player.connect();
}

public async handleVoiceServerUpdate(packet: GatewayVoiceServerUpdateDispatch) {
Expand Down
4 changes: 2 additions & 2 deletions src/Structures/KirishimaNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ export class KirishimaNode {
public async handleVoiceServerUpdate(packet: GatewayVoiceServerUpdateDispatch) {
const player = await this.kirishima.options.fetchPlayer!(packet.d.guild_id);
if (player) {
await (player as KirishimaPlayer).setServerUpdate(packet);
await player.setServerUpdate(packet);
}
}

public async handleVoiceStateUpdate(packet: GatewayVoiceStateUpdateDispatch) {
const player = await this.kirishima.options.fetchPlayer!(packet.d.guild_id!);
if (player) {
(player as KirishimaPlayer).setStateUpdate(packet);
player.setStateUpdate(packet);
}
}
}
37 changes: 6 additions & 31 deletions src/Structures/KirishimaPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Kirishima } from './Kirishima';
import type { KirishimaPlayerOptions } from '../typings/index';
import { createVoiceChannelJoinPayload, isTrack, KirishimaNode } from '../index';
import { GatewayVoiceServerUpdateDispatch, GatewayVoiceStateUpdateDispatch } from 'discord-api-types/gateway/v9';
import { isTrack, KirishimaNode } from '../index';

import {
ChannelMixEqualizer,
DistortionEqualizer,
Expand All @@ -17,37 +17,12 @@ import {
import { KirishimaTrack } from './Track/KirishimaTrack';
import { KirishimaFilter, KirishimaFilterOptions } from './KirishimaFilter';
import { Structure } from './Structure';
import { BasePlayer } from './BasePlayer';

export class KirishimaPlayer {
public voiceServer: GatewayVoiceServerUpdateDispatch['d'] | undefined;
public voiceState: GatewayVoiceStateUpdateDispatch['d'] | undefined;
export class KirishimaPlayer extends BasePlayer {
public filters = new (Structure.get('KirishimaFilter'))();
public constructor(public options: KirishimaPlayerOptions, public kirishima: Kirishima, public node: KirishimaNode) {}

public async connect(): Promise<KirishimaPlayer> {
await this.kirishima.options.send(this.options.guildId, createVoiceChannelJoinPayload(this.options));
return this;
}

public async disconnect(): Promise<KirishimaPlayer> {
await this.kirishima.options.send(this.options.guildId, createVoiceChannelJoinPayload(this.options, true));
return this;
}

public async setServerUpdate(packet: GatewayVoiceServerUpdateDispatch) {
this.voiceServer = packet.d;
if (!this.voiceState?.session_id) return;

await this.node.ws.send({
op: WebsocketOpEnum.VOICE_UPDATE,
guildId: this.voiceServer.guild_id,
sessionId: this.voiceState.session_id,
event: this.voiceServer
});
}

public setStateUpdate(packet: GatewayVoiceStateUpdateDispatch) {
this.voiceState = packet.d;
public constructor(public options: KirishimaPlayerOptions, public kirishima: Kirishima, public node: KirishimaNode) {
super(options, kirishima, node);
}

public async playTrack(track: KirishimaTrack | string, options?: { noReplace?: boolean; pause?: boolean; startTime?: number; endTime?: number }) {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * from './Structures/Track/KirishimaPartialTrack';
export * from './Structures/Track/KirishimaTrack';
export * from './Structures/KirishimaFilter';
export * from './Structures/KirishimaPlugin';
export * from './Structures/BasePlayer';
export * from './typings/index';
export * from './Util';
5 changes: 3 additions & 2 deletions src/typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { KirishimaTrack } from '../Structures/Track/KirishimaTrack';
import type { KirishimaPartialTrack } from '../Structures/Track/KirishimaPartialTrack';
import { KirishimaFilter } from '../Structures/KirishimaFilter';
import { KirishimaPlugin } from '../Structures/KirishimaPlugin';
import { BasePlayer } from '../Structures/BasePlayer';

export interface KirishimaOptions {
clientId?: string;
Expand All @@ -27,11 +28,11 @@ export interface KirishimaOptions {
}

export interface SpawnPlayerOptionHook {
(guildId: string, options: KirishimaPlayerOptions, node: KirishimaNode): Awaitable<unknown>;
(guildId: string, options: KirishimaPlayerOptions, node: KirishimaNode): Awaitable<BasePlayer>;
}

export interface PlayerOptionHook {
(guildId: string): Awaitable<unknown | undefined>;
(guildId: string): Awaitable<BasePlayer | undefined>;
}
export interface payload {
op: GatewayOpcodes;
Expand Down

0 comments on commit facea59

Please sign in to comment.