简体   繁体   English

我怎样才能使这个命令循环? [不和谐.js]

[英]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您可以使用 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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