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

Add bindings for some libraries built in #129

Merged
merged 2 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ jspm_packages/
.env

# Build
dist
/*.js
/*.d.ts
/*.js.map
lib/
docs

25 changes: 17 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "lavacord",
"version": "2.0.0",
"description": "A simple by design lavalink wrapper made for any discord library.",
"main": "dist/index",
"types": "dist/index.d.ts",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"prepublishOnly": "yarn compile",
"lint": "eslint --fix --ext ts src",
Expand Down Expand Up @@ -35,23 +35,32 @@
},
"homepage": "https://github.com/MrJacz/lavacord#readme",
"dependencies": {
"lavalink-types": "^2.0.1",
"lavalink-types": "^2.0.2",
"undici": "^5.21.0",
"ws": "^8.13.0"
},
"devDependencies": {
"@types/node": "^18.15.3",
"@types/node": "^18.15.10",
"@types/ws": "^8.5.4",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"eslint": "^8.36.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"typedoc": "^0.23.27",
"typedoc": "^0.23.28",
"typescript": "^4.9.5"
},
"optionalDependencies": {
"discord.js": "14.x",
"eris": "0.17.x",
"detritus-client": "0.16.x",
"cloudstorm": "0.7.x"
},
"files": [
"dist",
"/*.js",
"/*.d.ts",
"/*.js.map",
"lib",
"LICENCE"
],
"engines": {
Expand Down
20 changes: 20 additions & 0 deletions src/cloudstorm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Manager as BaseManager } from "./lib/Manager";
import type { ManagerOptions, LavalinkNodeOptions } from "./lib/Types";

import { Client } from "cloudstorm";

export * from "./index";

export class Manager extends BaseManager {
public constructor(public readonly client: Client, nodes: Array<LavalinkNodeOptions>, options: ManagerOptions) {
super(nodes, options);

if (!this.send) {
this.send = packet => {
if (!this.client.options.totalShards) return;
const shardID = Number((BigInt(packet.d.guild_id) >> BigInt(22)) % BigInt(this.client.options.totalShards));
if (this.client.shardManager.shards[shardID]) this.client.shardManager.shards[shardID].connector.betterWs.sendMessage(packet);
};
}
}
}
31 changes: 31 additions & 0 deletions src/detritus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Manager as BaseManager } from "./lib/Manager";
import type { ManagerOptions, LavalinkNodeOptions } from "./lib/Types";

import type { ClusterClient, ShardClient } from "detritus-client";

export * from "./index";

export class Manager extends BaseManager {
public constructor(public readonly client: ClusterClient | ShardClient, nodes: Array<LavalinkNodeOptions>, options: ManagerOptions) {
super(nodes, options);

if (!this.send) {
this.send = packet => {
const asCluster = this.client as ClusterClient;
const asShard = this.client as ShardClient;
if (asShard.guilds) {
asShard.gateway.send(packet.op, packet.d);
} else if (asCluster.shards) {
const shard = asCluster.shards.find(c => c.guilds.has(packet.d.guild_id));
if (shard) shard.gateway.send(packet.op, packet.d);
}
};
}

client.on("raw", packet => {
if (packet.t === "VOICE_SERVER_UPDATE") this.voiceServerUpdate(packet.d);
else if (packet.t === "VOICE_STATE_UPDATE") this.voiceStateUpdate(packet.d);
else if (packet.t === "GUILD_CREATE") for (const state of packet.d.voice_states ?? []) this.voiceStateUpdate({ ...state, guild_id: packet.d.id });
});
}
}
34 changes: 34 additions & 0 deletions src/discord.js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Manager as BaseManager } from "./lib/Manager";
import type { ManagerOptions, LavalinkNodeOptions } from "./lib/Types";

import { Client, GatewayDispatchEvents } from "discord.js";

export * from "./index";

export class Manager extends BaseManager {
public constructor(public readonly client: Client, nodes: Array<LavalinkNodeOptions>, options?: ManagerOptions) {
if (!options) options = {};
if (!options.user) options.user = client.user?.id ? client.user.id : undefined;
super(nodes, options);

if (!this.send) {
this.send = packet => {
const guild = this.client.guilds.cache.get(packet.d.guild_id);
if (guild) guild.shard.send(packet);
};
}

if (!client.readyAt && !this.user) {
client.once("shardReady", () => {
if (this.client.user?.id) this.user = this.client.user.id;
});
}

client.ws
.on(GatewayDispatchEvents.VoiceServerUpdate, this.voiceServerUpdate.bind(this))
.on(GatewayDispatchEvents.VoiceStateUpdate, this.voiceStateUpdate.bind(this))
.on(GatewayDispatchEvents.GuildCreate, data => {
for (const state of data.voice_states ?? []) this.voiceStateUpdate({ ...state, guild_id: data.id });
});
}
}
33 changes: 33 additions & 0 deletions src/eris.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Manager as BaseManager } from "./lib/Manager";
import type { ManagerOptions, LavalinkNodeOptions, DiscordPacket } from "./lib/Types";

import type { Client } from "eris";

export * from "./index";

export class Manager extends BaseManager {
public constructor(public readonly client: Client, nodes: Array<LavalinkNodeOptions>, options?: ManagerOptions) {
if (!options) options = {};
if (!options.user) options.user = client.user?.id ? client.user.id : undefined;
super(nodes, options);

if (!this.send) {
this.send = packet => {
const guild = this.client.guilds.get(packet.d.guild_id);
if (guild) guild.shard.sendWS(packet.op, packet.d);
};
}

if (!client.ready) {
client.once("ready", () => {
this.user = client.user.id;
});
}

client.on("rawWS", (packet: DiscordPacket) => {
if (packet.t === "VOICE_SERVER_UPDATE") this.voiceServerUpdate(packet.d);
else if (packet.t === "VOICE_STATE_UPDATE") this.voiceStateUpdate(packet.d);
else if (packet.t === "GUILD_CREATE") for (const state of packet.d.voice_states ?? []) this.voiceStateUpdate({ ...state, guild_id: packet.d.id });
});
}
}
9 changes: 6 additions & 3 deletions src/lib/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ export class Manager extends EventEmitter {
* @param nodes A Array of {@link LavalinkNodeOptions} that the Manager will connect to
* @param options The options for the Manager {@link ManagerOptions}
*/
public constructor(nodes: LavalinkNodeOptions[], options: ManagerOptions) {
public constructor(nodes: Array<LavalinkNodeOptions>, options: ManagerOptions) {
super();

if (options.user) this.user = options.user;
if (options.player) this.Player = options.player;
if (options.send) this.send = options.send;

for (const node of nodes) this.createNode(node);
setImmediate(() => {
if (this.send) this.send = () => undefined;
});
}

/**
Expand All @@ -68,7 +71,7 @@ export class Manager extends EventEmitter {
* Disconnects everything, basically destorying the manager.
* Stops all players, leaves all voice channels then disconnects all LavalinkNodes
*/
public disconnect(): Promise<boolean[]> {
public disconnect(): Promise<Array<boolean>> {
const promises = [];
for (const id of [...this.players.keys()]) promises.push(this.leave(id));
for (const node of [...this.nodes.values()]) node.destroy();
Expand Down Expand Up @@ -201,7 +204,7 @@ export class Manager extends EventEmitter {
/**
* Gets all connected nodes, sorts them by cou load of the node
*/
public get idealNodes(): LavalinkNode[] {
public get idealNodes(): Array<LavalinkNode> {
return [...this.nodes.values()]
.filter(node => node.connected)
.sort((a, b) => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"module": "commonjs",
"moduleResolution": "node",
"noUnusedLocals": true,
"outDir": "./dist",
"outDir": "./",
"pretty": true,
"removeComments": false,
"resolveJsonModule": true,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.tsbuildinfo

Large diffs are not rendered by default.

Loading