【问题标题】:Stop loop in discord.jsdiscord.js 中的停止循环
【发布时间】:2019-07-06 03:06:21
【问题描述】:

我需要帮助来停止 discord.js 中的循环。我不知道该怎么做才能停止循环。

let sec = 5;
let timer = setInterval(function() {
  if (command = "8") message.channel.send("ddd");
}, sec * 1000);

if (command = "stoploop") clearInterval(timer);

我的问题是 clearInterval 没有定义。

【问题讨论】:

  • 能否将您遇到的错误添加到问题中

标签: discord.js


【解决方案1】:

您不应该使用setInterval,因为 discord.js 是一个事件驱动库。你应该像 discord.js 的开发者那样编写代码。改用这个:

this.client.on('ready', () => {
    //Runs when discord bot is fully loaded.
    this.client.on('message', async (msg) => {
        //Runs when a message was sent to the channel.
        let parseTokens = yourCommandParser(msg.content); //Parses your command.
        executeYourCommand(parseTokens);
    });
});

我强烈建议你学习如何编写干净和更好的代码,然后学习编译和语言解析,然后再用命令制作一个不和谐的机器人。

希望这会有所帮助! (^ w ^)

事后思考: setInterval 也是不安全的,不要在任何需要用户输入的地方使用它。通过setInterval 可以进行一些漏洞利用,就像您入侵使用eval 执行代码的网站一样。永远不要在不和谐的机器人中使用 setInterval / setTimeouteval。欲了解更多信息,请搜索server side injection。 下面是一些例子:https://stackoverflow.com/a/68870077/15017928

【讨论】:

    【解决方案2】:

    我将间隔存储在客户端:

    let sec = 5;
    message.client.variables.timer = setInterval(function() {
      if (command = "8") message.channel.send("ddd");
    }, sec * 1000);
    
    //When you want to stop the loop
    clearInterval(message.client.variables.timer);
    

    【讨论】:

      【解决方案3】:
      let sec = 5;
      let timer = setInterval(function() {
        if (command = "8") message.channel.send("ddd");
      }, sec * 1000);
      
      if (command = "stoploop") clearTimeout(timer);
      

      您收到错误的原因是因为 clearInterval() 不是 JavaScript 中的函数。我相信您正在寻找的功能是 clearTimeout()。

      【讨论】:

        【解决方案4】:
        client.on ('message', async message => {
          var prefix = "=",
              command = message.content.slice (prefix.length).split (" ")[0],
              sec = 5;
          switch (command) {
            case "startloop":
              if (message.channel.loop) return message.channel.send ('loop is already started');
              else message.channel.loop = setInterval (() => message.channel.send ('ddd'), sec * 1000);
              break;
            case "stoploop":
              if (!message.channel.loop) return message.channel.send ('no loop to stop lol');
              else {
                clearInterval (message.channel.loop);
                message.channel.loop = false;
              }
              break;
          }
        });
        

        【讨论】:

        • 欢迎来到 SO。您能否添加文字来解释此代码如何回答问题。
        【解决方案5】:

        if (condition) 语句中,您不能使用=。这用于为变量、属性或其他任何内容分配新值。

        等于选择器是:

        • == 等于。
        • === 表示相同类型的等于。
        1 == '1' // returns true
        1 === '1' // returns false
        

        【讨论】:

        • 很好的解释性答案。
        猜你喜欢
        • 2019-12-24
        • 2019-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-11
        • 2017-04-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多