Skip to content
Snippets Groups Projects
Commit 096fdb00 authored by Reviath's avatar Reviath
Browse files

Update javascript/main.js, golang/main.go, golang/client/client.go,...

Update javascript/main.js, golang/main.go, golang/client/client.go, golang/ipc/ipc_main.go, golang/ipc/ipc.go, golang/ipc/ipc_windows.go, golang/client/imputMapper.go, golang/client/types.go files
Deleted node_modules/bindings/LICENSE.md, node_modules/bindings/README.md, node_modules/bindings/bindings.js, node_modules/bindings/package.json, node_modules/discord-rpc/.github/workflows/docs.yml, node_modules/discord-rpc/.github/FUNDING.yml, node_modules/discord-rpc/src/transports/index.js, node_modules/discord-rpc/src/transports/ipc.js, node_modules/discord-rpc/src/transports/websocket.js, node_modules/discord-rpc/src/client.js, node_modules/discord-rpc/src/constants.js, node_modules/discord-rpc/src/index.js, node_modules/discord-rpc/src/util.js, node_modules/discord-rpc/.eslintrc.js, node_modules/discord-rpc/README.md, node_modules/discord-rpc/package.json, node_modules/file-uri-to-path/test/test.js, node_modules/file-uri-to-path/test/tests.json, node_modules/file-uri-to-path/.npmignore, node_modules/file-uri-to-path/.travis.yml, node_modules/file-uri-to-path/History.md, node_modules/file-uri-to-path/LICENSE, node_modules/file-uri-to-path/README.md, node_modules/file-uri-to-path/index.d.ts, node_modules/file-uri-to-path/index.js, node_modules/file-uri-to-path/package.json, node_modules/node-addon-api/doc/Doxyfile, node_modules/node-addon-api/doc/array_buffer.md, node_modules/node-addon-api/doc/async_context.md, node_modules/node-addon-api/doc/async_operations.md, node_modules/node-addon-api/doc/async_worker.md, node_modules/node-addon-api/doc/basic_types.md, node_modules/node-addon-api/doc/bigint.md, node_modules/node-addon-api/doc/boolean.md, node_modules/node-addon-api/doc/buffer.md, node_modules/node-addon-api/doc/callback_scope.md, node_modules/node-addon-api/doc/callbackinfo.md, node_modules/node-addon-api/doc/checker-tool.md, node_modules/node-addon-api/doc/class_property_descriptor.md, node_modules/node-addon-api/doc/cmake-js.md, node_modules/node-addon-api/doc/conversion-tool.md, node_modules/node-addon-api/doc/creating_a_release.md, node_modules/node-addon-api/doc/dataview.md, node_modules/node-addon-api/doc/env.md, node_modules/node-addon-api/doc/error.md, node_modules/node-addon-api/doc/error_handling.md, node_modules/node-addon-api/doc/escapable_handle_scope.md, node_modules/node-addon-api/doc/external.md, node_modules/node-addon-api/doc/function.md, node_modules/node-addon-api/doc/function_reference.md, node_modules/node-addon-api/doc/generator.md, node_modules/node-addon-api/doc/handle_scope.md, node_modules/node-addon-api/doc/memory_management.md, node_modules/node-addon-api/doc/node-gyp.md, node_modules/node-addon-api/doc/number.md, node_modules/node-addon-api/doc/object.md, node_modules/node-addon-api/doc/object_lifetime_management.md, node_modules/node-addon-api/doc/object_reference.md, node_modules/node-addon-api/doc/object_wrap.md, node_modules/node-addon-api/doc/prebuild_tools.md, node_modules/node-addon-api/doc/promises.md, node_modules/node-addon-api/doc/property_descriptor.md, node_modules/node-addon-api/doc/range_error.md, node_modules/node-addon-api/doc/reference.md, node_modules/node-addon-api/doc/setup.md, node_modules/node-addon-api/doc/string.md, node_modules/node-addon-api/doc/symbol.md, node_modules/node-addon-api/doc/threadsafe_function.md, node_modules/node-addon-api/doc/type_error.md, node_modules/node-addon-api/doc/typed_array.md, node_modules/node-addon-api/doc/typed_array_of.md, node_modules/node-addon-api/doc/value.md, node_modules/node-addon-api/doc/version_management.md, node_modules/node-addon-api/doc/working_with_javascript_values.md, node_modules/node-addon-api/external-napi/node_api.h, node_modules/node-addon-api/src/node_api.cc, node_modules/node-addon-api/src/node_api.gyp, node_modules/node-addon-api/src/node_api.h, node_modules/node-addon-api/src/node_api_types.h, node_modules/node-addon-api/src/node_internals.cc, node_modules/node-addon-api/src/node_internals.h, node_modules/node-addon-api/src/nothing.c, node_modules/node-addon-api/src/util-inl.h, node_modules/node-addon-api/src/util.h, node_modules/node-addon-api/tools/README.md, node_modules/node-addon-api/tools/check-napi.js, node_modules/node-addon-api/tools/conversion.js, node_modules/node-addon-api/.editorconfig, node_modules/node-addon-api/.travis.yml, node_modules/node-addon-api/CHANGELOG.md, node_modules/node-addon-api/CODE_OF_CONDUCT.md, node_modules/node-addon-api/CONTRIBUTING.md, node_modules/node-addon-api/LICENSE.md, node_modules/node-addon-api/README.md, node_modules/node-addon-api/appveyor.yml, node_modules/node-addon-api/index.js, node_modules/node-addon-api/napi-inl.deprecated.h, node_modules/node-addon-api/napi-inl.h, node_modules/node-addon-api/napi.h, node_modules/node-addon-api/package.json, node_modules/node-fetch/lib/index.es.js, node_modules/node-fetch/lib/index.js, node_modules/node-fetch/lib/index.mjs, node_modules/node-fetch/CHANGELOG.md, node_modules/node-fetch/LICENSE.md, node_modules/node-fetch/README.md, node_modules/node-fetch/browser.js, node_modules/node-fetch/package.json, node_modules/register-scheme/build/Release/.deps/Release/node-addon-api/src/nothing.a.d, node_modules/register-scheme/build/Release/.deps/Release/obj.target/node-addon-api/src/nothing.o.d, node_modules/register-scheme/build/Release/.deps/Release/obj.target/register-protocol-handler/src/addon.o.d, node_modules/register-scheme/build/Release/.deps/Release/obj.target/register-protocol-handler/src/register_linux.o.d, node_modules/register-scheme/build/Release/.deps/Release/obj.target/register-protocol-handler.node.d, node_modules/register-scheme/build/Release/.deps/Release/nothing.a.d, node_modules/register-scheme/build/Release/.deps/Release/register-protocol-handler.node.d, node_modules/register-scheme/build/Release/node-addon-api/src/nothing.a, node_modules/register-scheme/build/Release/obj.target/node-addon-api/src/nothing.o, node_modules/register-scheme/build/Release/obj.target/register-protocol-handler/src/addon.o, node_modules/register-scheme/build/Release/obj.target/register-protocol-handler/src/register_linux.o, node_modules/register-scheme/build/Release/obj.target/register-protocol-handler.node, node_modules/register-scheme/build/Release/nothing.a, node_modules/register-scheme/build/Release/register-protocol-handler.node, node_modules/register-scheme/build/Makefile, node_modules/register-scheme/build/binding.Makefile, node_modules/register-scheme/build/config.gypi, node_modules/register-scheme/build/register-protocol-handler.target.mk, node_modules/register-scheme/node-addon-api/src/node-api.target.mk, node_modules/register-scheme/node-addon-api/src/nothing.target.mk, node_modules/register-scheme/src/addon.cc, node_modules/register-scheme/src/register.h, node_modules/register-scheme/src/register_linux.cpp, node_modules/register-scheme/src/register_mac.m, node_modules/register-scheme/src/register_win.cpp, node_modules/register-scheme/README.md, node_modules/register-scheme/binding.gyp, node_modules/register-scheme/index.js, node_modules/register-scheme/package.json, node_modules/ws/lib/buffer-util.js, node_modules/ws/lib/constants.js, node_modules/ws/lib/event-target.js, node_modules/ws/lib/extension.js, node_modules/ws/lib/limiter.js, node_modules/ws/lib/permessage-deflate.js, node_modules/ws/lib/receiver.js, node_modules/ws/lib/sender.js, node_modules/ws/lib/stream.js, node_modules/ws/lib/validation.js, node_modules/ws/lib/websocket-server.js, node_modules/ws/lib/websocket.js, node_modules/ws/LICENSE, node_modules/ws/README.md, node_modules/ws/browser.js, node_modules/ws/index.js, node_modules/ws/package.json, node_modules/.package-lock.json, package-lock.json, package.json files
parent 0403d566
Branches
No related tags found
No related merge requests found
Showing
with 382 additions and 1522 deletions
package client
import (
"crypto/rand"
"encoding/json"
"fmt"
"os"
"../ipc"
)
var logged bool
func Login(clientid string) error {
if !logged {
payload, err := json.Marshal(Handshake{"1", clientid})
if err != nil {
return err
}
err = ipc.OpenSocket()
if err != nil {
return err
}
ipc.Send(0, string(payload))
}
logged = true
return nil
}
func Logout() {
logged = false
err := ipc.CloseSocket()
if err != nil {
panic(err)
}
}
func SetActivity(activity Activity) error {
if !logged {
return nil
}
payload, err := json.Marshal(Frame{
"SET_ACTIVITY",
Args{
os.Getpid(),
mapActivity(&activity),
},
getNonce(),
})
if err != nil {
return err
}
ipc.Send(1, string(payload))
return nil
}
func getNonce() string {
buf := make([]byte, 16)
_, err := rand.Read(buf)
if err != nil {
fmt.Println(err)
}
buf[6] = (buf[6] & 0x0f) | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", buf[0:4], buf[4:6], buf[6:8], buf[8:10], buf[10:])
}
package client
import (
"time"
)
type Activity struct {
Details string
State string
LargeImage string
LargeText string
SmallImage string
SmallText string
Party *Party
Timestamps *Timestamps
Secrets *Secrets
Buttons []*Button
}
type Button struct {
Label string
Url string
}
type Party struct {
ID string
Players int
MaxPlayers int
}
type Timestamps struct {
Start *time.Time
End *time.Time
}
type Secrets struct {
Match string
Join string
Spectate string
}
func mapActivity(activity *Activity) *PayloadActivity {
if activity.LargeImage == "" {
activity.LargeImage = "none"
}
if activity.LargeText == "" {
activity.LargeText = "none"
}
if activity.SmallImage == "" {
activity.SmallImage = "none"
}
if activity.SmallText == "" {
activity.SmallText = "none"
}
final := &PayloadActivity{
Details: activity.Details,
State: activity.State,
Assets: PayloadAssets{
LargeImage: activity.LargeImage,
LargeText: activity.LargeText,
SmallImage: activity.SmallImage,
SmallText: activity.SmallText,
},
}
if activity.Timestamps != nil && activity.Timestamps.Start != nil {
start := uint64(activity.Timestamps.Start.UnixNano() / 1e6)
final.Timestamps = &PayloadTimestamps{
Start: &start,
}
if activity.Timestamps.End != nil {
end := uint64(activity.Timestamps.End.UnixNano() / 1e6)
final.Timestamps.End = &end
}
}
if activity.Party != nil {
final.Party = &PayloadParty{
ID: activity.Party.ID,
Size: [2]int{activity.Party.Players, activity.Party.MaxPlayers},
}
}
if activity.Secrets != nil {
final.Secrets = &PayloadSecrets{
Join: activity.Secrets.Join,
Match: activity.Secrets.Match,
Spectate: activity.Secrets.Spectate,
}
}
if len(activity.Buttons) > 0 {
for _, btn := range activity.Buttons {
final.Buttons = append(final.Buttons, &PayloadButton{
Label: btn.Label,
Url: btn.Url,
})
}
}
return final
}
package client
type Handshake struct {
V string `json:"v"`
ClientId string `json:"client_id"`
}
type Frame struct {
Cmd string `json:"cmd"`
Args Args `json:"args"`
Nonce string `json:"nonce"`
}
type Args struct {
Pid int `json:"pid"`
Activity *PayloadActivity `json:"activity"`
}
type PayloadActivity struct {
Details string `json:"details"`
State string `json:"state"`
Assets PayloadAssets `json:"assets"`
Party *PayloadParty `json:"party,omitempty"`
Timestamps *PayloadTimestamps `json:"timestamps,omitempty"`
Secrets *PayloadSecrets `json:"secrets,omitempty"`
Buttons []*PayloadButton `json:"buttons,omitempty"`
}
type PayloadAssets struct {
LargeImage string `json:"large_image"`
LargeText string `json:"large_text"`
SmallImage string `json:"small_image"`
SmallText string `json:"small_text"`
}
type PayloadParty struct {
ID string `json:"id"`
Size [2]int `json:"size"`
}
type PayloadTimestamps struct {
Start *uint64 `json:"start,omitempty"`
End *uint64 `json:"end,omitempty"`
}
type PayloadSecrets struct {
Match string `json:"match,omitempty"`
Join string `json:"join,omitempty"`
Spectate string `json:"spectate,omitempty"`
}
type PayloadButton struct {
Label string `json:"label,omitempty"`
Url string `json:"url,omitempty"`
}
package ipc
import (
"net"
"time"
)
func OpenSocket() error {
sock, err := net.DialTimeout("unix", GetIpcPath()+"/discord-ipc-0", time.Second*2)
if err != nil {
return err
}
socket = sock
return nil
}
package ipc
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"os"
)
var socket net.Conn
func GetIpcPath() string {
variablesnames := []string{"XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP"}
for _, variablename := range variablesnames {
path, exists := os.LookupEnv(variablename)
if exists {
return path
}
}
return "/tmp"
}
func CloseSocket() error {
if socket != nil {
socket.Close()
socket = nil
}
return nil
}
func Read() string {
buf := make([]byte, 512)
payloadlength, err := socket.Read(buf)
if err != nil {
}
buffer := new(bytes.Buffer)
for i := 8; i < payloadlength; i++ {
buffer.WriteByte(buf[i])
}
return buffer.String()
}
func Send(opcode int, payload string) string {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, int32(opcode))
if err != nil {
fmt.Println(err)
}
err = binary.Write(buf, binary.LittleEndian, int32(len(payload)))
if err != nil {
fmt.Println(err)
}
buf.Write([]byte(payload))
_, err = socket.Write(buf.Bytes())
if err != nil {
fmt.Println(err)
}
return Read()
}
package ipc
import (
npipe "gopkg.in/natefinch/npipe.v2"
"time"
)
func OpenSocket() error {
sock, err := npipe.DialTimeout(`\\.\pipe\discord-ipc-0`, time.Second*2)
if err != nil {
return err
}
socket = sock
return nil
}
package main
import (
"time"
"./client"
"os"
"os/signal"
"syscall"
)
func main() {
err := client.Login("INSERT_CLIENT_ID")
if err != nil {
panic(err)
}
now := time.Now()
err = client.SetActivity(client.Activity{
State: "git.randomchars.net/Reviath/remilia-scarlet",
Details: "Last Project: Remilia Scarlet",
LargeImage: "revi",
LargeText: "Written with golang",
SmallImage: "release",
SmallText: "Leviathan Dev Team",
Party: &client.Party{
ID: "-1",
Players: 1,
MaxPlayers: 1,
},
Timestamps: &client.Timestamps{
Start: &now,
},
Buttons: []*client.Button{
&client.Button{
Label: "GitLab",
Url: "https://git.randomchars.net/Reviath",
},
},
})
if err != nil {
panic(err)
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}
File moved
{
"name": "discord-rpc",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"node_modules/bindings": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
"peer": true,
"dependencies": {
"file-uri-to-path": "1.0.0"
}
},
"node_modules/discord-rpc": {
"version": "3.2.0",
"resolved": "git+ssh://git@github.com/discordjs/rpc.git#50376b913d089e8e28220fc67416c3e9bc5bca53",
"license": "MIT",
"dependencies": {
"node-fetch": "^2.6.1",
"ws": "^7.3.1"
},
"peerDependencies": {
"register-scheme": "github:devsnek/node-register-scheme"
}
},
"node_modules/file-uri-to-path": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
"peer": true
},
"node_modules/node-addon-api": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-1.7.2.tgz",
"integrity": "sha512-ibPK3iA+vaY1eEjESkQkM0BbCqFOaZMiXRTtdB0u7b4djtY6JnsjvPdUHVMg6xQt3B8fpTTWHI9A+ADjM9frzg==",
"peer": true
},
"node_modules/node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==",
"engines": {
"node": "4.x || >=6.0.0"
}
},
"node_modules/register-scheme": {
"version": "0.0.2",
"resolved": "git+ssh://git@github.com/devsnek/node-register-scheme.git#e7cc9a63a1f512565da44cb57316d9fb10750e17",
"hasInstallScript": true,
"license": "MIT",
"peer": true,
"dependencies": {
"bindings": "^1.3.0",
"node-addon-api": "^1.3.0"
}
},
"node_modules/ws": {
"version": "7.4.3",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.3.tgz",
"integrity": "sha512-hr6vCR76GsossIRsr8OLR9acVVm1jyfEWvhbNjtgPOrfvAlKzvyeg/P6r8RuDjRyrcQoPQT7K0DGEPc7Ae6jzA==",
"engines": {
"node": ">=8.3.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": "^5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
}
}
(The MIT License)
Copyright (c) 2012 Nathan Rajlich &lt;nathan@tootallnate.net&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
node-bindings
=============
### Helper module for loading your native module's `.node` file
This is a helper module for authors of Node.js native addon modules.
It is basically the "swiss army knife" of `require()`ing your native module's
`.node` file.
Throughout the course of Node's native addon history, addons have ended up being
compiled in a variety of different places, depending on which build tool and which
version of node was used. To make matters worse, now the `gyp` build tool can
produce either a __Release__ or __Debug__ build, each being built into different
locations.
This module checks _all_ the possible locations that a native addon would be built
at, and returns the first one that loads successfully.
Installation
------------
Install with `npm`:
``` bash
$ npm install --save bindings
```
Or add it to the `"dependencies"` section of your `package.json` file.
Example
-------
`require()`ing the proper bindings file for the current node version, platform
and architecture is as simple as:
``` js
var bindings = require('bindings')('binding.node')
// Use your bindings defined in your C files
bindings.your_c_function()
```
Nice Error Output
-----------------
When the `.node` file could not be loaded, `node-bindings` throws an Error with
a nice error message telling you exactly what was tried. You can also check the
`err.tries` Array property.
```
Error: Could not load the bindings file. Tried:
→ /Users/nrajlich/ref/build/binding.node
→ /Users/nrajlich/ref/build/Debug/binding.node
→ /Users/nrajlich/ref/build/Release/binding.node
→ /Users/nrajlich/ref/out/Debug/binding.node
→ /Users/nrajlich/ref/Debug/binding.node
→ /Users/nrajlich/ref/out/Release/binding.node
→ /Users/nrajlich/ref/Release/binding.node
→ /Users/nrajlich/ref/build/default/binding.node
→ /Users/nrajlich/ref/compiled/0.8.2/darwin/x64/binding.node
at bindings (/Users/nrajlich/ref/node_modules/bindings/bindings.js:84:13)
at Object.<anonymous> (/Users/nrajlich/ref/lib/ref.js:5:47)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
...
```
The searching for the `.node` file will originate from the first directory in which has a `package.json` file is found.
License
-------
(The MIT License)
Copyright (c) 2012 Nathan Rajlich &lt;nathan@tootallnate.net&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* Module dependencies.
*/
var fs = require('fs'),
path = require('path'),
fileURLToPath = require('file-uri-to-path'),
join = path.join,
dirname = path.dirname,
exists =
(fs.accessSync &&
function(path) {
try {
fs.accessSync(path);
} catch (e) {
return false;
}
return true;
}) ||
fs.existsSync ||
path.existsSync,
defaults = {
arrow: process.env.NODE_BINDINGS_ARROW || '',
compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled',
platform: process.platform,
arch: process.arch,
nodePreGyp:
'node-v' +
process.versions.modules +
'-' +
process.platform +
'-' +
process.arch,
version: process.versions.node,
bindings: 'bindings.node',
try: [
// node-gyp's linked version in the "build" dir
['module_root', 'build', 'bindings'],
// node-waf and gyp_addon (a.k.a node-gyp)
['module_root', 'build', 'Debug', 'bindings'],
['module_root', 'build', 'Release', 'bindings'],
// Debug files, for development (legacy behavior, remove for node v0.9)
['module_root', 'out', 'Debug', 'bindings'],
['module_root', 'Debug', 'bindings'],
// Release files, but manually compiled (legacy behavior, remove for node v0.9)
['module_root', 'out', 'Release', 'bindings'],
['module_root', 'Release', 'bindings'],
// Legacy from node-waf, node <= 0.4.x
['module_root', 'build', 'default', 'bindings'],
// Production "Release" buildtype binary (meh...)
['module_root', 'compiled', 'version', 'platform', 'arch', 'bindings'],
// node-qbs builds
['module_root', 'addon-build', 'release', 'install-root', 'bindings'],
['module_root', 'addon-build', 'debug', 'install-root', 'bindings'],
['module_root', 'addon-build', 'default', 'install-root', 'bindings'],
// node-pre-gyp path ./lib/binding/{node_abi}-{platform}-{arch}
['module_root', 'lib', 'binding', 'nodePreGyp', 'bindings']
]
};
/**
* The main `bindings()` function loads the compiled bindings for a given module.
* It uses V8's Error API to determine the parent filename that this function is
* being invoked from, which is then used to find the root directory.
*/
function bindings(opts) {
// Argument surgery
if (typeof opts == 'string') {
opts = { bindings: opts };
} else if (!opts) {
opts = {};
}
// maps `defaults` onto `opts` object
Object.keys(defaults).map(function(i) {
if (!(i in opts)) opts[i] = defaults[i];
});
// Get the module root
if (!opts.module_root) {
opts.module_root = exports.getRoot(exports.getFileName());
}
// Ensure the given bindings name ends with .node
if (path.extname(opts.bindings) != '.node') {
opts.bindings += '.node';
}
// https://github.com/webpack/webpack/issues/4175#issuecomment-342931035
var requireFunc =
typeof __webpack_require__ === 'function'
? __non_webpack_require__
: require;
var tries = [],
i = 0,
l = opts.try.length,
n,
b,
err;
for (; i < l; i++) {
n = join.apply(
null,
opts.try[i].map(function(p) {
return opts[p] || p;
})
);
tries.push(n);
try {
b = opts.path ? requireFunc.resolve(n) : requireFunc(n);
if (!opts.path) {
b.path = n;
}
return b;
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND' &&
e.code !== 'QUALIFIED_PATH_RESOLUTION_FAILED' &&
!/not find/i.test(e.message)) {
throw e;
}
}
}
err = new Error(
'Could not locate the bindings file. Tried:\n' +
tries
.map(function(a) {
return opts.arrow + a;
})
.join('\n')
);
err.tries = tries;
throw err;
}
module.exports = exports = bindings;
/**
* Gets the filename of the JavaScript file that invokes this function.
* Used to help find the root directory of a module.
* Optionally accepts an filename argument to skip when searching for the invoking filename
*/
exports.getFileName = function getFileName(calling_file) {
var origPST = Error.prepareStackTrace,
origSTL = Error.stackTraceLimit,
dummy = {},
fileName;
Error.stackTraceLimit = 10;
Error.prepareStackTrace = function(e, st) {
for (var i = 0, l = st.length; i < l; i++) {
fileName = st[i].getFileName();
if (fileName !== __filename) {
if (calling_file) {
if (fileName !== calling_file) {
return;
}
} else {
return;
}
}
}
};
// run the 'prepareStackTrace' function above
Error.captureStackTrace(dummy);
dummy.stack;
// cleanup
Error.prepareStackTrace = origPST;
Error.stackTraceLimit = origSTL;
// handle filename that starts with "file://"
var fileSchema = 'file://';
if (fileName.indexOf(fileSchema) === 0) {
fileName = fileURLToPath(fileName);
}
return fileName;
};
/**
* Gets the root directory of a module, given an arbitrary filename
* somewhere in the module tree. The "root directory" is the directory
* containing the `package.json` file.
*
* In: /home/nate/node-native-module/lib/index.js
* Out: /home/nate/node-native-module
*/
exports.getRoot = function getRoot(file) {
var dir = dirname(file),
prev;
while (true) {
if (dir === '.') {
// Avoids an infinite loop in rare cases, like the REPL
dir = process.cwd();
}
if (
exists(join(dir, 'package.json')) ||
exists(join(dir, 'node_modules'))
) {
// Found the 'package.json' file or 'node_modules' dir; we're done
return dir;
}
if (prev === dir) {
// Got to the top
throw new Error(
'Could not find module root given file: "' +
file +
'". Do you have a `package.json` file? '
);
}
// Try the parent dir next
prev = dir;
dir = join(dir, '..');
}
};
{
"name": "bindings",
"description": "Helper module for loading your native module's .node file",
"keywords": [
"native",
"addon",
"bindings",
"gyp",
"waf",
"c",
"c++"
],
"version": "1.5.0",
"author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)",
"repository": {
"type": "git",
"url": "git://github.com/TooTallNate/node-bindings.git"
},
"main": "./bindings.js",
"bugs": {
"url": "https://github.com/TooTallNate/node-bindings/issues"
},
"homepage": "https://github.com/TooTallNate/node-bindings",
"license": "MIT",
"dependencies": {
"file-uri-to-path": "1.0.0"
}
}
'use strict';
module.exports = {
root: true,
extends: 'airbnb-base',
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'script',
},
env: {
es6: true,
node: true,
},
overrides: [
{
files: ['*.jsx'],
parserOptions: {
sourceType: 'module',
ecmaFeatures: { jsx: true },
},
},
{
files: ['*.mjs'],
parserOptions: { sourceType: 'module' },
env: {
node: true,
},
rules: {
'no-restricted-globals': ['error', 'require'],
},
},
{
files: ['*.web.js'],
env: { browser: true },
},
],
rules: {
'strict': ['error', 'global'],
'indent': ['error', 2, {
SwitchCase: 1,
FunctionDeclaration: {
parameters: 'first',
},
FunctionExpression: {
parameters: 'first',
},
CallExpression: {
arguments: 'first',
},
}],
'no-bitwise': 'off',
'no-iterator': 'off',
'global-require': 'off',
'quote-props': ['error', 'consistent-as-needed'],
'brace-style': ['error', '1tbs', { allowSingleLine: false }],
'curly': ['error', 'all'],
'no-param-reassign': 'off',
'arrow-parens': ['error', 'always'],
'no-multi-assign': 'off',
'no-underscore-dangle': 'off',
'no-restricted-syntax': 'off',
'object-curly-newline': 'off',
'prefer-const': ['error', { destructuring: 'all' }],
'class-methods-use-this': 'off',
'implicit-arrow-linebreak': 'off',
'lines-between-class-members': 'off',
'import/no-dynamic-require': 'off',
'import/no-extraneous-dependencies': ['error', {
devDependencies: true,
}],
'import/extensions': 'off',
'import/prefer-default-export': 'off',
'import/no-unresolved': 'off',
},
globals: {
WebAssembly: false,
BigInt: false,
BigInt64Array: false,
BigUint64Array: false,
URL: false,
Atomics: false,
SharedArrayBuffer: false,
globalThis: false,
FinalizationGroup: false,
WeakRef: false,
queueMicrotask: false,
},
};
# 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
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with a single custom sponsorship URL
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '15'
- run: "npm install"
- run: "npm run docs"
- uses: JamesIves/github-pages-deploy-action@3.7.1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: docs
FOLDER: docs-out
<div align="center">
<br />
<p>
<a href="https://discord.gg/bRCvFy9"><img src="https://discordapp.com/api/guilds/222078108977594368/embed.png" alt="Discord server" /></a>
<a href="https://www.npmjs.com/package/discord-rpc"><img src="https://img.shields.io/npm/v/discord-rpc.svg?maxAge=3600" alt="NPM version" /></a>
<a href="https://www.npmjs.com/package/discord-rpc"><img src="https://img.shields.io/npm/dt/discord-rpc.svg?maxAge=3600" alt="NPM downloads" /></a>
<a href="https://david-dm.org/discordjs/RPC"><img src="https://img.shields.io/david/discordjs/RPC.svg?maxAge=3600" alt="Dependencies" /></a>
</p>
<p>
<a href="https://nodei.co/npm/discord-rpc/"><img src="https://nodei.co/npm/discord-rpc.png?downloads=true&stars=true" alt="NPM info" /></a>
</p>
</div>
# Discord.js RPC Extension
### [Documentation](https://discord.js.org/#/docs/rpc/)
### [Rich Presence Example](https://github.com/discordjs/RPC/blob/master/example)
### __Browser__ Example
```javascript
const clientId = '287406016902594560';
const scopes = ['rpc', 'rpc.api', 'messages.read'];
const client = new RPC.Client({ transport: 'websocket' });
client.on('ready', () => {
console.log('Logged in as', client.application.name);
console.log('Authed for user', client.user.username);
client.selectVoiceChannel('81384788862181376');
});
// Log in to RPC with client id
client.login({ clientId, scopes });
```
{
"name": "discord-rpc",
"version": "3.2.0",
"description": "A simple RPC client for Discord",
"keywords": [
"discord",
"rpc",
"rich presence",
"remote procedural call"
],
"main": "src/index.js",
"jsdelivr": "browser.js",
"unpkg": "browser.js",
"author": "snek <me@gus.host>",
"license": "MIT",
"homepage": "https://github.com/discordjs/RPC#readme",
"bugs": {
"url": "https://github.com/discordjs/RPC/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/discordjs/RPC.git"
},
"scripts": {
"lint": "eslint src test --ext=js",
"docs": "mkdir -p docs-out && docgen --source src --output docs-out/master.json --jsdoc jsdoc.json --custom docgen.json",
"example": "electron example/main.js",
"build:browser": "webpack-cli",
"prepublishOnly": "npm run lint && npm run build:browser"
},
"dependencies": {
"node-fetch": "^2.6.1",
"ws": "^7.3.1"
},
"peerDependencies": {
"register-scheme": "github:devsnek/node-register-scheme"
},
"devDependencies": {
"babel-eslint": "^10.0.3",
"discord.js-docgen": "github:discordjs/docgen",
"electron": "^7.1.9",
"eslint": "^6.1.0",
"eslint-config-airbnb-base": "14.0.0",
"eslint-plugin-import": "^2.18.2",
"jsdoc-strip-async-await": "^0.1.0",
"webpack": "^4.40.0",
"webpack-cli": "^3.3.8"
},
"browser": {
"net": false,
"ws": false,
"uws": false,
"erlpack": false,
"electron": false,
"register-scheme": false,
"./src/transports/IPC.js": false
}
}
'use strict';
const EventEmitter = require('events');
const { setTimeout, clearTimeout } = require('timers');
const fetch = require('node-fetch');
const transports = require('./transports');
const { RPCCommands, RPCEvents, RelationshipTypes } = require('./constants');
const { pid: getPid, uuid } = require('./util');
function subKey(event, args) {
return `${event}${JSON.stringify(args)}`;
}
/**
* @typedef {RPCClientOptions}
* @extends {ClientOptions}
* @prop {string} transport RPC transport. one of `ipc` or `websocket`
*/
/**
* The main hub for interacting with Discord RPC
* @extends {BaseClient}
*/
class RPCClient extends EventEmitter {
/**
* @param {RPCClientOptions} [options] Options for the client.
* You must provide a transport
*/
constructor(options = {}) {
super();
this.options = options;
this.accessToken = null;
this.clientId = null;
/**
* Application used in this client
* @type {?ClientApplication}
*/
this.application = null;
/**
* User used in this application
* @type {?User}
*/
this.user = null;
const Transport = transports[options.transport];
if (!Transport) {
throw new TypeError('RPC_INVALID_TRANSPORT', options.transport);
}
this.fetch = (method, path, { data, query } = {}) =>
fetch(`${this.fetch.endpoint}${path}${query ? new URLSearchParams(query) : ''}`, {
method,
body: data,
headers: {
Authorization: `Bearer ${this.accessToken}`,
},
}).then(async (r) => {
const body = await r.json();
if (!r.ok) {
const e = new Error(r.status);
e.body = body;
throw e;
}
return body;
});
this.fetch.endpoint = 'https://discord.com/api';
/**
* Raw transport userd
* @type {RPCTransport}
* @private
*/
this.transport = new Transport(this);
this.transport.on('message', this._onRpcMessage.bind(this));
/**
* Map of nonces being expected from the transport
* @type {Map}
* @private
*/
this._expecting = new Map();
/**
* Map of current subscriptions
* @type {Map}
* @private
*/
this._subscriptions = new Map();
this._connectPromise = undefined;
}
/**
* Search and connect to RPC
*/
connect(clientId) {
if (this._connectPromise) {
return this._connectPromise;
}
this._connectPromise = new Promise((resolve, reject) => {
this.clientId = clientId;
const timeout = setTimeout(() => reject(new Error('RPC_CONNECTION_TIMEOUT')), 10e3);
timeout.unref();
this.once('connected', () => {
clearTimeout(timeout);
resolve(this);
});
this.transport.once('close', () => {
this._expecting.forEach((e) => {
e.reject(new Error('connection closed'));
});
this.emit('disconnected');
reject(new Error('connection closed'));
});
this.transport.connect().catch(reject);
});
return this._connectPromise;
}
/**
* @typedef {RPCLoginOptions}
* @param {string} clientId Client ID
* @param {string} [clientSecret] Client secret
* @param {string} [accessToken] Access token
* @param {string} [rpcToken] RPC token
* @param {string} [tokenEndpoint] Token endpoint
* @param {string[]} [scopes] Scopes to authorize with
*/
/**
* Performs authentication flow. Automatically calls Client#connect if needed.
* @param {RPCLoginOptions} options Options for authentication.
* At least one property must be provided to perform login.
* @example client.login({ clientId: '1234567', clientSecret: 'abcdef123' });
* @returns {Promise<RPCClient>}
*/
async login(options = {}) {
let { clientId, accessToken } = options;
await this.connect(clientId);
if (!options.scopes) {
this.emit('ready');
return this;
}
if (!accessToken) {
accessToken = await this.authorize(options);
}
return this.authenticate(accessToken);
}
/**
* Request
* @param {string} cmd Command
* @param {Object} [args={}] Arguments
* @param {string} [evt] Event
* @returns {Promise}
* @private
*/
request(cmd, args, evt) {
return new Promise((resolve, reject) => {
const nonce = uuid();
this.transport.send({ cmd, args, evt, nonce });
this._expecting.set(nonce, { resolve, reject });
});
}
/**
* Message handler
* @param {Object} message message
* @private
*/
_onRpcMessage(message) {
if (message.cmd === RPCCommands.DISPATCH && message.evt === RPCEvents.READY) {
if (message.data.user) {
this.user = message.data.user;
}
this.emit('connected');
} else if (this._expecting.has(message.nonce)) {
const { resolve, reject } = this._expecting.get(message.nonce);
if (message.evt === 'ERROR') {
const e = new Error(message.data.message);
e.code = message.data.code;
e.data = message.data;
reject(e);
} else {
resolve(message.data);
}
this._expecting.delete(message.nonce);
} else {
const subid = subKey(message.evt, message.args);
if (!this._subscriptions.has(subid)) {
return;
}
this._subscriptions.get(subid)(message.data);
}
}
/**
* Authorize
* @param {Object} options options
* @returns {Promise}
* @private
*/
async authorize({ scopes, clientSecret, rpcToken, redirectUri } = {}) {
if (clientSecret && rpcToken === true) {
const body = await this.fetch('POST', '/oauth2/token/rpc', {
data: new URLSearchParams({
client_id: this.clientId,
client_secret: clientSecret,
}),
});
rpcToken = body.rpc_token;
}
const { code } = await this.request('AUTHORIZE', {
scopes,
client_id: this.clientId,
rpc_token: rpcToken,
});
const response = await this.fetch('POST', '/oauth2/token', {
data: new URLSearchParams({
client_id: this.clientId,
client_secret: clientSecret,
code,
grant_type: 'authorization_code',
redirect_uri: redirectUri,
}),
});
return response.access_token;
}
/**
* Authenticate
* @param {string} accessToken access token
* @returns {Promise}
* @private
*/
authenticate(accessToken) {
return this.request('AUTHENTICATE', { access_token: accessToken })
.then(({ application, user }) => {
this.accessToken = accessToken;
this.application = application;
this.user = user;
this.emit('ready');
return this;
});
}
/**
* Fetch a guild
* @param {Snowflake} id Guild ID
* @param {number} [timeout] Timeout request
* @returns {Promise<Guild>}
*/
getGuild(id, timeout) {
return this.request(RPCCommands.GET_GUILD, { guild_id: id, timeout });
}
/**
* Fetch all guilds
* @param {number} [timeout] Timeout request
* @returns {Promise<Collection<Snowflake, Guild>>}
*/
getGuilds(timeout) {
return this.request(RPCCommands.GET_GUILDS, { timeout });
}
/**
* Get a channel
* @param {Snowflake} id Channel ID
* @param {number} [timeout] Timeout request
* @returns {Promise<Channel>}
*/
getChannel(id, timeout) {
return this.request(RPCCommands.GET_CHANNEL, { channel_id: id, timeout });
}
/**
* Get all channels
* @param {Snowflake} [id] Guild ID
* @param {number} [timeout] Timeout request
* @returns {Promise<Collection<Snowflake, Channel>>}
*/
async getChannels(id, timeout) {
const { channels } = await this.request(RPCCommands.GET_CHANNELS, {
timeout,
guild_id: id,
});
return channels;
}
/**
* @typedef {CertifiedDevice}
* @prop {string} type One of `AUDIO_INPUT`, `AUDIO_OUTPUT`, `VIDEO_INPUT`
* @prop {string} uuid This device's Windows UUID
* @prop {object} vendor Vendor information
* @prop {string} vendor.name Vendor's name
* @prop {string} vendor.url Vendor's url
* @prop {object} model Model information
* @prop {string} model.name Model's name
* @prop {string} model.url Model's url
* @prop {string[]} related Array of related product's Windows UUIDs
* @prop {boolean} echoCancellation If the device has echo cancellation
* @prop {boolean} noiseSuppression If the device has noise suppression
* @prop {boolean} automaticGainControl If the device has automatic gain control
* @prop {boolean} hardwareMute If the device has a hardware mute
*/
/**
* Tell discord which devices are certified
* @param {CertifiedDevice[]} devices Certified devices to send to discord
* @returns {Promise}
*/
setCertifiedDevices(devices) {
return this.request(RPCCommands.SET_CERTIFIED_DEVICES, {
devices: devices.map((d) => ({
type: d.type,
id: d.uuid,
vendor: d.vendor,
model: d.model,
related: d.related,
echo_cancellation: d.echoCancellation,
noise_suppression: d.noiseSuppression,
automatic_gain_control: d.automaticGainControl,
hardware_mute: d.hardwareMute,
})),
});
}
/**
* @typedef {UserVoiceSettings}
* @prop {Snowflake} id ID of the user these settings apply to
* @prop {?Object} [pan] Pan settings, an object with `left` and `right` set between
* 0.0 and 1.0, inclusive
* @prop {?number} [volume=100] The volume
* @prop {bool} [mute] If the user is muted
*/
/**
* Set the voice settings for a user, by id
* @param {Snowflake} id ID of the user to set
* @param {UserVoiceSettings} settings Settings
* @returns {Promise}
*/
setUserVoiceSettings(id, settings) {
return this.request(RPCCommands.SET_USER_VOICE_SETTINGS, {
user_id: id,
pan: settings.pan,
mute: settings.mute,
volume: settings.volume,
});
}
/**
* Move the user to a voice channel
* @param {Snowflake} id ID of the voice channel
* @param {Object} [options] Options
* @param {number} [options.timeout] Timeout for the command
* @param {boolean} [options.force] Force this move. This should only be done if you
* have explicit permission from the user.
* @returns {Promise}
*/
selectVoiceChannel(id, { timeout, force = false } = {}) {
return this.request(RPCCommands.SELECT_VOICE_CHANNEL, { channel_id: id, timeout, force });
}
/**
* Move the user to a text channel
* @param {Snowflake} id ID of the voice channel
* @param {Object} [options] Options
* @param {number} [options.timeout] Timeout for the command
* have explicit permission from the user.
* @returns {Promise}
*/
selectTextChannel(id, { timeout } = {}) {
return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout });
}
/**
* Get current voice settings
* @returns {Promise}
*/
getVoiceSettings() {
return this.request(RPCCommands.GET_VOICE_SETTINGS)
.then((s) => ({
automaticGainControl: s.automatic_gain_control,
echoCancellation: s.echo_cancellation,
noiseSuppression: s.noise_suppression,
qos: s.qos,
silenceWarning: s.silence_warning,
deaf: s.deaf,
mute: s.mute,
input: {
availableDevices: s.input.available_devices,
device: s.input.device_id,
volume: s.input.volume,
},
output: {
availableDevices: s.output.available_devices,
device: s.output.device_id,
volume: s.output.volume,
},
mode: {
type: s.mode.type,
autoThreshold: s.mode.auto_threshold,
threshold: s.mode.threshold,
shortcut: s.mode.shortcut,
delay: s.mode.delay,
},
}));
}
/**
* Set current voice settings, overriding the current settings until this session disconnects.
* This also locks the settings for any other rpc sessions which may be connected.
* @param {Object} args Settings
* @returns {Promise}
*/
setVoiceSettings(args) {
return this.request(RPCCommands.SET_VOICE_SETTINGS, {
automatic_gain_control: args.automaticGainControl,
echo_cancellation: args.echoCancellation,
noise_suppression: args.noiseSuppression,
qos: args.qos,
silence_warning: args.silenceWarning,
deaf: args.deaf,
mute: args.mute,
input: args.input ? {
device_id: args.input.device,
volume: args.input.volume,
} : undefined,
output: args.output ? {
device_id: args.output.device,
volume: args.output.volume,
} : undefined,
mode: args.mode ? {
mode: args.mode.type,
auto_threshold: args.mode.autoThreshold,
threshold: args.mode.threshold,
shortcut: args.mode.shortcut,
delay: args.mode.delay,
} : undefined,
});
}
/**
* Capture a shortcut using the client
* The callback takes (key, stop) where `stop` is a function that will stop capturing.
* This `stop` function must be called before disconnecting or else the user will have
* to restart their client.
* @param {Function} callback Callback handling keys
* @returns {Promise<Function>}
*/
captureShortcut(callback) {
const subid = subKey(RPCEvents.CAPTURE_SHORTCUT_CHANGE);
const stop = () => {
this._subscriptions.delete(subid);
return this.request(RPCCommands.CAPTURE_SHORTCUT, { action: 'STOP' });
};
this._subscriptions.set(subid, ({ shortcut }) => {
callback(shortcut, stop);
});
return this.request(RPCCommands.CAPTURE_SHORTCUT, { action: 'START' })
.then(() => stop);
}
/**
* Sets the presence for the logged in user.
* @param {object} args The rich presence to pass.
* @param {number} [pid] The application's process ID. Defaults to the executing process' PID.
* @returns {Promise}
*/
setActivity(args = {}, pid = getPid()) {
let timestamps;
let assets;
let party;
let secrets;
if (args.startTimestamp || args.endTimestamp) {
timestamps = {
start: args.startTimestamp,
end: args.endTimestamp,
};
if (timestamps.start instanceof Date) {
timestamps.start = Math.round(timestamps.start.getTime());
}
if (timestamps.end instanceof Date) {
timestamps.end = Math.round(timestamps.end.getTime());
}
if (timestamps.start > 2147483647000) {
throw new RangeError('timestamps.start must fit into a unix timestamp');
}
if (timestamps.end > 2147483647000) {
throw new RangeError('timestamps.end must fit into a unix timestamp');
}
}
if (
args.largeImageKey || args.largeImageText
|| args.smallImageKey || args.smallImageText
) {
assets = {
large_image: args.largeImageKey,
large_text: args.largeImageText,
small_image: args.smallImageKey,
small_text: args.smallImageText,
};
}
if (args.partySize || args.partyId || args.partyMax) {
party = { id: args.partyId };
if (args.partySize || args.partyMax) {
party.size = [args.partySize, args.partyMax];
}
}
if (args.matchSecret || args.joinSecret || args.spectateSecret) {
secrets = {
match: args.matchSecret,
join: args.joinSecret,
spectate: args.spectateSecret,
};
}
return this.request(RPCCommands.SET_ACTIVITY, {
pid,
activity: {
state: args.state,
details: args.details,
timestamps,
assets,
party,
secrets,
buttons: args.buttons,
instance: !!args.instance,
},
});
}
/**
* Clears the currently set presence, if any. This will hide the "Playing X" message
* displayed below the user's name.
* @param {number} [pid] The application's process ID. Defaults to the executing process' PID.
* @returns {Promise}
*/
clearActivity(pid = getPid()) {
return this.request(RPCCommands.SET_ACTIVITY, {
pid,
});
}
/**
* Invite a user to join the game the RPC user is currently playing
* @param {User} user The user to invite
* @returns {Promise}
*/
sendJoinInvite(user) {
return this.request(RPCCommands.SEND_ACTIVITY_JOIN_INVITE, {
user_id: user.id || user,
});
}
/**
* Request to join the game the user is playing
* @param {User} user The user whose game you want to request to join
* @returns {Promise}
*/
sendJoinRequest(user) {
return this.request(RPCCommands.SEND_ACTIVITY_JOIN_REQUEST, {
user_id: user.id || user,
});
}
/**
* Reject a join request from a user
* @param {User} user The user whose request you wish to reject
* @returns {Promise}
*/
closeJoinRequest(user) {
return this.request(RPCCommands.CLOSE_ACTIVITY_JOIN_REQUEST, {
user_id: user.id || user,
});
}
createLobby(type, capacity, metadata) {
return this.request(RPCCommands.CREATE_LOBBY, {
type,
capacity,
metadata,
});
}
updateLobby(lobby, { type, owner, capacity, metadata } = {}) {
return this.request(RPCCommands.UPDATE_LOBBY, {
id: lobby.id || lobby,
type,
owner_id: (owner && owner.id) || owner,
capacity,
metadata,
});
}
deleteLobby(lobby) {
return this.request(RPCCommands.DELETE_LOBBY, {
id: lobby.id || lobby,
});
}
connectToLobby(id, secret) {
return this.request(RPCCommands.CONNECT_TO_LOBBY, {
id,
secret,
});
}
sendToLobby(lobby, data) {
return this.request(RPCCommands.SEND_TO_LOBBY, {
id: lobby.id || lobby,
data,
});
}
disconnectFromLobby(lobby) {
return this.request(RPCCommands.DISCONNECT_FROM_LOBBY, {
id: lobby.id || lobby,
});
}
updateLobbyMember(lobby, user, metadata) {
return this.request(RPCCommands.UPDATE_LOBBY_MEMBER, {
lobby_id: lobby.id || lobby,
user_id: user.id || user,
metadata,
});
}
getRelationships() {
const types = Object.keys(RelationshipTypes);
return this.request(RPCCommands.GET_RELATIONSHIPS)
.then((o) => o.relationships.map((r) => ({
...r,
type: types[r.type],
})));
}
/**
* Subscribe to an event
* @param {string} event Name of event e.g. `MESSAGE_CREATE`
* @param {Object} [args] Args for event e.g. `{ channel_id: '1234' }`
* @param {Function} callback Callback when an event for the subscription is triggered
* @returns {Promise<Object>}
*/
subscribe(event, args, callback) {
if (!callback && typeof args === 'function') {
callback = args;
args = undefined;
}
return this.request(RPCCommands.SUBSCRIBE, args, event).then(() => {
const subid = subKey(event, args);
this._subscriptions.set(subid, callback);
return {
unsubscribe: () => this.request(RPCCommands.UNSUBSCRIBE, args, event)
.then(() => this._subscriptions.delete(subid)),
};
});
}
/**
* Destroy the client
*/
async destroy() {
await this.transport.close();
}
}
module.exports = RPCClient;
'use strict';
function keyMirror(arr) {
const tmp = {};
for (const value of arr) {
tmp[value] = value;
}
return tmp;
}
exports.browser = typeof window !== 'undefined';
exports.RPCCommands = keyMirror([
'DISPATCH',
'AUTHORIZE',
'AUTHENTICATE',
'GET_GUILD',
'GET_GUILDS',
'GET_CHANNEL',
'GET_CHANNELS',
'CREATE_CHANNEL_INVITE',
'GET_RELATIONSHIPS',
'GET_USER',
'SUBSCRIBE',
'UNSUBSCRIBE',
'SET_USER_VOICE_SETTINGS',
'SET_USER_VOICE_SETTINGS_2',
'SELECT_VOICE_CHANNEL',
'GET_SELECTED_VOICE_CHANNEL',
'SELECT_TEXT_CHANNEL',
'GET_VOICE_SETTINGS',
'SET_VOICE_SETTINGS_2',
'SET_VOICE_SETTINGS',
'CAPTURE_SHORTCUT',
'SET_ACTIVITY',
'SEND_ACTIVITY_JOIN_INVITE',
'CLOSE_ACTIVITY_JOIN_REQUEST',
'ACTIVITY_INVITE_USER',
'ACCEPT_ACTIVITY_INVITE',
'INVITE_BROWSER',
'DEEP_LINK',
'CONNECTIONS_CALLBACK',
'BRAINTREE_POPUP_BRIDGE_CALLBACK',
'GIFT_CODE_BROWSER',
'GUILD_TEMPLATE_BROWSER',
'OVERLAY',
'BROWSER_HANDOFF',
'SET_CERTIFIED_DEVICES',
'GET_IMAGE',
'CREATE_LOBBY',
'UPDATE_LOBBY',
'DELETE_LOBBY',
'UPDATE_LOBBY_MEMBER',
'CONNECT_TO_LOBBY',
'DISCONNECT_FROM_LOBBY',
'SEND_TO_LOBBY',
'SEARCH_LOBBIES',
'CONNECT_TO_LOBBY_VOICE',
'DISCONNECT_FROM_LOBBY_VOICE',
'SET_OVERLAY_LOCKED',
'OPEN_OVERLAY_ACTIVITY_INVITE',
'OPEN_OVERLAY_GUILD_INVITE',
'OPEN_OVERLAY_VOICE_SETTINGS',
'VALIDATE_APPLICATION',
'GET_ENTITLEMENT_TICKET',
'GET_APPLICATION_TICKET',
'START_PURCHASE',
'GET_SKUS',
'GET_ENTITLEMENTS',
'GET_NETWORKING_CONFIG',
'NETWORKING_SYSTEM_METRICS',
'NETWORKING_PEER_METRICS',
'NETWORKING_CREATE_TOKEN',
'SET_USER_ACHIEVEMENT',
'GET_USER_ACHIEVEMENTS',
]);
exports.RPCEvents = keyMirror([
'CURRENT_USER_UPDATE',
'GUILD_STATUS',
'GUILD_CREATE',
'CHANNEL_CREATE',
'RELATIONSHIP_UPDATE',
'VOICE_CHANNEL_SELECT',
'VOICE_STATE_CREATE',
'VOICE_STATE_DELETE',
'VOICE_STATE_UPDATE',
'VOICE_SETTINGS_UPDATE',
'VOICE_SETTINGS_UPDATE_2',
'VOICE_CONNECTION_STATUS',
'SPEAKING_START',
'SPEAKING_STOP',
'GAME_JOIN',
'GAME_SPECTATE',
'ACTIVITY_JOIN',
'ACTIVITY_JOIN_REQUEST',
'ACTIVITY_SPECTATE',
'ACTIVITY_INVITE',
'NOTIFICATION_CREATE',
'MESSAGE_CREATE',
'MESSAGE_UPDATE',
'MESSAGE_DELETE',
'LOBBY_DELETE',
'LOBBY_UPDATE',
'LOBBY_MEMBER_CONNECT',
'LOBBY_MEMBER_DISCONNECT',
'LOBBY_MEMBER_UPDATE',
'LOBBY_MESSAGE',
'CAPTURE_SHORTCUT_CHANGE',
'OVERLAY',
'OVERLAY_UPDATE',
'ENTITLEMENT_CREATE',
'ENTITLEMENT_DELETE',
'USER_ACHIEVEMENT_UPDATE',
'READY',
'ERROR',
]);
exports.RPCErrors = {
CAPTURE_SHORTCUT_ALREADY_LISTENING: 5004,
GET_GUILD_TIMED_OUT: 5002,
INVALID_ACTIVITY_JOIN_REQUEST: 4012,
INVALID_ACTIVITY_SECRET: 5005,
INVALID_CHANNEL: 4005,
INVALID_CLIENTID: 4007,
INVALID_COMMAND: 4002,
INVALID_ENTITLEMENT: 4015,
INVALID_EVENT: 4004,
INVALID_GIFT_CODE: 4016,
INVALID_GUILD: 4003,
INVALID_INVITE: 4011,
INVALID_LOBBY: 4013,
INVALID_LOBBY_SECRET: 4014,
INVALID_ORIGIN: 4008,
INVALID_PAYLOAD: 4000,
INVALID_PERMISSIONS: 4006,
INVALID_TOKEN: 4009,
INVALID_USER: 4010,
LOBBY_FULL: 5007,
NO_ELIGIBLE_ACTIVITY: 5006,
OAUTH2_ERROR: 5000,
PURCHASE_CANCELED: 5008,
PURCHASE_ERROR: 5009,
RATE_LIMITED: 5011,
SELECT_CHANNEL_TIMED_OUT: 5001,
SELECT_VOICE_FORCE_REQUIRED: 5003,
SERVICE_UNAVAILABLE: 1001,
TRANSACTION_ABORTED: 1002,
UNAUTHORIZED_FOR_ACHIEVEMENT: 5010,
UNKNOWN_ERROR: 1000,
};
exports.RPCCloseCodes = {
CLOSE_NORMAL: 1000,
CLOSE_UNSUPPORTED: 1003,
CLOSE_ABNORMAL: 1006,
INVALID_CLIENTID: 4000,
INVALID_ORIGIN: 4001,
RATELIMITED: 4002,
TOKEN_REVOKED: 4003,
INVALID_VERSION: 4004,
INVALID_ENCODING: 4005,
};
exports.LobbyTypes = {
PRIVATE: 1,
PUBLIC: 2,
};
exports.RelationshipTypes = {
NONE: 0,
FRIEND: 1,
BLOCKED: 2,
PENDING_INCOMING: 3,
PENDING_OUTGOING: 4,
IMPLICIT: 5,
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment