UNPKG

dspamdetector

Version:

Detect spam in a message.

184 lines 7.69 kB
function caps (text) { var capCount = 0 text = text.split(" ").join("") var chars = text.split('') let caracteres = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ".split("") chars.forEach(function (char) { if(caracteres.some(p => char.includes(p))){ if (char === char.toUpperCase()) { capCount++ } } }) return (capCount / text.length) * 100 } let letras = "abcdefghijklmnñopqrstuvwxyz".split("") function char_count(str,letter) { var arr = []; for(var i = 0; i < str.length; i++) { if (str[i].toLowerCase() === letter) arr.push(i); } return (arr.length * 100) / str.length; } function mayor(text){ let porcentajes = [] letras.forEach(l => { porcentajes.push(char_count(text,l)) }) return Math.max(...porcentajes) } function borrar(server,canal,usuario,tiempo){ setTimeout(function(){ let index = flood.map(f => f.server).indexOf(server) if(index == -1) return let indexc = flood[index].canales.map(c => c.canal).indexOf(canal) if(indexc == -1) return let indexu = flood[index].canales[indexc].usuarios.map(u => u.id).indexOf(usuario) if(indexu == -1) return flood[index].canales[indexc].usuarios.splice(indexu,1) if(flood[index].canales[indexc].usuarios.length == 0){ flood[index].canales.splice(indexc,1) } if(flood[index].canales.length == 0){ flood.splice(index,1) } },tiempo*1000) } let flood = [] function add(userid,serverid,channelid,limit,tiempo){ if(flood.map(f => f.server).indexOf(serverid) == -1){ flood.push({ server:serverid, canales:[ { canal:channelid, usuarios:[ {id:userid, mensajes:1 } ] } ] }) borrar(serverid,channelid,userid,tiempo) return false } else { let index = flood.map(f => f.server).indexOf(serverid) let indexc = flood[index].canales.map(c => c.canal).indexOf(channelid) if(indexc == -1){ flood[index].canales.push({ canal:channelid, usuarios:[ {id:userid, mensajes:1 } ] }) borrar(serverid,channelid,userid,tiempo) return false } else { let indexu = flood[index].canales[indexc].usuarios.map(u => u.id).indexOf(userid) if(indexu == -1){ flood[index].canales[indexc].usuarios.push({ id:userid, mensajes:1 }) borrar(serverid,channelid,userid,tiempo) return false } else { flood[index].canales[indexc].usuarios[indexu].mensajes += 1 if(flood[index].canales[indexc].usuarios[indexu].mensajes >= limit){ borrar(serverid,channelid,userid,tiempo) return true } } } } } const defaultoptions = { minletters:4, minwords:0, maxpercentcaps:15, maxpercentletters:60, blockedwords:[], floodml:3, floodt:60 } module.exports = { detector:function(opciones){ if(!opciones){ opciones = JSON.parse(JSON.stringify(defaultoptions)); } if(opciones.minwords == undefined || typeof opciones.minwords !== "number"){ opciones.minwords = defaultoptions.minwords } if(opciones.minletters == undefined || typeof opciones.minletters !== "number"){ opciones.minletters = defaultoptions.minletters } if(opciones.maxpercentcaps == undefined || typeof opciones.maxpercentcaps !== "number"){ opciones.maxpercentcaps = defaultoptions.maxpercentcaps } if(opciones.maxpercentletters == undefined || typeof opciones.maxpercentletters !== "number"){ opciones.maxpercentletters = defaultoptions.maxpercentletters } if(opciones.blockedwords == undefined || typeof opciones.blockedwords !== "object"){ opciones.blockedwords = [] } if(opciones.floodml == undefined || typeof opciones.floodml !== "number"){ opciones.floodml = defaultoptions.floodml } if(opciones.floodt == undefined || typeof opciones.floodt !== "number"){ opciones.floodt = defaultoptions.floodt } this.opciones = opciones for(let i = 0;i<this.opciones.blockedwords.length;i++){ if(typeof this.opciones.blockedwords[i] !== "string"){ let index = this.opciones.blockedwords.findIndex(this.opciones.blockedwords[i]) this.opciones.blockedwords.splice(index,1) } } this.setOptions = function(opcion){ if(!opcion){ opciones = JSON.parse(JSON.stringify(defaultoptions)); } if(opcion.minwords == undefined || typeof opcion.minwords !== "number"){ opcion.minwords = defaultoptions.minwords } if(opcion.minletters == undefined || typeof opcion.minletters !== "number"){ opcion.minletters = defaultoptions.minletters } if(opcion.maxpercentcaps == undefined || typeof opcion.maxpercentcaps !== "number"){ opcion.maxpercentcaps = defaultoptions.maxpercentcaps } if(opcion.maxpercentletters == undefined || typeof opcion.maxpercentletters !== "number"){ opcion.maxpercentletters = defaultoptions.maxpercentletters } if(opcion.blockedwords == undefined || typeof opcion.blockedwords !== "object"){ opcion.blockedwords = [] } if(opcion.floodml == undefined || typeof opcion.floodml !== "number"){ opcion.floodml = defaultoptions.floodml } if(opcion.floodt == undefined || typeof opcion.floodt !== "number"){ opcion.floodt = defaultoptions.floodt } this.opciones = opcion return `Opciones actualizadas` } this.isSpam = async function(texto){ if(!texto) throw new Error("No ha dado ningun texto.") if(this.opciones.minletters >= texto.length) return false if(this.opciones.blockedwords.some(p => texto.includes(p))) return true if(mayor(texto) > this.opciones.maxpercentletters) return true if(caps(texto) > this.opciones.maxpercentcaps) return true if(texto.split(" ").length < this.opciones.minwords) return true return false } this.isFlood = async function(userid,serverid,channelid){ if(!userid) throw new Error("No ha dado la id del usuario.") if(!serverid) throw new Error("No ha dado la id del servidor.") if(!channelid) throw new Error("No ha dado la id del canal.") return add(userid,serverid,channelid,this.opciones.floodml,this.opciones.floodt) } } }