Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

javascript - how to make your discord bot log a message after it deletes it?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...