Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Discord RPC doesn't get cleared sometimes #16

Open
Mixaz017 opened this issue Apr 16, 2021 · 2 comments
Open

Discord RPC doesn't get cleared sometimes #16

Mixaz017 opened this issue Apr 16, 2021 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@Mixaz017
Copy link
Collaborator

Opening this issue until a proper fix is found.

@domo5581
Copy link

I am currently working on adding more Discord RPC features to idkr, I will take a look into this and try to obtain an solution.

@Mixaz017 Mixaz017 added the bug Something isn't working label Sep 29, 2021
@NullDev NullDev added this to the Release 2.0 milestone Nov 2, 2021
@z3db0y
Copy link

z3db0y commented Nov 23, 2021

const RPCApi = require('discord-rpc');
class RPC {

__onerr () {
    this.__r = 0;
    clearInterval(this.__i);
    setTimeout(() => {this.__init();}, 100);
}

__doInterval () {
    if(this.__i) return;
    this.__i = setInterval(() => {
        if(!this.__r) return this.__init();
        if(!this.__a) { this.__client.clearActivity().catch(this.__onerr); }
        else { this.__client.setActivity(this.__a).catch(this.__onerr); }
    }, 1000);
}

__init () {
    if(this.__r || this.__ii) return;
    this.__ii = 1;
    this.__client = new RPCApi.Client({ transport: 'ipc' });
    this.__client.login({ clientId: this.__id }).then(() => {
        this.__r = 1;
        this.__ii = 0;
        this.__doInterval();
    }).catch((err) => { console.log(err); this.__ii = 0; setTimeout(() => {this.__init();}, 100); });
}

constructor(clientID) {
    RPCApi.register(clientID);
    this.__id = clientID;
    this.__init();
}

setActivity(activity) {
    this.__a = activity;
}

clearActivity() {
    this.__a = null;
}

destroy() {
    if(this.__r) {
        this.__client.clearActivity();
        clearInterval(this.__i);
        this.__client = null;
    }
}

}

module.exports = { Client: RPC };


you're welcome. automatically inits rpc, re-inits every time discord is restarted so it doesn't break AND internally updates activity so you dont have to do setInterval. To destroy just do rpc.destroy().
(gh formatting is weird so i put a separator between code and text)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants