繁体   English   中英

删除包含特定单词的消息 discord.js v14

[英]delete a message if it contains a certain word discord js v14

我正在尝试删除来自用户的 Discord 上的一条消息,因为它包含列表中的一个或多个单词。

const badWords = ["badword1", "badword2", "badword3"];

client.on("guildBanAdd", (guild, user) => {
    const messages = guild.messages.cache.filter((m) => m.author.id === user.id);

    for (const message of messages.values()) {
        for (const badWord of badWords) {
            if (message.content.match(badWord)) {
                guild.members.ban(user);
                break;
            }
        }
    }
});

为此使用一些库往往很有用,因为上面建议的解决方案没有内置标记器。 这意味着如果有人要写诸如veryverynaughty之类的东西,它就不会被抓住,因为veryverynaughty很可能不在单词列表中,而[very, naughty]在。 或者替代方法是对该消息运行正则表达式。

但对于您的问题,您使用message.delete()删除消息时要小心,这不适用于超过 14 天的消息。 我现在找不到资源,但我认为有一个解决方法。

import Profanity from 'profanity-js'

const isMessageTextProfane = (message) => {
    const customBadwords = ["overthrow", "dictator"]
    const config = {
        language: "en-us"
    }
    const profanityInstance = new Profanity(message.content, config)
    profanityInstance.addWords(...customBadwords);
    return profanityInstance.isProfane(message.content)
}
client.on('guildBanAdd', (guild, user) => {

  const messages = guild.messages.cache.filter(m => m.author.id === user.id);


  for (const message of messages.values()) {
    for (const badWord of badWords) {
      if (isMessageTextProfane(message)) {
        message.delete()
        guild.members.ban(user);
        break;
      }
    }
  }
});
const badWords = ["badword1", "badword2", "badword3"];

client.on("messageCreate", (message) => {
    const words = message.split(" ");
    for (const word of words) {
        if (badWords.contains(word)) {
            message.delete();
        }
    }
});

我会使用 messageCreate 事件,因为它会跟踪新消息。 其次,我会将消息内容拆分为单词,然后遍历并检查其中一个单词是否在坏词列表中。 最后我会删除消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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