kowalski/main.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-05-24 18:39:12 -03:00
const TelegramBot = require('node-telegram-bot-api');
const fs = require('fs');
const path = require('path');
2024-06-02 01:38:25 -03:00
const token = process.env.TGBOT_TOKEN;
2024-05-24 18:39:12 -03:00
const bot = new TelegramBot(token, { polling: true });
2024-06-02 03:37:02 -03:00
const { isBlocked } = require('./blocklist');
2024-06-02 22:14:44 +00:00
const { isOnSpamWatch } = require('./spamwatch');
2024-06-02 14:24:03 +00:00
require('./logger');
2024-06-02 03:37:02 -03:00
2024-06-02 01:38:25 -03:00
const commandsPath = path.join(__dirname, 'commands');
2024-05-24 18:39:12 -03:00
const commandHandlers = {};
fs.readdirSync(commandsPath).forEach(file => {
const command = `/${path.parse(file).name}`;
2024-06-02 01:38:25 -03:00
const handler = require(path.join(commandsPath, file));
2024-05-24 18:39:12 -03:00
commandHandlers[command] = handler;
2024-06-02 01:38:25 -03:00
});
2024-05-24 18:39:12 -03:00
bot.on('message', (msg) => {
2024-06-02 12:12:32 +00:00
const userName = msg.from.first_name;
const userId = msg.from.id;
2024-06-02 12:12:32 +00:00
const messageText = msg.text;
2024-06-02 15:04:45 +00:00
if (msg.chat.type == 'private') {
2024-06-02 22:01:52 +00:00
if (isBlocked(userId) || isOnSpamWatch(userId)) {
2024-06-02 15:04:45 +00:00
console.log(`WARN: Blocked user ${userName}, ${userId} tried to access the bot with the command or message "${messageText}".\n`);
return;
}
console.log(`INFO: User ${userName}, ${userId} sended a command or message with the content:
${messageText}\n`)
}
2024-05-24 18:39:12 -03:00
if (commandHandlers[messageText]) {
commandHandlers[messageText](bot, msg);
}
});
2024-05-31 17:40:29 -03:00
bot.on('polling_error', (error) => {
console.error('WARN: Polling error:', error);
2024-05-31 17:40:29 -03:00
});
2024-06-02 01:38:25 -03:00
console.log(`INFO: Lynx started\n`);