-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (88 loc) · 3.49 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
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
require("dotenv").config();
const fs = require("fs");
const moment = require("moment");
const log = (...args) => console.log(require("chalk").gray(moment().format()), ...args);
const Steam = require("./Steam");
log("Hello World!");
const TelegramBot = require("node-telegram-bot-api");
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, { polling: true });
bot.commands = new Map();
try {
const commandFiles = fs.readdirSync("commands").filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
bot.commands.set(command.data.name, command);
}
} catch (error) {
log("There was an error during command loading:", error);
}
bot.on("message", (ctx) => {
// This only lets bot_command entities pass
if (ctx.entities == undefined || ctx.entities[0].type != "bot_command") return;
const chatId = ctx.chat.id;
const text = ctx.text;
const command = bot.commands.get(text.substring(1, ctx.entities[0].length));
if (!command) return;
log(`${ctx.chat.username || ctx.chat.first_name} (${chatId}): ${text}`);
// Mutating context seems pretty unethic to me... but it gets the job done.
Object.assign(ctx, {
...ctx,
bot: bot,
reply: (text, options = {}) => bot.sendMessage(ctx.chat.id, text, options),
argStr: text.substring(ctx.entities[0].length + 1, text.length)
});
if (command.data.restricted && !process.env.ALLOWED_IDS.split(" ").map(x => Number(x)).includes(ctx.chat.id)) {
ctx.reply("Sorry, you are not allowed to run this command!");
return;
}
try {
command.run(ctx);
} catch (error) {
log(error);
ctx.reply("There was an error while executing this command!");
}
});
bot.on("callback_query", onCallbackQuery = (callbackQuery) => {
const action = callbackQuery.data;
const ctx = callbackQuery.message;
const accounts = Steam.accounts;
if (Object.keys(accounts).includes(action)) {
account = accounts[action];
const text = `Account: <a href="https://steamcommunity.com/profiles/${account.steamid}">${account.account_name}</a>\n` +
`Code: <code>${Steam.getAuthCode(account.shared_secret)}</code>\n\n` +
`Updated on ` + moment().format("lll") + ` (${Math.floor(Math.random() * 1000)})`;
// API returns weird stuff when you're updating message with same content, cba to handle => add random number
const opts = {
chat_id: ctx.chat.id,
message_id: ctx.message_id,
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [
[{
text: "Back",
callback_data: "accounts"
},
{
text: "Refresh",
callback_data: action
}
]
]
})
};
bot.editMessageText(text, opts);
} else if (action == "accounts") {
bot.editMessageText("Pick an account below:", {
...Steam.accountPicker(),
chat_id: ctx.chat.id,
message_id: ctx.message_id
});
}
});
// Haven't encountered these errors yet, but you know...
bot.on("polling_error", (error) => log(error));
bot.on("webhook_error", (error) => log(error));
process.on("SIGINT", function () {
log("SIGINT receieved, goodbye :)");
process.exit();
});