首页 文章

如何使用discord bot过滤单词? (JavaScript的)

提问于
浏览
-2

我最近为我的不和谐服务器创建了一个机器人 . 现在我希望他过滤坏话 .

For example:


用户(没有机器人):你是个混蛋


用户(有机器人):你是[我很笨,因为我发誓]


这在Discord中甚至可能吗?我给了我的机器人所有权限! (包括删除消息,它不能用自编程序编辑消息)

如果那是不可能的^我们可以做以下事情吗?

能够直接删除消息并编写以下内容:


机器人:@username不要发誓!


现在我有以下代码(我不知道是否有用):

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
    console.log('Hello, the bot is online!')
});

client.on('message', message => {
    if(message.content === '--Trump'){
        message.reply('He is the president of the United States of 
America!');
    }
    if(message.content === '--Putin'){
        message.reply('He is the president of Russia!');
    }
});

client.login('MzAwMzM5NzAyMD*1NDUxNzc4.C8rH5w.M44LW*nrfbCR_zHzd**vtMqkr6nI');

2 回答

  • -1

    Docs . 目前在Discord API中,没有可能的方法来编辑来自其他用户的消息 . 您可以完全删除该消息,也可以重新发送但已编辑 . 如果你想重新发送它,那么你可以使用:

    let censor = "[Sorry, I Swear]"; /* Replace this with what you want */
    client.on('message', message => {
        let edit = message.content.replace(/asshole/gi, censor);
        message.delete();
        message.channel.send(`${message.author.username}: ${edit}`);
    }
    

    输入>>>你好混蛋

    输出<<< AkiraMiura:你好[抱歉,我发誓]

    请注意,如果用户发送一个2000字节(Charater)长消息,您将无法发送固定版本,它将被删除 .

  • 0

    尝试:

    client.on('message', message => {
        message.edit(message.content.replace(/asshole/gi, "[I'm stupid because I swear]"))
           .then(msg => console.log(`Updated the content of a message from ${msg.author}`))
           .catch(console.error);
    });
    

    感谢@AndréDion从API中提出了正确的方法

相关问题