From 0a26e621f3798f9f0214076ca243f8889972662c Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Mon, 27 Jul 2020 15:28:42 -0500 Subject: [PATCH] improve fetch errors --- .eslintrc.js | 1 + README.md | 2 +- example/main.js | 8 +++++--- src/client.js | 9 ++++++--- src/constants.js | 2 ++ test/rp.js | 45 ++++++--------------------------------------- 6 files changed, 21 insertions(+), 46 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index d69d1b4..c7c2cb7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,6 +1,7 @@ 'use strict'; module.exports = { + root: true, extends: 'airbnb-base', parser: 'babel-eslint', parserOptions: { diff --git a/README.md b/README.md index 02c1dd0..4e39e0b 100644 --- a/README.md +++ b/README.md @@ -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' }); diff --git a/example/main.js b/example/main.js index 5142774..85cecb5 100644 --- a/example/main.js +++ b/example/main.js @@ -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' }); @@ -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//rich-presence/assets rpc.setActivity({ details: `booped ${boops} times`, state: 'in slither party', diff --git a/src/client.js b/src/client.js index f4e2d5c..55063e3 100644 --- a/src/client.js +++ b/src/client.js @@ -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'; diff --git a/src/constants.js b/src/constants.js index 6298905..441f832 100644 --- a/src/constants.js +++ b/src/constants.js @@ -19,6 +19,7 @@ exports.RPCCommands = keyMirror([ 'GET_GUILDS', 'GET_CHANNEL', 'GET_CHANNELS', + 'CREATE_CHANNEL_INVITE', 'GET_RELATIONSHIPS', 'GET_USER', 'SUBSCRIBE', @@ -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', diff --git a/test/rp.js b/test/rp.js index 5e73dba..93fca29 100644 --- a/test/rp.js +++ b/test/rp.js @@ -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);