-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
138 lines (130 loc) · 4.37 KB
/
handler.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const botconfig = require('./config/config.json');
const fs = require('fs');
const mysql = require('promise-mysql');
const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot(botconfig.token, {polling: true});
var pool;
(async () => {
pool = await mysql.createPool({
'connectionLimit': 113,
'host': botconfig.mysqlConnection.host,
'port': botconfig.mysqlConnection.port,
'user': botconfig.mysqlConnection.user,
'password': botconfig.mysqlConnection.password,
'database': botconfig.mysqlConnection.database,
});
})();
bot.commands = new Map();
fs.readdir('./commands/', (err, files) => {
if (err) console.log(err);
let jsfile = files.filter(f => f.split('.').pop() === 'js');
if (jsfile.length <= 0) {
console.log('Couldn\'t find commands.');
return;
}
jsfile.forEach((f, i) => {
let props = require(`./commands/${f}`);
console.log(`${f} loaded!`);
bot.commands.set(props.help.name, props);
});
});
bot.on('getUpdates', (update) => {
console.log(update);
});
bot.on('message', (msg) => {
try {
let messageArray = '';
(async () => {
try {
messageArray = msg.text.split(' ');
let cmd = messageArray[0];
let args = msg.text.slice(cmd.length + 1);
let commandRequest = cmd.slice(1);
if (cmd.substring(0, 1) !== '/') {
return;
}
commandfile = bot.commands.get(commandRequest.substring(0,
(commandRequest.indexOf('@') === -1
? commandRequest.length
: commandRequest.indexOf('@'))));
if (commandfile) {
commandfile.run(botconfig, pool, bot, msg, args);
} else {
commandfile = commandRequest.substring(0,
(commandRequest.indexOf('@') === -1
? commandRequest.length
: commandRequest.indexOf('@')));
let qry_user = 'SELECT enabled FROM groups g WHERE g.id_telegram = ' +
msg.chat.id + ';';
let userData = await pool.query(qry_user);
if (!(typeof userData[0] === 'undefined' || userData[0].enabled ===
1)) {
return;
}
let qry_provider = 'SELECT rand() ord, cp.command, cp.endpoint, pr.description, pr.code, pr.destination, pr.api_user, pr.api_key ' +
'FROM command_provider cp ' +
' INNER JOIN provider pr ' +
' ON pr.id_provider = cp.id_provider ' +
'WHERE cp.command = \'' + commandfile + '\' ' +
' AND cp.deleted = 0 ' +
'ORDER BY 1 ' +
'LIMIT 1;';
commandfile = await pool.query(qry_provider);
if (typeof commandfile[0] === 'undefined') {
return;
}
let imagePromise = (bot.commands.get('request')).run(botconfig, pool,
bot, msg, commandfile[0]);
let imageUrl = await imagePromise;
let extension = imageUrl.substring(imageUrl.lastIndexOf('.') + 1,
imageUrl.length);
let send = {};
switch (extension) {
case 'gif':
case 'mp4':
send = require(`./lib/sendAnimation.js`);
break;
case 'webm':
case 'webp':
send = require(`./lib/sendSticker.js`);
break;
default:
send = require(`./lib/sendPhoto.js`);
break;
}
await send.run(botconfig.token, msg.chat.id,
commandfile[0].description, imageUrl);
let group = require(`./lib/group.js`);
await group.run(pool, msg.chat.id);
console.log();
}
} catch (e) {
console.log(e.message);
}
})();
} catch (e) {
//console.log(e.message);
}
});
bot.on('inline_query', (msg) => {
try {
if (commandfile) {
let query = encodeURIComponent(msg.query.trim());
let returntext = commandfile.run(botconfig, null, bot, msg, msg.query).
then(returntext => {
bot.answerInlineQuery(msg.id, [
{
type: 'article',
id: query + '_exchange',
title: `Exchange ${msg.query}`,
input_message_content: {
message_text: returntext,
},
},
]);
});
}
} catch (e) {
console.log(e.message);
}
});