2

I'm trying to make my discord bot pick up words I don't want people to use. I know how to make the bot delete the message containing the word and reply to the person appropriately. What I can't figure out is how to make the bot record the message containing the word and send it to the moderation log channel as an embed to log what message it just deleted and who sent it.

I've seen examples on how to log every deleted message but that's not what I want to do. I only want the bot to record messages it deleted whenever people use the word. (or log the message with the word before deleting it from the main channel)

this is the code i'm using so far, not sure how to add that part to it.

client.on('message', message => {
    const args = message.content.split(" ").slice(1);
    if(message.content.toLowerCase() === "word") {
        message.delete()
        message.reply("don't send that word here.");
    }
  });

1 Answer 1

1
if(message.content.toLowerCase().includes("word")) {
       let badMsg = message.content;
       let badMsgChan = message.guild.channels.cache.get(message.channel.id);
       let badMsgUser = message.author;
       let logChan = message.guild.channels.cache.find(ch => ch.name === "logs");

       let emb = new Discord.MessageEmbed()
          .setTitle("Blacklisted word used")
          .addField("Content", badMsg, true)
          .addField("Found in", badMsgChan, true)
          .addField("Written by", badMsgUser, true)
          .setTimestamp()

       logChan.send(emb);

       message.delete();
       message.reply("don't send that word here.");
}

I would recommend using .includes() instead of ===, because === means it has to be exactly that value (word). .includes() checks if the value (word) is included in the message content.

So I've created some variables that store the information about that "bad" message. badMsg is storing the message content (the sentence or whatever the word was included in). badMsgChan stores the channel, the "bad" message was found in. badMsgUser stores the user who has sent the "bad" message. And logChan is the channel where you want to send the embed in. You can change everything exactly in your style, I just wanted to give an example of how you could solve your problem. So the embed has 3 fields with:

  • Content -> The "bad" message content.
  • Found in -> The Channel where the "bad" message was found in.
  • Written by -> The user who sent the message.

Not the answer you're looking for? Browse other questions tagged or ask your own question.