简体   繁体   English

discord.js 机器人用于替换“,”

[英]discord.js bot for replacing “,”

i'm creating a discord bot with node.我正在创建一个带有节点的 discord 机器人。 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.我希望当用户发送带有前缀“doc”的消息时,它将所有“,”替换为“word1”“word2”“word3”等之间的随机词......我不太了解这个领域. 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.输入:doc 我遇到了哈利,我们一起去游泳,然后哈利回家了。


output:I met Harry word5 we went for a swim together word5 and afterwards Harry went home. output:我遇到了哈利 word5 我们一起去游泳 word5 然后哈利回家了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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