Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: v5: remove player cache #207

Draft
wants to merge 5 commits into
base: wip/v5
Choose a base branch
from
Draft
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
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"url": "https://github.com/shipgirlproject/Shoukaku.git"
},
"dependencies": {
"ws": "^8.18.0"
"ws": "^8.18.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@shipgirl/eslint-config": "^0.4.0",
Expand Down
12 changes: 11 additions & 1 deletion src/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as z from 'zod';
import Info from '../package.json';
// eslint-disable-next-line import-x/no-cycle
import { NodeOption, ShoukakuOptions } from './Shoukaku';
Expand All @@ -18,19 +19,28 @@ export enum VoiceState {
SESSION_FAILED_UPDATE
}

export enum OpCodes {
// export to allow compiler to determine shape
/**
* Websocket operation codes
* @see https://lavalink.dev/api/websocket#op-types
*/
export enum OpCodesEnum {
PLAYER_UPDATE = 'playerUpdate',
STATS = 'stats',
EVENT = 'event',
READY = 'ready'
}

export const OpCodes = z.nativeEnum(OpCodesEnum);
export type OpCode = z.TypeOf<typeof OpCodes>;

export const Versions = {
REST_VERSION: 4,
WEBSOCKET_VERSION: 4
};

export const ShoukakuDefaults: Required<ShoukakuOptions> = {
validate: false,
resume: false,
resumeTimeout: 30,
resumeByLibrary: false,
Expand Down
5 changes: 5 additions & 0 deletions src/Shoukaku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export interface NodeOption {
}

export interface ShoukakuOptions {
/**
* Whether to validate Lavalink responses (worse performance)
*/
validate?: boolean;
/**
* Whether to resume a connection on disconnect to Lavalink (Server Side) (Note: DOES NOT RESUME WHEN THE LAVALINK SERVER DIES)
*/
Expand Down Expand Up @@ -173,6 +177,7 @@ export class Shoukaku extends TypedEventEmitter<ShoukakuEvents> {
* @param connector A Discord library connector
* @param nodes An array that conforms to the NodeOption type that specifies nodes to connect to
* @param options Options to pass to create this Shoukaku instance
* @param options.validate Whether to validate Lavalink responses (worse performance)
* @param options.resume Whether to resume a connection on disconnect to Lavalink (Server Side) (Note: DOES NOT RESUME WHEN THE LAVALINK SERVER DIES)
* @param options.resumeTimeout Time to wait before lavalink starts to destroy the players of the disconnected client
* @param options.resumeByLibrary Whether to resume the players by doing it in the library side (Client Side) (Note: TRIES TO RESUME REGARDLESS OF WHAT HAPPENED ON A LAVALINK SERVER)
Expand Down
6 changes: 4 additions & 2 deletions src/connectors/Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { ServerUpdate, StateUpdatePartial } from '../guild/Connection';
import { NodeOption, Shoukaku } from '../Shoukaku';
import { mergeDefault } from '../Utils';

export type AnyFunction = (...args: any[]) => any;

export interface ConnectorMethods {
sendPacket: any;
getId: any;
sendPacket: AnyFunction;
getId: AnyFunction;
}

export const AllowedPackets = [ 'VOICE_STATE_UPDATE', 'VOICE_SERVER_UPDATE' ];
Expand Down
50 changes: 39 additions & 11 deletions src/guild/Connection.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
import { EventEmitter, once } from 'node:events';
import * as z from 'zod';
// eslint-disable-next-line import-x/no-cycle
import { State, VoiceState } from '../Constants';
import { Shoukaku, VoiceChannelOptions } from '../Shoukaku';

/**
* Represents the partial payload from a stateUpdate event
* @see https://discord.com/developers/docs/resources/voice#voice-state-object
*/
export interface StateUpdatePartial {
channel_id?: string;
session_id?: string;
self_deaf: boolean;
self_mute: boolean;
}
export const StateUpdatePartial = z.object({
/**
* Channel ID the state update is for
*/
channel_id: z.optional(z.string()),
/**
* Session ID the state update is for
*/
session_id: z.optional(z.string()),
/**
* Whether this user is locally deafened
*/
self_deaf: z.boolean(),
/**
* Whether this user is locally muted
*/
self_mute: z.boolean()
});

export type StateUpdatePartial = z.TypeOf<typeof StateUpdatePartial>;

/**
* Represents the payload from a serverUpdate event
* @see https://discord.com/developers/docs/topics/gateway-events#voice-server-update
*/
export interface ServerUpdate {
token: string;
guild_id: string;
endpoint: string;
}
export const ServerUpdate = z.object({
/**
* Voice connection token
*/
token: z.string(),
/**
* Guild this voice server update is for
*/
guild_id: z.string(),
/**
* Voice server hostname
*/
endpoint: z.string()
});

export type ServerUpdate = z.TypeOf<typeof ServerUpdate>;

/**
* Represents a connection to a Discord voice channel
Expand Down
Loading