最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I filter words with a discord bot? (JavaScript) - Stack Overflow

programmeradmin6浏览0评论

I have recently created a bot for my discord server. Now I want him to filter bad words.

For example:


User (without bot): You are an asshole


User (with bot): You are an [I'm stupid because I swear]


Is this even possible in Discord? I have given my bot all permissions! (including removing messages, it can't edit message with the program self tho)

If that is not possible^ Can we do the following?

The ability to directly delete the message and write the following:


Bot: @username Do not swear!


Now I have the following code (I dont know if useful):

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');

I have recently created a bot for my discord server. Now I want him to filter bad words.

For example:


User (without bot): You are an asshole


User (with bot): You are an [I'm stupid because I swear]


Is this even possible in Discord? I have given my bot all permissions! (including removing messages, it can't edit message with the program self tho)

If that is not possible^ Can we do the following?

The ability to directly delete the message and write the following:


Bot: @username Do not swear!


Now I have the following code (I dont know if useful):

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');
Share Improve this question edited Mar 18, 2019 at 10:33 chrips 5,3166 gold badges28 silver badges48 bronze badges asked Apr 8, 2017 at 20:46 MCKMCK 6452 gold badges7 silver badges10 bronze badges 3
  • @André Dion No, thank you for the link! Im quite new to javascript. How van I use that code? – MCK Commented Apr 9, 2017 at 8:05
  • 1 Please hide your client.login() line. I am pretty sure you don't want people to have random access to your bot if I am correct... – csk Commented Jul 15, 2017 at 5:48
  • I have covered it already with some "*"'s – MCK Commented Jul 19, 2017 at 18:54
Add a ment  | 

3 Answers 3

Reset to default 2

Docs. Currently in the Discord API there is no possible way to edit a message from another user. You could pletely delete the message or you could resend it but edited. If you want to resend it then you could use:

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}`);
}

Input >>> Hello asshole

Output <<< AkiraMiura: Hello [Sorry, I Swear]

Take note that if the user sends a 2000 byte (Charater) long message you won't be able to send a fixed version and it would just get deleted.

Use Regex to help you detect from blacklisted words you wanted to.

For example, If you want to blacklist the word asshol*, use the regex to detect the word:

if ((/asshole/gm).test(message.content)) 
  message.delete().then(() => {
    message.reply('Do not swear!'); // Sends: "@user1234 Do not swear!"
  });
}

If you wanted to blacklist/filter MULTIPLE words, like fu*k and sh*t

Use separators in Regex: /(word1|word2|word3)/gm So... use:

if ((/(fuck|shit)/gm).test(message.content)) {
    message.delete().then(() => {
      message.reply('Do not swear!');
    });
}

It's fully working!

Re-write in FULL code,

client.on('message', (message) => {
  if ((/asshole/gm).test(message.content)) {
    message.delete().then(() => {
      message.reply('Do not swear!'); // Sends: "@user1234 Do not swear!"
    });
  }
});

Tell me if it works

Good Luck,
Jedi

try:

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);
});

credit to @André Dion for bringing up the right method from the API

发布评论

评论列表(0)

  1. 暂无评论