Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Jul 27, 2020
1 parent 6991599 commit 9f3c3a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ class RPCClient extends EventEmitter {
headers: {
Authorization: `Bearer ${this.accessToken}`,
},
}).then((r) => r.json());
}).then((r) => {
if (!r.ok) {
throw new Error(r.status);
}
return r.json();
});

this.fetch.endpoint = 'https://discord.com/api';

Expand Down Expand Up @@ -212,7 +217,6 @@ class RPCClient extends EventEmitter {
scopes,
client_id: this.clientId,
rpc_token: rpcToken,
redirect_uri: redirectUri,
});

const response = await this.fetch('POST', '/oauth2/token', {
Expand Down
6 changes: 3 additions & 3 deletions src/transports/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ async function findEndpoint(tries = 0) {
const endpoint = `http://127.0.0.1:${6463 + (tries % 10)}`;
try {
const r = await fetch(endpoint);
if (r.status !== 401) {
return findEndpoint(tries + 1);
if (r.status === 404) {
return endpoint;
}
return endpoint;
return findEndpoint(tries + 1);
} catch (e) {
return findEndpoint(tries + 1);
}
Expand Down
7 changes: 5 additions & 2 deletions src/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ class WebSocketTransport extends EventEmitter {
this.tries = 0;
}

async connect(options, tries = this.tries) {
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}`,
{
origin: this.client.options.origin,
},
);
ws.onopen = this.onOpen.bind(this);
ws.onclose = ws.onerror = this.onClose.bind(this);
Expand Down Expand Up @@ -65,7 +68,7 @@ class WebSocketTransport extends EventEmitter {
}
if (!derr) {
// eslint-disable-next-line no-plusplus
setTimeout(() => this.connect(undefined, e.code === 1006 ? ++this.tries : 0), 250);
setTimeout(() => this.connect(e.code === 1006 ? ++this.tries : 0), 250);
}
}
}
Expand Down

0 comments on commit 9f3c3a5

Please sign in to comment.