【问题标题】:How can I make my discord bot reply to a message when it contains a specific letter/character?当消息包含特定字母/字符时,如何让我的不和谐机器人回复消息?
【发布时间】:2021-05-20 06:32:15
【问题描述】:

所以基本上,我希望我的机器人回复为其他机器人输入命令的人,例如当他们输入 -p(play) 时(在此处插入歌曲) 我不希望我的机器人播放歌曲,而是希望它回复他们,就像人们喜欢通过各种答案对某人的意见发表想法一样。 我已经输入了一个命令,当人们写 $w 时它会回复他们,但我希望我的机器人在他们在 $w 之后添加其他内容时回复。

const replies = [
    "nice", "I like it", "We have the same tastes", "lucky!", "I wish I were you",
]

client.on("message", gotMessage);

async function gotMessage(msg) {

    if (msg.content === "$w"){
        const index = Math.floor(Math.random() * replies.length);
        msg.channel.send(replies[index]);
     }
}

【问题讨论】:

    标签: javascript node.js discord discord.js bots


    【解决方案1】:

    因此,如果我理解正确,您想检查消息中是否包含单词。 为此,您可以使用String.includes()

    if (msg.content.includes("$w"){
        //do stuff
    }
    

    编辑:

    您还可以检查消息是否以它开头

    if(msg.content.startsWith("$w")){
    //do stuff
    }```
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2021-05-07
      • 2019-07-11
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2021-06-22
      相关资源
      最近更新 更多