簡體   English   中英

如何讓我的 discord 機器人回復包含特定字母/字符的消息?

[英]How can I make my discord bot reply to a message when it contains a specific letter/character?

所以基本上,我希望我的機器人回復為其他機器人輸入命令的人,例如當他們輸入 -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]);
     }
}

因此,如果我理解正確,您想檢查消息中是否包含單詞。 為此,您可以使用String.includes()

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

編輯:

您還可以檢查消息是否以它開頭

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM