Skip to content

Commit

Permalink
fix: remove unused structures
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi committed Dec 5, 2023
1 parent 51017fd commit 2c87675
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 84 deletions.
50 changes: 0 additions & 50 deletions packages/core/src/Structures/Filter.ts

This file was deleted.

52 changes: 20 additions & 32 deletions packages/core/src/Structures/Kirishima.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,81 @@
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
import { EventEmitter } from "node:events";
import { KirishimaNodeOptions, KirishimaOptions, KirishimaPlayerOptions, LoadTrackResponse } from "../typings/index.js";
import { KirishimaNodeOptions, KirishimaOptions } from "../typings/index.js";
import crypto from "node:crypto";

import { KirishimaNode } from "./Node.js";
import { GatewayVoiceServerUpdateDispatch, GatewayVoiceStateUpdateDispatch } from "discord-api-types/gateway/v9";
import { Collection } from "@discordjs/collection";
import { KirishimaPlayer } from "./Player.js";
import { Structure } from "./Structure.js";
import { PlayerOptions } from "@kirishima/types";
import { BasePlayer } from "./BasePlayer.js";

export class Kirishima extends EventEmitter {
public nodes = new Collection<string, KirishimaNode>();
public players?: Collection<string, KirishimaPlayer>;
public players?: Collection<string, BasePlayer>;
public constructor(public options: KirishimaOptions) {
super();

if (typeof options.send !== "function") throw Error("Send function must be present and must be a function.");

if (
typeof options.spawnPlayer !== "function" ||
(typeof options.spawnPlayer === undefined && (typeof options.fetchPlayer !== "function" || typeof options.fetchPlayer === undefined))
(typeof options.spawnPlayer === "undefined" && (typeof options.fetchPlayer !== "function" || typeof options.fetchPlayer === "undefined"))
) {
this.players = new Collection();
options.spawnPlayer = this.defaultSpawnPlayerHandler.bind(this);
}

if (
typeof options.fetchPlayer !== "function" ||
(typeof options.fetchPlayer === undefined && (typeof options.spawnPlayer !== "function" || typeof options.spawnPlayer === undefined))
(typeof options.fetchPlayer === "undefined" && (typeof options.spawnPlayer !== "function" || typeof options.spawnPlayer === "undefined"))
) {
options.fetchPlayer = this.defaultFetchPlayerHandler.bind(this);
}

if (!options.nodes.length) throw new Error("Nodes option must not a empty array");
}

public async initialize(clientId?: string) {
public async initialize(clientId?: string): Promise<Kirishima> {
if (!clientId && !this.options.clientId) throw new Error("Invalid clientId provided");
if (clientId && !this.options.clientId) this.options.clientId = clientId;
if (this.options.plugins) {
for (const plugin of [...this.options.plugins.values()]) {
await plugin.load(this);
}
}
return this.setNodes(this.options.nodes);
}

public async setNodes(nodeOrNodes: KirishimaNodeOptions | KirishimaNodeOptions[]): Promise<Kirishima> {
const isArray = Array.isArray(nodeOrNodes);
if (isArray) {
for (const node of nodeOrNodes) {
const kirishimaNode = new (Structure.get("KirishimaNode"))(node, this);
const kirishimaNode = new KirishimaNode(node, this);
await kirishimaNode.connect();
this.nodes.set(node.identifier ??= crypto.randomBytes(4).toString("hex"), kirishimaNode);
}
return this;
}
const kirishimaNode = new (Structure.get("KirishimaNode"))(nodeOrNodes, this);
const kirishimaNode = new KirishimaNode(nodeOrNodes, this);
await kirishimaNode.connect();
this.nodes.set(nodeOrNodes.identifier ??= crypto.randomBytes(4).toString("hex"), kirishimaNode);
return this;
}

public setClientName(clientName: string) {
public setClientName(clientName: string): this {
this.options.clientName = clientName;
return this;
}

public setClientId(clientId: string) {
public setClientId(clientId: string): this {
this.options.clientId = clientId;
return this;
}

public resolveNode(identifierOrGroup?: string) {
public resolveNode(identifierOrGroup?: string): KirishimaNode | undefined {
const resolveGroupedNode = this.nodes.filter(x => x.connected).find(x => x.options.group?.includes(identifierOrGroup!)!);
if (resolveGroupedNode) return resolveGroupedNode;
const resolveIdenfitierNode = this.nodes.filter(x => x.connected).find(x => x.options.identifier === identifierOrGroup);
if (resolveIdenfitierNode) return resolveIdenfitierNode;
return this.resolveBestNode().first();
}

public resolveBestNode() {
public resolveBestNode(): Collection<string, KirishimaNode> {
return this.nodes
.filter(x => x.connected)
.sort((x, y) => {
Expand All @@ -90,30 +85,23 @@ export class Kirishima extends EventEmitter {
});
}

public async resolveTracks(options: string | { source?: string | undefined; query: string }, node?: KirishimaNode): Promise<LoadTrackResponse> {
node ??= this.resolveNode();
const resolveTracks = await node!.rest.loadTracks(options);
if (resolveTracks.tracks.length) resolveTracks.tracks = resolveTracks.tracks.map(x => new (Structure.get("KirishimaTrack"))(x));
return resolveTracks as unknown as LoadTrackResponse;
}

public spawnPlayer(options: KirishimaPlayerOptions, node?: KirishimaNode) {
public spawnPlayer(options: PlayerOptions, node?: KirishimaNode): unknown {
return this.options.spawnPlayer!(options.guildId, options, node ?? this.resolveNode()!);

Check failure on line 89 in packages/core/src/Structures/Kirishima.ts

View workflow job for this annotation

GitHub Actions / test / lint

Unsafe argument of type `any` assigned to a parameter of type `string`
}

public async handleVoiceServerUpdate(packet: GatewayVoiceServerUpdateDispatch) {
public async handleVoiceServerUpdate(packet: GatewayVoiceServerUpdateDispatch): Promise<void> {
for (const node of [...this.nodes.values()]) {
await node.handleVoiceServerUpdate(packet);
}
}

public async handleVoiceStateUpdate(packet: GatewayVoiceStateUpdateDispatch) {
public async handleVoiceStateUpdate(packet: GatewayVoiceStateUpdateDispatch): Promise<void> {
for (const node of [...this.nodes.values()]) {
await node.handleVoiceStateUpdate(packet);
}
}

public async handleRawPacket(t: "VOICE_SERVER_UPDATE" | "VOICE_STATE_UPDATE", packet: unknown) {
public async handleRawPacket(t: "VOICE_SERVER_UPDATE" | "VOICE_STATE_UPDATE", packet: unknown): Promise<void> {
if (t === "VOICE_STATE_UPDATE") {
await this.handleVoiceStateUpdate(packet as GatewayVoiceStateUpdateDispatch);
}
Expand All @@ -122,15 +110,15 @@ export class Kirishima extends EventEmitter {
}
}

private defaultSpawnPlayerHandler(guildId: string, options: KirishimaPlayerOptions, node: KirishimaNode) {
private defaultSpawnPlayerHandler(guildId: string, options: PlayerOptions, node: KirishimaNode): BasePlayer {
const player = this.players!.has(guildId);
if (player) return this.players!.get(guildId)!;
const kirishimaPlayer = new (Structure.get("KirishimaPlayer"))(options, this, node);
const kirishimaPlayer = new BasePlayer(options, this, node);
this.players!.set(guildId, kirishimaPlayer);
return kirishimaPlayer;
}

private defaultFetchPlayerHandler(guildId: string) {
private defaultFetchPlayerHandler(guildId: string): BasePlayer | undefined {
return this.players!.get(guildId);
}
}
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from "./Structures/Node.js";
export * from "./Structures/Kirishima.js";
export * from "./Structures/Filter.js";
export * from "./Structures/BasePlayer.js";

export * from "./typings/index.js";
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/typings/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
import { Awaitable } from "@sapphire/utilities";
import { KirishimaNode } from "../Structures/Node.js";
import { PlayerOptions, ShardPayload } from "@kirishima/types";
Expand All @@ -21,7 +22,7 @@ export interface KirishimaOptions {

export type SpawnPlayerOptionHook = (guildId: string, options: PlayerOptions, node: KirishimaNode) => Awaitable<unknown>;

export type PlayerOptionHook = (guildId: string) => Awaitable<BasePlayer>;
export type PlayerOptionHook = (guildId: string) => Awaitable<BasePlayer | undefined>;

export interface KirishimaNodeOptions {
identifier?: string;
Expand Down

0 comments on commit 2c87675

Please sign in to comment.