-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReconnect.js
110 lines (101 loc) · 3.74 KB
/
Reconnect.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
registerPlugin({
name: 'Reconnect',
version: '2.3a',
description: 'Reconnect after connection lost or kick - with optional automatic reconnect after x minutes',
author: 'TS3index.com <[email protected]> / Modded by Michael <[email protected]>',
backends: ['ts3', 'discord'],
vars: [
{
name: 'autorestart',
title: 'Auto-Neustart der Instanz alle X - Minuten',
type: 'checkbox'
},
{
name: 'restarttime',
indent: 2,
title: 'Minuten:',
type: 'number',
placeholder: '1440',
conditions: [
{ field: 'autorestart', value: true }
]
},
{
name: 'spacer0',
title: '',
},
{
name: 'dev',
title: 'enable DEBUG',
type: 'checkbox'
}
]
}, function(_, config, meta) {
var engine = require('engine');
var backend = require('backend');
var event = require('event');
var message = meta.name + ' V ' + meta.version +' > ';
if (!config || typeof config.autorestart == 'undefined') {
config.autorestart = false;
engine.saveConfig(config);
}
if (!config || typeof config.dev == 'undefined') {
config.dev = false;
engine.saveConfig(config);
}
if (typeof config.restarttime == 'undefined' || !config.restarttime || !parseInt(config.restarttime) || config.restarttime < 10) {
config.restarttime = 1440;
engine.saveConfig(config);
}
event.on('load', () => {
let messagetxt = message + 'loaded successfully.';
if (config.autorestart) messagetxt + ' - Autorestart: ' + config.restarttime + ' Minutes';
engine.log(messagetxt);
});
event.on('disconnect', () => {
if (config.dev) engine.log(message + 'detect disconnect event.');
checkConnection();
});
event.on('clientKicked', (moveInfo) => {
if (moveInfo.client.isSelf()) {
if (config.dev) engine.log(message + 'detect, bot was kicked');
setTimeout(checkConnection, 3000);
}
});
if (config.autorestart) {
setInterval(function(){
engine.log(message + 'Auto-Restart instance ...');
backend.disconnect();
waitForBackend(10,5);
}, config.restarttime * 1000 * 60);
}
setInterval(checkConnection, 60000);
function checkConnection() {
// if (config.dev) engine.log(message + 'check connection interval start.');
if (!backend.isConnected() || !engine.isRunning()) {
if (config.dev) engine.log(message + 'something is wrong. Try to reconnect...');
backend.disconnect();
waitForBackend(10, 5);
}
}
function waitForBackend(attempts, wait) {
return new Promise((success, fail) => {
let attempt = 1;
const timer = setInterval(() => {
setTimeout(() => { backend.connect(); }, 3500);
if (backend.isConnected()) {
clearInterval(timer);
if (config.dev) engine.log('waitForBackend() took ' + attempt + ' attempts with a timer of ' + wait + ' seconds to resolve');
success();
return;
} else if (attempt > attempts) {
clearInterval(timer);
if (config.dev) engine.log('waitForBackend() failed at ' + attempt + '. attempt with a timer of ' + wait + ' seconds');
fail('backend');
return;
}
attempt++;
}, wait * 1000);
});
}
});