簡體   English   中英

用 discord.js 制作機器人,如何讓我的機器人檢測到某個短語?

[英]Making a bot with discord.js, how to make my bot detect a certain phrase?

如果在我的不和諧中說出某個短語,我正在制作一個機器人,它會ping我。 目前我正在使用

if(message.content.toLowerCase() === 'word')

我怎樣才能使它能夠檢測到任何句子中的“單詞”? 編碼新手,所以我只是按照指南進行操作,幾個小時后我無法弄清楚。 如果只說“單詞”而沒有別的,我只會讓它來 ping 我。

一個簡單的解決方案是:

 function contains(content, word) { return (content.toLowerCase().indexOf('word') >= 0); } console.log(contains("This is the word!")); //true console.log(contains("This is WordPress")); //true console.log(contains("Something")); //false

如您所見,第二個示例不正確,因為 WordPress 在語義上與word不同。 所以,如果找到這個詞還不夠,但我們還想隔離它,那么我們可以這樣做:

 function contains(content, word) { let pattern = new RegExp("[^az]" + word + "[^az]"); return (pattern.test(content.toLowerCase())); } console.log(contains("This is the word!", "word")); //true console.log(contains("This is WordPress", "word")); //true console.log(contains("Something", "word")); //false

在第二個示例中,我在前面加上[^az] ,意思是“不是字母”

暫無
暫無

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

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