Added main code + middleware

This commit is contained in:
Lucas Gabriel 2024-07-28 13:31:24 -03:00
parent 4c13dc91e9
commit 8fe6dce44d
No known key found for this signature in database
GPG Key ID: D9B075FC6DC93985
2 changed files with 37 additions and 0 deletions

9
Middleware.js Normal file
View File

@ -0,0 +1,9 @@
module.exports = (isOnSpamWatch) => {
return async (ctx, next) => {
if (await isOnSpamWatch(ctx.from.id)) {
console.log(`User ${ctx.from.id} is banned on SpamWatch. Blocking command.`);
return;
}
await next();
};
};

28
spamwatch.js Normal file
View File

@ -0,0 +1,28 @@
const fs = require('fs');
const path = require('path');
const blocklistPath = path.join(__dirname, '../../props/sw_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: SpamWatch blocklist file not found. Creating a new (blank) one.\nUse your API key to push the blocklist to the file.');
fs.writeFileSync(blocklistPath, '');
} else {
console.error('WARN: Error reading SpamWatch blocklist:', error);
}
}
};
const isOnSpamWatch = (userId) => {
return blocklist.includes(String(userId));
};
readBlocklist();
module.exports = { isOnSpamWatch };