![](/img/trans.png)
[英]Using node.js and the discord.js library to message users when I run a command, but it only starts to work after the targeted user runs the command
[英]How to make an if statement work if a message starts the prefix + a command using discord.js
您好,對於我的 discord 機器人,我已經從使用 discord.py 切換到 discord.js。 我目前遇到問題,我希望我的機器人的一個功能能夠給出以命令和前綴開頭的句子(例如 *say Hello world)然后我希望機器人刪除前綴和命令並發送 rest內容回來。
但是我似乎無法讓 if 語句正常工作 vie 嘗試了這個
if (msg.content.startsWith == prefix + 'say')
這類似於 pythons 語法,但如果我使用它似乎不會觸發,我希望能夠輸入這樣的內容 (*say hello world) 並且 if 語句應該觸發,因為消息中的第一件事是命令,但是它不起作用
startsWith
不是屬性,它是 function。
您可以通過使用startsWith()
來實現您的目標
if (msg.content.startsWith(`${prefix}say`))
或者你可以檢查message.content
是否等於${prefix}say
if (msg.content === `${prefix}say`)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.