From daa1cdfd6b57c755b54f179e28b2374a0affc990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FAY=E3=82=B7?= <103030954+FAYStarNext@users.noreply.github.com> Date: Sun, 21 Jul 2024 23:05:40 +0700 Subject: [PATCH] Fix: Duplicate Node --- .npmignore | 3 +- README.md | 46 +++++++++- example/.gitignore | 175 ++++++++++++++++++++++++++++++++++++++ example/README.md | 15 ++++ example/package.json | 16 ++++ example/src/index.ts | 46 ++++++++++ example/tsconfig.json | 27 ++++++ src/structures/Manager.ts | 7 +- src/structures/Node.ts | 7 +- 9 files changed, 329 insertions(+), 13 deletions(-) create mode 100644 example/.gitignore create mode 100644 example/README.md create mode 100644 example/package.json create mode 100644 example/src/index.ts create mode 100644 example/tsconfig.json diff --git a/.npmignore b/.npmignore index 3366001..158ee5f 100644 --- a/.npmignore +++ b/.npmignore @@ -2,4 +2,5 @@ .vscode/ src/ bun.lockb -test/ \ No newline at end of file +test/ +example/ \ No newline at end of file diff --git a/README.md b/README.md index 8032a58..e8a8e0e 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,51 @@ That's it! You have successfully installed Sunday.ts and are ready to start usin ## 🎈 Usage ```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 diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/example/.gitignore @@ -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 diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..7410a2d --- /dev/null +++ b/example/README.md @@ -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. diff --git a/example/package.json b/example/package.json new file mode 100644 index 0000000..27e9288 --- /dev/null +++ b/example/package.json @@ -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" + } +} \ No newline at end of file diff --git a/example/src/index.ts b/example/src/index.ts new file mode 100644 index 0000000..0d03db5 --- /dev/null +++ b/example/src/index.ts @@ -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); \ No newline at end of file diff --git a/example/tsconfig.json b/example/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/example/tsconfig.json @@ -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 + } +} diff --git a/src/structures/Manager.ts b/src/structures/Manager.ts index 3710d49..5b2a2aa 100644 --- a/src/structures/Manager.ts +++ b/src/structures/Manager.ts @@ -115,9 +115,7 @@ export class Manager extends TypedEmitter { */ constructor(options: ManagerOptions) { super(); - check(options); - Structure.get("Player").init(this); Structure.get("Node").init(this); TrackUtils.init(this); @@ -148,9 +146,8 @@ export class Manager extends TypedEmitter { } 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); }); } } diff --git a/src/structures/Node.ts b/src/structures/Node.ts index 8076385..1c331df 100644 --- a/src/structures/Node.ts +++ b/src/structures/Node.ts @@ -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));