-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
102 lines (84 loc) · 3.33 KB
/
bot.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
const config = require('./config.json');
const { super_admins, admins, block_commands } = config.bot_settings;
const { address, port, password } = config.rcon_settings;
const Rcon = require('rcon');
const rcon = new Rcon(address, port, password, {
tcp: true,
challenge: true
});
rcon.connect();
const { VK } = require('vk-io');
const vk = new VK({
token: config.access_token,
pollingGroupId: config.group_id,
apiVersion: config.api_version
});
const { updates } = vk;
const sendCommand = async (command) => {
let response;
try {
rcon.send(command);
response = await new Promise((resolve, reject) => {
rcon.once('response', (res) => {
if(res == '') {
resolve('Сервер не вернул ответа. Но команда успешно отправлена!');
}
resolve(res);
});
setTimeout(() => {
reject('Сервер не ответил вовремя.');
}, 10000);
});
} catch (error) {
console.error('При отправке команды произошла ошибка:', error);
return 'При выполнении произошла ошибка.\nВозможно, сервер или RCON отключены!';
}
if (response === undefined) {
response = 'Сервер не вернул ответа.';
}
return response;
};
updates.on(['message_new'], async (context, next) => {
const { text, senderId, id } = context;
const startSymbol = text.substring(0, 1);
if(startSymbol == "/") {
const [ command, ...args ] = text.substring(1).split(' ');
console.log('Команда: ', command);
if (super_admins.includes(senderId)) {
const resp = await sendCommand(`${command} ${args.slice(' ')}`);
context.send('Результат выполнения команды:\n\n' + resp, {
reply_to: id
});
} else if (admins.includes(senderId)) {
if (block_commands.includes(command.toLowerCase())) {
context.send('Вы не можете использовать данную команду!', {
reply_to: id
});
} else {
const resp = await sendCommand(`${command} ${args.slice(' ')}`);
context.send('Результат выполнения команды:\n\n' + resp, {
reply_to: id
});
}
} else {
context.send('У вас нет прав на использование RCON!.', {
reply_to: id
});
}
}else{
context.send('Для обращения к серверу, используйте "/" в начале сообщения.', {
reply_to: id
});
}
return next();
});
rcon.on('auth', ()=> {
console.log(`RCON > Успешно авторизировался!`);
}).on('error', (err)=> {
console.error('RCON > Произошла ошибка: ', err);
}).on('end', ()=> {
console.log('RCON > Закрываю соединение.');
});
updates.start()
.then(()=> console.log('RCON-бот запущен!'))
.catch(err=> console.error('При запуске произошла ошибка:', err));