Skip to content

Commit

Permalink
Fix: Duplicate Node
Browse files Browse the repository at this point in the history
  • Loading branch information
EvarinDev committed Jul 21, 2024
1 parent 6fcb0b1 commit daa1cdf
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.vscode/
src/
bun.lockb
test/
test/
example/
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,51 @@ That's it! You have successfully installed Sunday.ts and are ready to start usin
## 🎈 Usage <a name="usage"></a>

```ts

import { Client } from "discord.js";
import { Manager } from "sunday.ts";

let client = new Client({
intents: [
"Guilds",
"GuildMembers",
"MessageContent",
"GuildMessages",
"GuildVoiceStates"
],
});
let manager = new Manager({
nodes: [
{
host: 'localhost',
port: 2333,
password: 'youshallnotpass',
version: "v4",
},
],
clientId: "1234567890",
send(guild_id, payload) {
const guild = client.guilds.cache.get(guild_id);
if (guild) guild.shard.send(payload);
},
});

manager.on("NodeConnect", (node) => {
console.log(`Node ${node.options.host} connected`);
});
manager.on("NodeRaw", async (node) => {
console.log(`sent raw data: ${JSON.stringify(node)}`);
});
client.on("ready", () => {
manager.init();
});
manager.on("PlayerCreate", (player) => {
console.log(`Player created in guild ${player.guild}`);
});
manager.on("NodeError" , (node, error) => {
console.log(`Node ${node.options.host} has an error: ${error.message}`);
});
client.on("raw", (data) => manager.updateVoiceState(data));
client.login("");
```

## ⛏️ Built Using <a name = "built_using"></a>
Expand Down
175 changes: 175 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
15 changes: 15 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# sunday.ts-

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run src/index.ts
```

This project was created using `bun init` in bun v1.1.18. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
16 changes: 16 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "sunday.ts-example",
"module": "src/index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"discord.js": "^14.15.3",
"dotenv": "^16.4.5",
"sunday.ts": "^1.0.3-indev"
}
}
46 changes: 46 additions & 0 deletions example/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Client } from "discord.js";
import { Manager } from "../../src";
import "dotenv/config";

let client = new Client({
intents: [
"Guilds",
"GuildMembers",
"MessageContent",
"GuildMessages",
"GuildVoiceStates"
],
});
let manager = new Manager({
nodes: [
{
host: 'localhost',
port: 2333,
password: 'youshallnotpass',
version: "v4",
},
],
clientId: "1234567890",
send(guild_id, payload) {
const guild = client.guilds.cache.get(guild_id);
if (guild) guild.shard.send(payload);
},
});

manager.on("NodeConnect", (node) => {
console.log(`Node ${node.options.host} connected`);
});
manager.on("NodeRaw", async (node) => {
console.log(`sent raw data: ${JSON.stringify(node)}`);
});
client.on("ready", () => {
manager.init();
});
manager.on("PlayerCreate", (player) => {
console.log(`Player created in guild ${player.guild}`);
});
manager.on("NodeError" , (node, error) => {
console.log(`Node ${node.options.host} has an error: ${error.message}`);
});
client.on("raw", (data) => manager.updateVoiceState(data));
client.login(process.env.TOKEN);
27 changes: 27 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
7 changes: 2 additions & 5 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ export class Manager extends TypedEmitter<ManagerEventEmitter> {
*/
constructor(options: ManagerOptions) {
super();

check(options);

Structure.get("Player").init(this);
Structure.get("Node").init(this);
TrackUtils.init(this);
Expand Down Expand Up @@ -148,9 +146,8 @@ export class Manager extends TypedEmitter<ManagerEventEmitter> {
}

if (this.options.nodes) {
this.options.nodes.forEach((nodeOptions, index) => {
const node = new (Structure.get("Node"))(nodeOptions);
this.nodes.set(index.toString(), node);
this.options.nodes.forEach((nodeOptions) => {
return new (Structure.get("Node"))(nodeOptions);
});
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/structures/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,13 @@ export class Node {
/** Connects to the Node. */
public connect(): void {
if (this.connected) return;

const headers = {
Authorization: this.options.password,
"Num-Shards": String(this.manager.options.shards),
"User-Id": this.manager.options.clientId,
"Client-Name": this.manager.options.clientName,
};
if (this.options.version === "v4") {
this.socket = new WebSocket(`ws${this.options.secure ? "s" : ""}://${this.address}/v4/websocket`, { headers });
} else {
this.socket = new WebSocket(`ws${this.options.secure ? "s" : ""}://${this.address}/websocket`, { headers });
}
this.socket = new WebSocket(`ws${this.options.secure ? "s" : ""}://${this.address}${this.options.version === "v4" ? "/v4/websocket" : "/"}`, { headers });
this.socket.on("open", this.open.bind(this));
this.socket.on("close", this.close.bind(this));
this.socket.on("message", this.message.bind(this));
Expand Down

0 comments on commit daa1cdf

Please sign in to comment.