idkr-client / idkr

:video_game: idk, just a Krunker client
GNU Affero General Public License v3.0
56 stars 37 forks source link

Discord RPC doesn't get cleared sometimes #16

Open Mixaz017 opened 3 years ago

Mixaz017 commented 3 years ago

Opening this issue until a proper fix is found.

amuseee commented 3 years ago

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.

z3db0y commented 2 years ago

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)