Variable for toggle function and enabling/disablin

2019-07-18 03:35发布

问题:

As in my previous question I was making a troll function now I'm trying to figure out how to make it toggle to make it work so my friend doesn't have to ban it every now and then.

The toggle command works but its not actually internally working.

NOTE: I have two discord accounts so I could test it on the other one.

The part where it uses the toggle is in the bottom

const Discord = require("discord.js");
const client = new Discord.Client;
var enabled = true
client.on("message", message => {
  if(message.author.bot) return;
  
  
  let messageArray = message.content.split(" ")
  let command = messageArray[0]
  let args = messageArray.slice(1)
  if(!command.startsWith(prefix)) return;
  
  if (command === `${prefix}cleanup`) {
    if (message.author.id != 234430480672358400) {
      message.delete()
      let embed = new Discord.RichEmbed()
        .setColor("#e20909")
        .setImage("https://cdn.discordapp.com/attachments/358640529376018432/451111825266835476/unknown.png")
        .setTitle(`${message.author.tag}, wow ur mom bad for you trying to use this unauthorized >:(`);
      message.channel.sendEmbed(embed) 
        .then(newMessage => newMessage.delete(5000));
    return};
    message.delete();
    message.channel.send("https://cdn.discordapp.com/attachments/330441704073330688/453693702687162369/yeet.png");
  };
  
  if (command===`${prefix}toggle_win`) {
    if (message.author.id == 234430480672358400) {
      if(enabled === true) {
        enabled = false
        let embed = new Discord.RichEmbed()
          .setColor("#18dd50")
         .setImage("https://cdn.discordapp.com/attachments/358640529376018432/451109668002070533/Capturedab.PNG")
          .setTitle(`${message.author.tag} success, classifier module is disabled until you repeat the command!`);
          message.channel.sendEmbed(embed) 
              
      }else{
        enabled = true
        let embed = new Discord.RichEmbed()
          .setColor("#18dd50")
         .setImage("https://cdn.discordapp.com/attachments/358640529376018432/451109668002070533/Capturedab.PNG")
          .setTitle(`${message.author.tag} success, classifier module is enabled until you repeat the command!`);
          message.channel.sendEmbed(embed) 
              
      }
      
      
    }else{
    let embed = new Discord.RichEmbed()
    .setColor("#e20909")
    .setImage("https://cdn.discordapp.com/attachments/358640529376018432/451101447405174785/Capture.PNG")
    .setTitle(`${message.author.tag}, ur iq is now -666 try again to have -1337`);
    message.channel.sendEmbed(embed) 
      .then(newMessage => newMessage.delete(5000));
    }
  }
  
  if (message.channel.id != 425328056777834506) return;
  if (enabled === true && message.author.id != 234430480672358400 && Math.floor(Math.random() * Math.floor(4))=== 3 && message.attachments.size > 0) {
    message.channel.send("Detected carried win, will now initiate\nhttps://cdn.discordapp.com/attachments/330441704073330688/453693702687162369/yeet.png");
  } else if (enabled === true && message.content.search("!cleanup")===-1 && message.author.id != 234430480672358400 && message.attachments.size === 0) {
    message.channel.send("send me a poto of ur win :thonk:");
  };
});

回答1:

The toggle command actually works, try printing the variable out somewhere.

What might actually be the problem is your code structure, if(!command.startsWith(prefix)) return; near the start of the handler will exit the function if the message does not start with a prefix.
Which means that, this code you have...

  if (message.channel.id != 425328056777834506) return;
  if (enabled === true && message.author.id != 234430480672358400 && Math.floor(Math.random() * Math.floor(4))=== 3 && message.attachments.size > 0) {
    message.channel.send("Detected carried win, will now initiate\nhttps://cdn.discordapp.com/attachments/330441704073330688/453693702687162369/yeet.png");
  } else if (enabled === true && message.content.search("!cleanup")===-1 && message.author.id != 234430480672358400 && message.attachments.size === 0) {
    message.channel.send("send me a poto of ur win :thonk:");
  };

Will not be executed unless the message starts with the required prefix.

You could try using a else state instead, which makes it...

  if(!command.startsWith(prefix)) 
  {
    //More stuff or...
    return;
  } else {
    if (message.channel.id != 425328056777834506) return;
    if (enabled === true && message.author.id != 234430480672358400 && Math.floor(Math.random() * Math.floor(4))=== 3 && message.attachments.size > 0) {
      message.channel.send("Detected carried win, will now initiate\nhttps://cdn.discordapp.com/attachments/330441704073330688/453693702687162369/yeet.png");
    } else if (enabled === true && message.content.search("!cleanup")===-1 && message.author.id != 234430480672358400 && message.attachments.size === 0) {
      message.channel.send("send me a poto of ur win :thonk:");
    }
  }