Implemented blocklist + some code changes

This commit is contained in:
Lucas Gabriel 2024-06-02 01:48:19 -03:00
parent 945182329e
commit 4b0e188467
No known key found for this signature in database
GPG Key ID: D9B075FC6DC93985
13 changed files with 48 additions and 12 deletions

View File

@ -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": {

28
src/blocklist.js Normal file
View File

@ -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 };

View File

@ -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}`);
}

View File

@ -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}`);
}

View File

@ -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}`);
}

View File

@ -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}`);
}

View File

@ -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}`);
}

View File

@ -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}`);
}

View File

@ -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}`);
}

View File

@ -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}`);
};

View File

@ -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}`);
}

View File

@ -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`);
});
};

View File

@ -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();