diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..67d8bc5 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,20 @@ +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: '14' + - run: "npm install" + - run: "npm run docs" + - uses: JamesIves/github-pages-deploy-action@3.5.9 + with: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BRANCH: docs + FOLDER: docs-out diff --git a/.gitignore b/.gitignore index 7dfc6f3..959bead 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ node_modules/** package-lock.json -docs.json browser.js test/auth.js .vscode +docs-out diff --git a/package.json b/package.json index 7911c8f..e35c3f1 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ }, "scripts": { "lint": "eslint src test --ext=js", - "docs": "docgen --source src --output docs.json --jsdoc jsdoc.json --custom docgen.json", + "docs": "mkdir -p docs-out && docgen --source src --output docs-out/master.json --jsdoc jsdoc.json --custom docgen.json", "example": "electron example/main.js", "build:browser": "webpack-cli", "prepublishOnly": "npm run lint && npm run build:browser" diff --git a/src/client.js b/src/client.js index e59f4d5..b4233c4 100644 --- a/src/client.js +++ b/src/client.js @@ -422,12 +422,10 @@ class RPCClient extends EventEmitter { * @param {Snowflake} id ID of the voice channel * @param {Object} [options] Options * @param {number} [options.timeout] Timeout for the command - * @param {boolean} [options.force=false] Force this move. This should only be done if you - * have explicit permission from the user. * @returns {Promise>} */ - selectTextChannel(id, { timeout, force = false } = {}) { - return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout, force }); + selectTextChannel(id, { timeout } = {}) { + return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout }); } /** @@ -847,7 +845,7 @@ class RPCClient extends EventEmitter { * @returns {Promise} */ async destroy() { - this.transport.close(); + await this.transport.close(); } } diff --git a/src/transports/ipc.js b/src/transports/ipc.js index 8d9dce1..f050a01 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -127,9 +127,13 @@ class IPCTransport extends EventEmitter { return; } if (data.cmd === 'AUTHORIZE' && data.evt !== 'ERROR') { - findEndpoint().then((endpoint) => { - this.client.request.endpoint = endpoint; - }); + findEndpoint() + .then((endpoint) => { + this.client.request.endpoint = endpoint; + }) + .catch((e) => { + this.client.emit('error', e); + }); } this.emit('message', data); break; @@ -151,9 +155,12 @@ class IPCTransport extends EventEmitter { this.socket.write(encode(op, data)); } - close() { - this.send({}, OPCodes.CLOSE); - this.socket.end(); + async close() { + return new Promise((r) => { + this.once('close', r); + this.send({}, OPCodes.CLOSE); + this.socket.end(); + }); } ping() { diff --git a/src/transports/websocket.js b/src/transports/websocket.js index 5e4a3f8..66e083a 100644 --- a/src/transports/websocket.js +++ b/src/transports/websocket.js @@ -17,59 +17,62 @@ class WebSocketTransport extends EventEmitter { this.tries = 0; } - async connect(tries = this.tries) { - if (this.connected) { - return; - } - const port = 6463 + (tries % 10); - this.hostAndPort = `127.0.0.1:${port}`; - const ws = this.ws = new WebSocket( - `ws://${this.hostAndPort}/?v=1&client_id=${this.client.clientId}`, + async connect() { + const port = 6463 + (this.tries % 10); + this.tries += 1; + + this.ws = new WebSocket( + `ws://127.0.0.1:${port}/?v=1&client_id=${this.client.clientId}`, { origin: this.client.options.origin, }, ); - ws.onopen = this.onOpen.bind(this); - ws.onclose = ws.onerror = this.onClose.bind(this); - ws.onmessage = this.onMessage.bind(this); + this.ws.onopen = this.onOpen.bind(this); + this.ws.onclose = this.onClose.bind(this); + this.ws.onerror = this.onError.bind(this); + this.ws.onmessage = this.onMessage.bind(this); } - send(data) { - if (!this.ws) { - return; - } - this.ws.send(pack(data)); + onOpen() { + this.emit('open'); } - close() { - if (!this.ws) { + onClose(event) { + if (!event.wasClean) { return; } - this.ws.close(); + this.emit('close', event); } - ping() {} // eslint-disable-line no-empty-function + onError(event) { + try { + this.ws.close(); + } catch {} // eslint-disable-line no-empty + + if (this.tries > 20) { + this.emit('error', event.error); + } else { + setTimeout(() => { + this.connect(); + }, 250); + } + } onMessage(event) { this.emit('message', unpack(event.data)); } - onOpen() { - this.emit('open'); + send(data) { + this.ws.send(pack(data)); } - onClose(e) { - try { + ping() {} // eslint-disable-line no-empty-function + + close() { + return new Promise((r) => { + this.once('close', r); this.ws.close(); - } catch (err) {} // eslint-disable-line no-empty - const derr = e.code >= 4000 && e.code < 5000; - if (!e.code || derr) { - this.emit('close', e); - } - if (!derr) { - // eslint-disable-next-line no-plusplus - setTimeout(() => this.connect(e.code === 1006 ? ++this.tries : 0), 250); - } + }); } } diff --git a/test/rp.js b/test/rp.js index 93fca29..a9a06bd 100644 --- a/test/rp.js +++ b/test/rp.js @@ -12,9 +12,13 @@ const client = new Client({ transport: 'ipc', }); -client.on('ready', () => { - client.subscribe('MESSAGE_CREATE', { channel_id: '381886868708655104' }, console.log) - .catch(console.error); +client.on('ready', async () => { + await client.selectTextChannel('201803114049699849'); + console.log(await client.getChannel('201803114049699849')); + client.destroy() + .then(() => { + console.log('closed!'); + }); }); client.login(require('./auth')).catch(console.error);