简体   繁体   中英

Discord.JS Customizable Banned Words/Filter

I'm trying to make a discord banned words filter that is customizable per server, similar to how MEE6, Dyno, etc does.

I currently have the server and the specified words saved in a database. What I have currently to filter these words is to query a database search in every single messages sent and I'm sure it is not a good idea.

example code:

client.on("messageCreate", async message => {
sql.query(`SELECT * FROM words WHERE serverid = ${serverID}`, (err, res) => {
  let words = res[0]
    for (let i in words) {
    if(message.content.includes(words[i])) return message.delete()
    }
  })
})

I thought that this method will be incredibly slow and inefficient when there is thousands of messages being sent simultaneously, I'm wondering if there is a better way to do this by storing the words in a cache/map or something, thanks.

I would run that query only when the bot starts up, and store them inside an object in client object so it could be accessed easily without having to rely on global object.

client.wordFilters = {}
// for every server id
sql.query(`SELECT * FROM words WHERE serverid = ${serverID}`, (err, res) => {
     client.wordFilters[serverID] = [...res] 
// assuming res is an array of banned words 
// or you need to turn it into an array first
// or just use it as an object, use it as how you see fit
  })

then every time a new banned word is added, don't forget to also push it to the array inside client.wordFilters[serverID]

client.on("messageCreate", async message => {
  // assuming the res object above is an array
  for (let i = 0; i < client.wordFilters[serverID]; i++){
     let word = client.wordFilters[serverID][i]
     if(message.content.includes(word)) return message.delete()
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM