UNPKG

spam-engel-modulu

Version:

Discord botlar için basit spam engelleme kütüphanesi

80 lines (71 loc) 2.42 kB
// lib/spamDetector.js class SpamDetector { constructor(options = {}) { this.limit = options.limit || 5; // Aynı mesajdan kaç tane olursa spam sayılacak this.timeWindow = options.timeWindow || 10000; // 10 saniye içinde this.users = new Map(); // Kullanıcı bazlı mesaj geçmişi } /** * @param {string} userId - Kullanıcı ID'si * @param {string} message - Gönderilen mesaj * @returns {boolean} - Spam ise true, değilse false */ check(userId, message) { const now = Date.now(); if (!this.users.has(userId)) { this.users.set(userId, []); } const userMessages = this.users.get(userId); // Zaman penceresinden eski mesajları temizle const recentMessages = userMessages.filter(time => now - time < this.timeWindow); recentMessages.push(now); this.users.set(userId, recentMessages); // Aynı mesajdan limit kadar varsa spam say return recentMessages.length >= this.limit; } /** * Kullanıcının mesaj geçmişini temizler * @param {string} userId */ clearUser(userId) { this.users.delete(userId); } /** * Tüm kullanıcı geçmişlerini temizler */ clearAll() { this.users.clear(); } /** * Kullanıcının spam durumunu gösterir * @param {string} userId * @returns {{count: number, isSpam: boolean}} */ getSpamStatus(userId) { const userMessages = this.users.get(userId) || []; const now = Date.now(); const recentMessages = userMessages.filter(time => now - time < this.timeWindow); return { count: recentMessages.length, isSpam: recentMessages.length >= this.limit }; } /** * Otomatik temizleme başlatır (dakika bazında) * @param {number} minutes */ startCleanupInterval(minutes = 1) { setInterval(() => { const now = Date.now(); for (const [userId, timestamps] of this.users) { const recent = timestamps.filter(t => now - t < this.timeWindow); if (recent.length === 0) { this.users.delete(userId); } else { this.users.set(userId, recent); } } }, minutes * 60 * 1000); } } module.exports = SpamDetector;