├── .github └── FUNDING.yml ├── .gitignore ├── .npmignore ├── .npmrc ├── README.md ├── index.js ├── package.json ├── test.js └── webpack.config.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [devsnek] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | custom: # Replace with a single custom sponsorship URL 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | browser.js 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | test.js 3 | webpack.config.js 4 | README.md 5 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
13 | 14 | # Discord Rich Presence 15 | 16 | A simple wrapper around [discord-rpc](https://npmjs.org/discord-rpc) 17 | 18 | ### Example 19 | 20 | ```javascript 21 | const client = require('discord-rich-presence')('180984871685062656'); 22 | 23 | client.updatePresence({ 24 | state: 'slithering', 25 | details: '🐍', 26 | startTimestamp: Date.now(), 27 | endTimestamp: Date.now() + 1337, 28 | largeImageKey: 'snek_large', 29 | smallImageKey: 'snek_small', 30 | instance: true, 31 | }); 32 | ``` 33 | 34 | In browser you can import/require it as `discord-rich-presence/browser`. 35 | However, it should be noted that currently using rich presence in browser is 36 | a feature whitelisted by Discord, and you will most likely be unable to use it. 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Discord = require('discord-rpc'); 4 | const EventEmitter = require('events'); 5 | 6 | const browser = typeof window !== 'undefined'; 7 | 8 | function makeClient(clientId) { 9 | const rpc = new Discord.Client({ transport: browser ? 'websocket' : 'ipc' }); 10 | 11 | let connected = false; 12 | let activityCache = null; 13 | 14 | const instance = new class RP extends EventEmitter { 15 | updatePresence(d) { 16 | if (connected) { 17 | rpc.setActivity(d).catch((e) => this.emit('error', e)); 18 | } else { 19 | activityCache = d; 20 | } 21 | } 22 | 23 | reply(user, response) { 24 | const handle = (e) => this.emit('error', e); 25 | switch (response) { 26 | case 'YES': 27 | rpc.sendJoinInvite(user).catch(handle); 28 | break; 29 | case 'NO': 30 | case 'IGNORE': 31 | rpc.closeJoinRequest(user).catch(handle); 32 | break; 33 | default: 34 | throw new RangeError('unknown response'); 35 | } 36 | } 37 | 38 | disconnect() { 39 | rpc.destroy().catch((e) => this.emit('error', e)); 40 | } 41 | }(); 42 | 43 | rpc.on('error', (e) => instance.emit('error', e)); 44 | 45 | rpc.login({ clientId }) 46 | .then(() => { 47 | instance.emit('connected'); 48 | connected = true; 49 | 50 | rpc.subscribe('ACTIVITY_JOIN', ({ secret }) => { 51 | instance.emit('join', secret); 52 | }); 53 | rpc.subscribe('ACTIVITY_SPECTATE', ({ secret }) => { 54 | instance.emit('spectate', secret); 55 | }); 56 | rpc.subscribe('ACTIVITY_JOIN_REQUEST', (user) => { 57 | instance.emit('joinRequest', user); 58 | }); 59 | 60 | if (activityCache) { 61 | rpc.setActivity(activityCache).catch((e) => instance.emit('error', e)); 62 | activityCache = null; 63 | } 64 | }) 65 | .catch((e) => instance.emit('error', e)); 66 | 67 | return instance; 68 | } 69 | 70 | module.exports = makeClient; 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "discord-rich-presence", 3 | "version": "0.0.8", 4 | "description": "rich presence wrapper for discord-rpc", 5 | "main": "index.js", 6 | "unpkg": "browser.js", 7 | "jsdelivr": "browser.js", 8 | "scripts": { 9 | "build:browser": "webpack", 10 | "prepublishOnly": "env NODE_ENV=production npm run build:browser" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/devsnek/discord-rich-presence.git" 15 | }, 16 | "keywords": [ 17 | "discord", 18 | "rich", 19 | "presence", 20 | "rpc" 21 | ], 22 | "author": "snek