如何创建 discord.js 机器人命令并在用户响应中的任何位置找到它?
How to create a discord.js bot command and find it anywhere in the response from a user?
现在,如果只说了机器人用户名,我就在下面有了它,我希望如果用户在消息中的任何地方说机器人用户名,机器人就会看到它。
同样,这对我来说也是一个学习实验,让我了解如何用其他词来做到这一点,比如如何找到其中包含 AAPL 的消息,我的下一步是找到小写或用一个字母封顶等
if (message.content.startsWith("<@554504420206051328>")) {
message.channel.send('My advice to you, is buy AAPL.');
} else
.includes()
函数就是您要找的函数:
if (message.content.includes("<@554504420206051328>")) {
message.channel.send('My advice to you, is buy AAPL.');
}
如果您想了解更多信息,可以查看此功能documentation。
现在对于你的问题的第二部分(在消息中搜索 AAPL
),它实际上与第一部分相同:
if (message.content.toLowerCase().includes("aapl")) {
message.channel.send('I found AAPL');
}
.toLowerCase()
函数将捕获的消息内容以小写形式显示。所以当你在寻找aapl
时,你实际上是在寻找AAPL
、aapl
、Aapl
等
现在,如果只说了机器人用户名,我就在下面有了它,我希望如果用户在消息中的任何地方说机器人用户名,机器人就会看到它。
同样,这对我来说也是一个学习实验,让我了解如何用其他词来做到这一点,比如如何找到其中包含 AAPL 的消息,我的下一步是找到小写或用一个字母封顶等
if (message.content.startsWith("<@554504420206051328>")) {
message.channel.send('My advice to you, is buy AAPL.');
} else
.includes()
函数就是您要找的函数:
if (message.content.includes("<@554504420206051328>")) {
message.channel.send('My advice to you, is buy AAPL.');
}
如果您想了解更多信息,可以查看此功能documentation。
现在对于你的问题的第二部分(在消息中搜索 AAPL
),它实际上与第一部分相同:
if (message.content.toLowerCase().includes("aapl")) {
message.channel.send('I found AAPL');
}
.toLowerCase()
函数将捕获的消息内容以小写形式显示。所以当你在寻找aapl
时,你实际上是在寻找AAPL
、aapl
、Aapl
等