替代message.content.startsWith

我正在使用Discord机器人,并且正在使用页面执行帮助命令。

我有这样的设置:

if(message.content.startWith(`${prefix}help`))

if(message.content.startWith(`${prefix}help 2`))

If i do >help 2 I get both. Is there any way to stop that from happening?

评论
  • 逗妇乳
    逗妇乳 回复

    When using startsWith(">help") (not startWith) it will match ">help 2" because that string does indeed start with ">help". If you want to match these cases more precisely you should put the most specific cases first and use else if for subsequent comparisons.

    if (message.content.startsWith(`${prefix}help 2`)) { /* do stuff */ }
    else if (message.content.startsWith(`${prefix}help`)) { /* do other */ }
    

    如果第一种情况不匹配,则上面的代码将仅与第二种情况匹配。