Skip to content

Commit 0a26e62

Browse files
committed
improve fetch errors
1 parent 9f3c3a5 commit 0a26e62

File tree

6 files changed

+21
-46
lines changed

6 files changed

+21
-46
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
module.exports = {
4+
root: true,
45
extends: 'airbnb-base',
56
parser: 'babel-eslint',
67
parserOptions: {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
### __Browser__ Example
2121

2222
```javascript
23-
const clientId = '187406016902594560';
23+
const clientId = '287406016902594560';
2424
const scopes = ['rpc', 'rpc.api', 'messages.read'];
2525

2626
const client = new RPC.Client({ transport: 'websocket' });

example/main.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ app.on('activate', () => {
4343
}
4444
});
4545

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

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

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

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

62+
// You'll need to have snek_large and snek_small assets uploaded to
63+
// https://discord.com/developers/applications/<application_id>/rich-presence/assets
6264
rpc.setActivity({
6365
details: `booped ${boops} times`,
6466
state: 'in slither party',

src/client.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ class RPCClient extends EventEmitter {
5858
headers: {
5959
Authorization: `Bearer ${this.accessToken}`,
6060
},
61-
}).then((r) => {
61+
}).then(async (r) => {
62+
const body = await r.json();
6263
if (!r.ok) {
63-
throw new Error(r.status);
64+
const e = new Error(r.status);
65+
e.body = body;
66+
throw e;
6467
}
65-
return r.json();
68+
return body;
6669
});
6770

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

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ exports.RPCCommands = keyMirror([
1919
'GET_GUILDS',
2020
'GET_CHANNEL',
2121
'GET_CHANNELS',
22+
'CREATE_CHANNEL_INVITE',
2223
'GET_RELATIONSHIPS',
2324
'GET_USER',
2425
'SUBSCRIBE',
@@ -42,6 +43,7 @@ exports.RPCCommands = keyMirror([
4243
'CONNECTIONS_CALLBACK',
4344
'BRAINTREE_POPUP_BRIDGE_CALLBACK',
4445
'GIFT_CODE_BROWSER',
46+
'GUILD_TEMPLATE_BROWSER',
4547
'OVERLAY',
4648
'BROWSER_HANDOFF',
4749
'SET_CERTIFIED_DEVICES',

test/rp.js

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,13 @@ try {
88

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

11-
const { clientId } = require('./auth');
12-
13-
const client = new Client({ transport: 'ipc' });
11+
const client = new Client({
12+
transport: 'ipc',
13+
});
1414

1515
client.on('ready', () => {
16-
console.log(client);
17-
18-
client.subscribe('ACTIVITY_JOIN', ({ secret }) => {
19-
console.log('should join game with secret:', secret);
20-
});
21-
22-
client.subscribe('ACTIVITY_SPECTATE', ({ secret }) => {
23-
console.log('should spectate game with secret:', secret);
24-
});
25-
26-
client.subscribe('ACTIVITY_JOIN_REQUEST', (user) => {
27-
console.log('user wants to join:', user);
28-
});
29-
30-
client.setActivity({
31-
state: 'slithering',
32-
details: '🐍',
33-
startTimestamp: new Date(),
34-
largeImageKey: 'snek_large',
35-
smallImageKey: 'snek_small',
36-
partyId: 'snek_party',
37-
partySize: 1,
38-
partyMax: 1,
39-
matchSecret: 'slithers',
40-
joinSecret: 'boop',
41-
spectateSecret: 'sniff',
42-
instance: true,
43-
}).then(console.log);
44-
45-
client.getRelationships().then((relations) => {
46-
relations
47-
.filter((r) => r.type === 'IMPLICIT')
48-
.map((r) => `${r.user.username}#${r.user.discriminator}`)
49-
.forEach((c) => console.log(c));
50-
});
16+
client.subscribe('MESSAGE_CREATE', { channel_id: '381886868708655104' }, console.log)
17+
.catch(console.error);
5118
});
5219

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

0 commit comments

Comments
 (0)