NodeJS 知道用户是否说了一些具体的话

0vvn1miw  于 2023-06-22  发布在  Node.js
关注(0)|答案(2)|浏览(107)

我正在编写一个不和谐机器人,其中有一个命令可以查看用户在特定通道中发送的消息。在这个频道里,我们只能说“BLABLA”。我做了一个几乎可以工作的代码,如果我说“BLABLA”以外的话,机器人会删除消息,但如果我说例如:“嘿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");
        });
    }
  }
});
jslywgbw

jslywgbw1#

您可以使用Regular Expressions并只允许发送字母bBlLaA沿着String.prototype.startsWith(),以确保消息以“BLA”开头。

const RegEx = /^[BbLlAa]+$/

// Checking if the message contains any other character except b, B, l, L, a, A.
if (!message.content.match(RegEx)) {message.delete(); return message.author.send("You can only say BLABLA.");};

// Checking if the message starts with BLA.
if (!message.content.startsWith("BLA")) {message.delete(); return message.author.send("You can only say BLABLA.")};

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

if (message.content !== "BLABLA") {message.delete(); return message.author.send("You can only say BLABLA.")};
lmvvr0a8

lmvvr0a82#

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

const saidBlaBla = message.content.toLowerCase()
   .includes('blabla');

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

相关问题