Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 知道用户是否说了一些特定的话_Javascript_Node.js_Discord_Discord.js - Fatal编程技术网

Javascript 知道用户是否说了一些特定的话

Javascript 知道用户是否说了一些特定的话,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,我正在编写一个discord机器人,有一个命令可以查看用户在特定通道中发送的消息。在这个频道里,我们只能说“布拉布拉”。我已经编写了一个几乎可以运行的代码,如果我说的不是“BLABLA”,机器人会删除消息,但是如果我说的是“hey BLABLA”,消息将不会被删除 这是我的密码: bot.on("message", (msg) => { if (msg.channel.id == "509789705651617793") { if (!

我正在编写一个discord机器人,有一个命令可以查看用户在特定通道中发送的消息。在这个频道里,我们只能说“布拉布拉”。我已经编写了一个几乎可以运行的代码,如果我说的不是“BLABLA”,机器人会删除消息,但是如果我说的是“hey BLABLA”,消息将不会被删除

这是我的密码:

bot.on("message", (msg) => {
  if (msg.channel.id == "509789705651617793") {
    if (!msg.content.toLowerCase().includes("bla")) {
      msg.delete(1);
      msg.author.send('You can only send "BLABLA" in <#509789705651617793>');

      bot.channels
        .get(`576073035476631563`)
        .send(
          `${msg.author}, tried to send something else than "blabla" in <#509789705651617793>, his message was: ${msg.content}`
        )
        .catch((err) => {
          console.error("err");
        });
    }
  }
});
bot.on(“消息”(msg)=>{
如果(msg.channel.id==“509789705651617793”){
如果(!msg.content.toLowerCase().包括(“bla”)){
删除(1);
msg.author.send('您只能在中发送“BLABLA”);
bot.channels
.get(`576073035476631563`)
.发送(
`${msg.author},试图在中发送“blabla”以外的内容,他的消息是:${msg.content}`
)
.catch((错误)=>{
控制台错误(“err”);
});
}
}
});

您需要检查
message.content的整个字符串BLABLA

const saidBlaBla = ['BLABLA'].some(word => message.content.includes(word))

if(!saidBlaBla) {
     msg.delete(1)
     msg.author.send('You can only send "BLABLA" in <#509789705651617793>')

// Rest of your code goes here
}
const saidballa=['BLABLA'].some(word=>message.content.includes(word))
如果(!saidballa){
msg.delete(1)
msg.author.send('您只能在'中发送“BLABLA”)
//剩下的代码放在这里
}
您可以使用并仅允许发送字母
b
b
l
l
a
a
,同时确保消息以“BLA”开头

const RegEx=/^[BbLlAa]+$/
//检查消息是否包含除b、b、l、l、a、a以外的任何其他字符。
if(!message.content.match(RegEx)){message.delete();return message.author.send(“您只能说blablabla。”);};
//检查消息是否以BLA开头。
if(!message.content.startsWith(“BLA”){message.delete();return message.author.send(“您只能说blablabla。”)};

另一种方法是检查消息是否等于“BLABLA”

if(message.content!=“BLABLA”){message.delete();return message.author.send(“您只能说blablabla.”);

“嘿,BLABLA”。toLowerCase()
。包括(“bla”)
。如果需要精确匹配,不要使用
。includes
。那么我可以使用什么呢?也许可以测试相等性,
=
?是的,但是用户可以发送“BLA”或“blablabla”或“blablablablabla”,所以我不能真正测试相等类型。那么,请注意字符类中的管道
[a | b]
只是一个文字管道字符。它不是“或”的意思,这就是角色类的意思。谢谢你的提示!我修改了正则表达式。