-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
76 lines (67 loc) · 2.32 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
const botconfig = require("./config/config.json");
const fs = require("fs");
const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot(botconfig.token, {polling: true});
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;
}
let commandfile = bot.commands.get(commandRequest.substring(0, (commandRequest.indexOf('@') === -1 ? commandRequest.length : commandRequest.indexOf('@'))));
if (commandfile) {
bot.sendMessage(msg.chat.id, await commandfile.run(botconfig, null, bot, msg, args));
}
} catch (e) {
console.log(e.message);
}
})();
} catch (e) {
console.log(e.message);
}
});
bot.on('inline_query', (msg) => {
try {
let commandfile = bot.commands.get('exchange');
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);
}
});