Repository: devsnek/discord-rich-presence Branch: master Commit: e7c9d0b76e46 Files: 9 Total size: 6.3 KB Directory structure: gitextract_drg0u9mi/ ├── .github/ │ └── FUNDING.yml ├── .gitignore ├── .npmignore ├── .npmrc ├── README.md ├── index.js ├── package.json ├── test.js └── webpack.config.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/FUNDING.yml ================================================ # These are supported funding model platforms github: [devsnek] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] patreon: # Replace with a single Patreon username open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel custom: # Replace with a single custom sponsorship URL ================================================ FILE: .gitignore ================================================ node_modules browser.js ================================================ FILE: .npmignore ================================================ node_modules test.js webpack.config.js README.md ================================================ FILE: .npmrc ================================================ package-lock=false ================================================ FILE: README.md ================================================
# Discord Rich Presence A simple wrapper around [discord-rpc](https://npmjs.org/discord-rpc) ### Example ```javascript const client = require('discord-rich-presence')('180984871685062656'); client.updatePresence({ state: 'slithering', details: '🐍', startTimestamp: Date.now(), endTimestamp: Date.now() + 1337, largeImageKey: 'snek_large', smallImageKey: 'snek_small', instance: true, }); ``` In browser you can import/require it as `discord-rich-presence/browser`. However, it should be noted that currently using rich presence in browser is a feature whitelisted by Discord, and you will most likely be unable to use it. ================================================ FILE: index.js ================================================ 'use strict'; const Discord = require('discord-rpc'); const EventEmitter = require('events'); const browser = typeof window !== 'undefined'; function makeClient(clientId) { const rpc = new Discord.Client({ transport: browser ? 'websocket' : 'ipc' }); let connected = false; let activityCache = null; const instance = new class RP extends EventEmitter { updatePresence(d) { if (connected) { rpc.setActivity(d).catch((e) => this.emit('error', e)); } else { activityCache = d; } } reply(user, response) { const handle = (e) => this.emit('error', e); switch (response) { case 'YES': rpc.sendJoinInvite(user).catch(handle); break; case 'NO': case 'IGNORE': rpc.closeJoinRequest(user).catch(handle); break; default: throw new RangeError('unknown response'); } } disconnect() { rpc.destroy().catch((e) => this.emit('error', e)); } }(); rpc.on('error', (e) => instance.emit('error', e)); rpc.login({ clientId }) .then(() => { instance.emit('connected'); connected = true; rpc.subscribe('ACTIVITY_JOIN', ({ secret }) => { instance.emit('join', secret); }); rpc.subscribe('ACTIVITY_SPECTATE', ({ secret }) => { instance.emit('spectate', secret); }); rpc.subscribe('ACTIVITY_JOIN_REQUEST', (user) => { instance.emit('joinRequest', user); }); if (activityCache) { rpc.setActivity(activityCache).catch((e) => instance.emit('error', e)); activityCache = null; } }) .catch((e) => instance.emit('error', e)); return instance; } module.exports = makeClient; ================================================ FILE: package.json ================================================ { "name": "discord-rich-presence", "version": "0.0.8", "description": "rich presence wrapper for discord-rpc", "main": "index.js", "unpkg": "browser.js", "jsdelivr": "browser.js", "scripts": { "build:browser": "webpack", "prepublishOnly": "env NODE_ENV=production npm run build:browser" }, "repository": { "type": "git", "url": "git+https://github.com/devsnek/discord-rich-presence.git" }, "keywords": [ "discord", "rich", "presence", "rpc" ], "author": "snek