Skip to content

Commit

Permalink
improve fetch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Jul 27, 2020
1 parent 9f3c3a5 commit 0a26e62
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 46 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

module.exports = {
root: true,
extends: 'airbnb-base',
parser: 'babel-eslint',
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
### __Browser__ Example

```javascript
const clientId = '187406016902594560';
const clientId = '287406016902594560';
const scopes = ['rpc', 'rpc.api', 'messages.read'];

const client = new RPC.Client({ transport: 'websocket' });
Expand Down
8 changes: 5 additions & 3 deletions example/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ app.on('activate', () => {
}
});

// don't change the client id if you want this example to work
const clientId = '180984871685062656';
// Set this to your Client ID.
const clientId = '280984871685062656';

// only needed for discord allowing spectate, join, ask to join
// Only needed if you want to use spectate, join, or ask to join
DiscordRPC.register(clientId);

const rpc = new DiscordRPC.Client({ transport: 'ipc' });
Expand All @@ -59,6 +59,8 @@ async function setActivity() {

const boops = await mainWindow.webContents.executeJavaScript('window.boops');

// You'll need to have snek_large and snek_small assets uploaded to
// https://discord.com/developers/applications/<application_id>/rich-presence/assets
rpc.setActivity({
details: `booped ${boops} times`,
state: 'in slither party',
Expand Down
9 changes: 6 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ class RPCClient extends EventEmitter {
headers: {
Authorization: `Bearer ${this.accessToken}`,
},
}).then((r) => {
}).then(async (r) => {
const body = await r.json();
if (!r.ok) {
throw new Error(r.status);
const e = new Error(r.status);
e.body = body;
throw e;
}
return r.json();
return body;
});

this.fetch.endpoint = 'https://discord.com/api';
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports.RPCCommands = keyMirror([
'GET_GUILDS',
'GET_CHANNEL',
'GET_CHANNELS',
'CREATE_CHANNEL_INVITE',
'GET_RELATIONSHIPS',
'GET_USER',
'SUBSCRIBE',
Expand All @@ -42,6 +43,7 @@ exports.RPCCommands = keyMirror([
'CONNECTIONS_CALLBACK',
'BRAINTREE_POPUP_BRIDGE_CALLBACK',
'GIFT_CODE_BROWSER',
'GUILD_TEMPLATE_BROWSER',
'OVERLAY',
'BROWSER_HANDOFF',
'SET_CERTIFIED_DEVICES',
Expand Down
45 changes: 6 additions & 39 deletions test/rp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,13 @@ try {

const { Client } = require('../');

const { clientId } = require('./auth');

const client = new Client({ transport: 'ipc' });
const client = new Client({
transport: 'ipc',
});

client.on('ready', () => {
console.log(client);

client.subscribe('ACTIVITY_JOIN', ({ secret }) => {
console.log('should join game with secret:', secret);
});

client.subscribe('ACTIVITY_SPECTATE', ({ secret }) => {
console.log('should spectate game with secret:', secret);
});

client.subscribe('ACTIVITY_JOIN_REQUEST', (user) => {
console.log('user wants to join:', user);
});

client.setActivity({
state: 'slithering',
details: '🐍',
startTimestamp: new Date(),
largeImageKey: 'snek_large',
smallImageKey: 'snek_small',
partyId: 'snek_party',
partySize: 1,
partyMax: 1,
matchSecret: 'slithers',
joinSecret: 'boop',
spectateSecret: 'sniff',
instance: true,
}).then(console.log);

client.getRelationships().then((relations) => {
relations
.filter((r) => r.type === 'IMPLICIT')
.map((r) => `${r.user.username}#${r.user.discriminator}`)
.forEach((c) => console.log(c));
});
client.subscribe('MESSAGE_CREATE', { channel_id: '381886868708655104' }, console.log)
.catch(console.error);
});

client.login({ clientId }).catch(console.error);
client.login(require('./auth')).catch(console.error);

0 comments on commit 0a26e62

Please sign in to comment.