简体   繁体   中英

how can i make this command loop? [Discord.js]

I have this command but I don't have an idea how to make it loop until I turn off the bot. I will be really grateful if you can help me. I have to mention that I need the message to keep changing.

the code I want to loop is this:

module.exports = {
  name: "spam",
  description: "face spam",
  execute(message, args) {
    let messages = [
      "Ghg&gjhgTF55$FGjhuc$%",
      "ggt6rjhf*r8rfhgo(gFT&",
      "F^v 6r796GIGYv6rcKUYg",
      "g7v%C75C&TOHbgv*C^ruk",
      "hgC^%4x7vV(P&v0b87&^C",
      "hguiv^&vr675rXC&5vvv6",
      "78v tv8976r9uyv ci y6",
      "b87%687*V8V*v7UYVOUYV",
      "bt8787V5V9&^v*&5Xdiov",
      " *^&t*^&V&cr(R5kjYGBk",
    ];
    const message = messages[Math.floor(Math.random() * messages.length)];
    message.channel.send(message);
  },
};

You can use setInterval

const timeInMs = 1000;
let msg; // not const
setInterval(function(){
 // The code you want to repeat
 message.channel.send(msg);
 msg= messages[Math.floor(Math.random() * (messages.length))]
}, timeInMs);

Solution

 let bot = null; let messages = [ "Ghg&gjhgTF55$FGjhuc$%", "ggt6rjhf*r8rfhgo(gFT&", "F^v 6r796GIGYv6rcKUYg", "g7v%C75C&TOHbgv*C^ruk", "hgC^%4x7vV(P&v0b87&^C", "hguiv^&vr675rXC&5vvv6", "78v tv8976r9uyv ci y6", "b87%687*V8V*v7UYVOUYV", "bt8787V5V9&^v*&5Xdiov", " *^&t*^&V&cr(R5kjYGBk", ]; function start() { bot = setInterval(function () { const msg = messages[Math.floor(Math.random() * messages.length)]; console.log(msg); }, 1000); } function stop() { bot && clearInterval(bot); }
 <button onclick="start()" >Start</button> <button onclick="stop()" >Stop</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM