Skip to content

Commit

Permalink
feat: convert interfaces to zod objects
Browse files Browse the repository at this point in the history
  • Loading branch information
0t4u committed Oct 29, 2024
1 parent 4aeeb06 commit e6d19be
Show file tree
Hide file tree
Showing 7 changed files with 1,635 additions and 299 deletions.
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
11 changes: 10 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,13 +19,21 @@ 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
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 '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

0 comments on commit e6d19be

Please sign in to comment.