简体   繁体   中英

discord.js bot for replacing “,”

i'm creating a discord bot with node. I would like that when a user sends a message with the prefix "doc", it replaces all the "," with a random word between "word1" "word2" "word3" etc... I don't know this field well. I got to this point and I don't know how to do it:

bot.on("message", async message => {
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;

  let prefix = botconfig.prefix;
  let messageArray = message.content.split(" ");
  let cmd = messageArray[0];
  let args = messageArray.slice(1);

  //replace ","

});

I arrived at this point:

var words = [" word1 "," word2 "," word3 "," word4 "," word5 "," word6 "];


bot.on("message", async message => {
    if (message.content.includes("doc ")) {

      if(message.content.includes(",")){
        var txt = message.content.replace(/doc /gi, "");
        var rando = Math.floor((Math.random() * 6) + 1);
        var txt = txt.replace(/,/gi, words[rando]);
        
        message.channel.send(txt);

      }else{
        message.channel.send("the text doesn't contain any comma");
      }

    }

input:doc I met Harry, we went for a swim together, and afterwards Harry went home.


output:I met Harry word5 we went for a swim together word5 and afterwards Harry went home.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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