我正在構建一個不和諧的機器人,我希望機器人根據我提供的輸入進行回復。我的 JSON 檔案看起來像這樣。
const greetings = [
{
id: 1,
received: [
"Hi",
"Hi there",
"Hello",
"Hey",
"Hey there"
],
reply: "Hi there"
},
{
id: 2,
recived: [
"Wassup",
"Sup"
],
reply: "Nothing much. Wassup",
},
{
id: 3,
received: [
"How are you doing",
"How are you doing?",
"How have you been doing",
"How have you been doing?",
],
reply: "I'm good. And I hope you're doing well too",
},
];
當我在那里發送像 Hi 或 Hello 這樣的訊息時,我希望它在那里回復 Hi。當我發送諸如 How are you doing 之類的訊息時,它應該回復我很好。我希望你也做得很好。
如何檢查發送了哪條訊息并回傳適當的回復?
uj5u.com熱心網友回復:
您可以使用它Array.find來查找具有其值的陣列項并Array.some檢查它是否存在。
代碼將是:
if (greetings.some(x => x.received.includes(message.content))) {
message.channel.send(greetings.find(x => x.received.includes(message.content)).reply);
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/412327.html
標籤:
下一篇:如何匹配多維json中的兩個值
