This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (54 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { Plugin } = require('powercord/entities');
const { getModule, FluxDispatcher } = require('powercord/webpack');
const { getVoiceChannelId } = getModule([ 'getVoiceChannelId' ], false);
const { selectVoiceChannel } = getModule([ 'selectVoiceChannel' ], false);
const Settings = require('./components/settings.jsx');
module.exports = class VCReconnect extends Plugin {
constructor () {
super();
this.pingCheckEnabled = true;
}
/* Entry Point */
startPlugin () {
this.checkPing = this.checkPing.bind(this);
FluxDispatcher.subscribe('RTC_CONNECTION_PING', this.checkPing);
powercord.api.settings.registerSettings(this.entityID, {
category: this.entityID,
label: this.manifest.name,
render: Settings
});
}
checkPing (arg) {
if (!this.pingCheckEnabled) {
return;
}
const lastPing = arg.pings.pop().value;
const pingThreshold = this.settings.get('PingThreshold');
if (lastPing > pingThreshold) {
console.log(`[VC Reconnect] Ping higher than set threshold! Attempting to rejoin VC. ${lastPing} > ${pingThreshold}`);
this.reconnect();
}
}
reconnect () {
const setPing = () => {
this.pingCheckEnabled = !this.pingCheckEnabled;
};
const voiceId = getVoiceChannelId();
FluxDispatcher.subscribe('RTC_CONNECTION_STATE', function func (e) {
if (e.state === 'DISCONNECTED') {
selectVoiceChannel(voiceId);
setTimeout(() => {
setPing();
FluxDispatcher.unsubscribe('RTC_CONNECTION_STATE', func);
}, 10000);
}
});
setPing();
selectVoiceChannel(null);
}
/* Unsubscribe/Unregister when unloading the plugin */
pluginWillUnload () {
powercord.api.settings.unregisterSettings(this.entityID);
FluxDispatcher.unsubscribe('RTC_CONNECTION_PING', this.checkPing);
}
};