为啥这个命令在不应该触发的时候触发?

Posted

技术标签:

【中文标题】为啥这个命令在不应该触发的时候触发?【英文标题】:Why is this command triggering when it isn't supposed to?为什么这个命令在不应该触发的时候触发? 【发布时间】:2019-02-13 03:28:28 【问题描述】:

我得到以下代码才能正常工作。它旨在删除某人在不和谐中的颜色组,当他们换成新颜色时。这工作得很好,现在在服务器上已经有一周左右的时间了。但是就在今天,有人在服务器上输入了“和蓝眼睛”作为消息,尽管没有输入命令,但它将该人的颜色组更改为蓝色。我将代码设置为仅在术语“!colorme”启动命令时触发。我不知道为什么它会触发“!colorme”以外的其他东西作为开始。关于为什么在不应该触发的情况下触发的任何想法?

const botSettings = require("./botsettings.json");
const Discord = require("discord.js");
const bot = new Discord.Client(
  disableEveryone: true
);

console.log(botSettings.token);
console.log(botSettings.prefix);

bot.on("ready", () => 
  console.log("Rebot is ready!");

  bot.generateInvite(["ADMINISTRATOR"]).then(link => 
    console.log(link);
  ).catch(err => 
    console.log(err.stack);
  );
);

bot.on('message', async (message) => 

  if (message.content.toLowerCase().startsWith("!colorme"))
    message.member.roles.array().some(r => 
      if (["Red", "RedOrange", "Orange", "OrangeYellow", "Yellow", "YellowGreen", "Green", "GreenBlue", "Blue", "Indigo", "Violet", "RedViolet", "PastelRed", "PastelOrange", "PastelYellow", "PastelGreen", "PastelBlue", "PastelIndigo", "PastelViolet", "DarkRed", "DarkOrange", "DarkYellow", "DarkGreen", "DarkBlue", "DarkIndigo", "DarkViolet"].includes(r.name)) 
        message.member.removeRole(r);
      
    )

  
    var args = message.content.toLowerCase().split(" ");
    // console.log(args);

    if (args[1] === 'blue') 
      var role = message.guild.roles.find(role => role.name === 'Blue')
      addUserRole('Blue', message);
      message.channel.send('Role successfully added!')
        .then(message => 
          message.delete(3000)
        )
     else if (args[1] === 'red') 
      addUserRole('Red', message);
      message.channel.send('Role successfully added!')
        .then(message => 
          message.delete(3000)
        )
       else if (args[1] === 'redorange') 
        addUserRole('RedOrange', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'orange') 
        addUserRole('Orange', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
     else if (args[1] === 'orangeyellow') 
      addUserRole('OrangeYellow', message);
      message.channel.send('Role successfully added!')
        .then(message => 
          message.delete(3000)
        )
       else if (args[1] === 'yellow') 
        addUserRole('Yellow', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'yellowgreen') 
        addUserRole('YellowGreen', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'green') 
        addUserRole('Green', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'greenblue') 
        addUserRole('GreenBlue', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'indigo') 
        addUserRole('Indigo', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'violet') 
        addUserRole('Violet', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'redviolet') 
        addUserRole('RedViolet', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'pastelred') 
        addUserRole('PastelRed', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'pastelorange') 
        addUserRole('PastelOrange', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'pastelyellow') 
        addUserRole('PastelYellow', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'pastelgreen') 
        addUserRole('PastelGreen', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'pastelblue') 
        addUserRole('PastelBlue', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'pastelindigo') 
        addUserRole('PastelIndigo', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'pastelviolet') 
        addUserRole('PastelViolet', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'darkred') 
        addUserRole('DarkRed', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'darkorange') 
        addUserRole('DarkOrange', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'darkyellow') 
        addUserRole('DarkYellow', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'darkgreen') 
        addUserRole('DarkGreen', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'darkblue') 
        addUserRole('DarkBlue', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'darkindigo') 
        addUserRole('DarkIndigo', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
       else if (args[1] === 'darkviolet') 
        addUserRole('DarkViolet', message);
        message.channel.send('Role successfully added!')
          .then(message => 
            message.delete(3000)
          )
    
  ;
)

function addUserRole(roleName, message) 
  var role = message.guild.roles.find(role => role.name === roleName);
  message.member.addRole(role.id)

  if (message.content.startsWith("!colorme")) 
    message.delete(3000);
    return;
  
;

bot.login(botSettings.token);

【问题讨论】:

该块语句应该做什么? 你到底是什么意思? 【参考方案1】:

你基本上是这样做的:

 if(message.startsWith("!colorme"))
   roles.some(/*..*/)
 
  //...
 

现在if 语句只适用于下面的语句,即roles.some(/*...*/) 部分,但/*..*/(块语句)将始终执行,无论条件是真还是假。要解决这个问题,只需在块内移动语句:

 if(message.startsWith("!colorme"))  // <----
   roles.some(/*..*/)
   //...
 

【讨论】:

我已经尝试了建议的内容,据我了解,它给出了意外的令牌错误

以上是关于为啥这个命令在不应该触发的时候触发?的主要内容,如果未能解决你的问题,请参考以下文章

条件断点在不应该触发时触发

为啥 Youtube 评论不会触发 DOM 突变?

sql问题,首先是快照,为啥不能恢复,然后是如何删除触发器(我指的是用命令),再就是游标和索引有何区

mysql怎么自动删除超过3天的数据

对于INSERT BULK 命令, 怎么建立insert触发器

如何仅在触发当前命令时使用命令?