From 4b0e1884674046a6667ace9047464beba7397750 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel <90426410+lucmsilva651@users.noreply.github.com> Date: Sun, 2 Jun 2024 01:48:19 -0300 Subject: [PATCH] Implemented blocklist + some code changes --- package.json | 2 +- src/blocklist.js | 28 ++++++++++++++++++++++++++++ src/commands/chatinfo.js | 2 +- src/commands/customize.js | 2 +- src/commands/furry.js | 2 +- src/commands/gay.js | 2 +- src/commands/help.js | 2 +- src/commands/random.js | 2 +- src/commands/start.js | 2 +- src/commands/stats.js | 2 +- src/commands/whois.js | 2 +- src/logger.js | 2 +- src/main.js | 10 +++++++++- 13 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 src/blocklist.js diff --git a/package.json b/package.json index 1822993..cb8377c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "A simple Telegram bot made in Node.js", "main": "src/main.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "echo \"WARN: no test specified\" && exit 1", "start": "node --env-file=config.env src/main.js" }, "repository": { diff --git a/src/blocklist.js b/src/blocklist.js new file mode 100644 index 0000000..2c7a0ec --- /dev/null +++ b/src/blocklist.js @@ -0,0 +1,28 @@ +const fs = require('fs'); +const path = require('path'); + +const blocklistPath = path.join(__dirname, '../blocklist.txt'); + +let blocklist = []; + +const readBlocklist = () => { + try { + const data = fs.readFileSync(blocklistPath, 'utf8'); + blocklist = data.split('\n').map(id => id.trim()).filter(id => id !== ''); + } catch (error) { + if (error.code === 'ENOENT') { + console.log('WARN: Blocklist file not found. Creating a new one.'); + fs.writeFileSync(blocklistPath, ''); // Create an empty blocklist file + } else { + console.error('WARN: Error reading blocklist:', error); + } + } +}; + +const isBlocked = (userId) => { + return blocklist.includes(String(userId)); +}; + +readBlocklist(); + +module.exports = { isBlocked }; diff --git a/src/commands/chatinfo.js b/src/commands/chatinfo.js index c059bc1..27b0f02 100644 --- a/src/commands/chatinfo.js +++ b/src/commands/chatinfo.js @@ -31,6 +31,6 @@ module.exports = function(bot, msg) { const message = chatNameOutput; bot.sendMessage(chatId, message, { parse_mode: 'Markdown' }) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /chatinfo executed by ${userName}, ${userId}`); } diff --git a/src/commands/customize.js b/src/commands/customize.js index 9cb9b16..310cc46 100644 --- a/src/commands/customize.js +++ b/src/commands/customize.js @@ -19,6 +19,6 @@ module.exports = function(bot, msg) { const message = "Select your pronouns:"; bot.sendMessage(chatId, message, opts,{ parse_mode: 'Markdown' }) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /customize executed by ${userName}, ${userId}`); } diff --git a/src/commands/furry.js b/src/commands/furry.js index 65a1160..d150ace 100644 --- a/src/commands/furry.js +++ b/src/commands/furry.js @@ -19,6 +19,6 @@ module.exports = function(bot, msg) { const message = `${isFurry}`; bot.sendMessage(chatId, message, { parse_mode: 'Markdown' }) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /furry executed by ${userName}, ${userId}`); } diff --git a/src/commands/gay.js b/src/commands/gay.js index 2456dc4..7331044 100644 --- a/src/commands/gay.js +++ b/src/commands/gay.js @@ -19,6 +19,6 @@ module.exports = function(bot, msg) { const message = `${isGay}`; bot.sendMessage(chatId, message, { parse_mode: 'Markdown' }) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /gay executed by ${userName}, ${userId}`); } diff --git a/src/commands/help.js b/src/commands/help.js index f82a159..cc24f51 100644 --- a/src/commands/help.js +++ b/src/commands/help.js @@ -17,6 +17,6 @@ module.exports = function(bot, msg) { `Thanks to all users, testers, contributors, and others. Without you, perhaps this bot wouldn't be possible ❤️`; bot.sendPhoto(chatId, lynxFullPhoto, { caption: message, parse_mode: 'Markdown' }) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /help executed by ${userName}, ${userId}`); } diff --git a/src/commands/random.js b/src/commands/random.js index 69438f3..c0e8e28 100644 --- a/src/commands/random.js +++ b/src/commands/random.js @@ -12,7 +12,7 @@ module.exports = function(bot, msg) { const message = `*Generated value:* ${randomValue}`; bot.sendMessage(chatId, message, { parse_mode: 'Markdown' }) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /random executed by ${userName}, ${userId}`); } \ No newline at end of file diff --git a/src/commands/start.js b/src/commands/start.js index da32f80..543f4b1 100644 --- a/src/commands/start.js +++ b/src/commands/start.js @@ -7,6 +7,6 @@ module.exports = function(bot, msg) { const message = `*Hello! I am Lynx!*\nI was made with love by Lucas Gabriel (lucmsilva)!\n\nSee /help for the bot commands!`; bot.sendPhoto(chatId, lynxProfilePhoto, { caption: message, parse_mode: 'Markdown' } ) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /start executed by ${userName}, ${userId}`); } diff --git a/src/commands/stats.js b/src/commands/stats.js index 3012985..35d2fd9 100644 --- a/src/commands/stats.js +++ b/src/commands/stats.js @@ -31,6 +31,6 @@ module.exports = function (bot, msg) { const message = getSystemInfo(); bot.sendMessage(chatId, message, { parse_mode: 'Markdown' }) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /stats executed by ${userName}, ${userId}`); }; diff --git a/src/commands/whois.js b/src/commands/whois.js index c058e2a..9014443 100644 --- a/src/commands/whois.js +++ b/src/commands/whois.js @@ -24,6 +24,6 @@ module.exports = function(bot, msg) { const message = `*Your name is:* ${userName}\n${haveUsername}\n*Your ID is:* ${userId}\n*You are a bot:* ${isBot}\n*Your language:* ${userLang}\n\n${userPremiumOutput}`; bot.sendMessage(chatId, message, { parse_mode: 'Markdown' }) - .catch(error => console.error('ERROR: Message cannot be sent:', error)); + .catch(error => console.error('WARN: Message cannot be sent:', error)); console.log(`INFO: /whois executed by ${userName}, ${userId}`); } diff --git a/src/logger.js b/src/logger.js index 7a0aea3..b77ea70 100644 --- a/src/logger.js +++ b/src/logger.js @@ -34,7 +34,7 @@ const logMessage = async (message) => { console.log = (message) => { logMessage(message).catch(err => { - process.stderr.write(`Error writing to log: ${err}\n`); + process.stderr.write(`WARN: Error writing to log: ${err}\n`); }); }; diff --git a/src/main.js b/src/main.js index 051b7f1..bc8c2e4 100644 --- a/src/main.js +++ b/src/main.js @@ -2,6 +2,7 @@ const TelegramBot = require('node-telegram-bot-api'); const fs = require('fs'); const path = require('path'); const logMessage = require('./logger'); +const { isBlocked } = require('./blocklist'); // Importa a função de blocklist const token = process.env.TGBOT_TOKEN; const bot = new TelegramBot(token, { polling: true }); @@ -15,6 +16,13 @@ fs.readdirSync(commandsPath).forEach(file => { }); bot.on('message', (msg) => { + const userId = msg.from.id; + + if (isBlocked(userId)) { + console.log(`WARN: Blocked user ${userId} tried to access the bot.`); + return; + } + const messageText = msg.text; if (commandHandlers[messageText]) { commandHandlers[messageText](bot, msg); @@ -22,7 +30,7 @@ bot.on('message', (msg) => { }); bot.on('polling_error', (error) => { - console.error('Polling error:', error); + console.error('WARN: Polling error:', error); }); const date = new Date().toString();