如何停止执行功能?

时间:2019-07-19 18:01:14

标签: javascript node.js discord.js

所以我基本上想为我的不和谐票机器人创建一个倒计时。如果有人输入-close [...],该频道将在10秒后删除。但是,如果执行此命令的人在频道中键入内容,则倒计时将停止,并且该频道不会被删除。

到目前为止,效果很好。但是,如果我中止发送给频道的所有其他消息的倒数,则嵌入将被发送到显示“倒数停止”的位置,如果再次键入-close [...],也会弹出此消息,但该信道仍会10秒后删除。

function closeTicket (_ticketid, channel, deleter, reason) {
    var timer = setTimeout(function() {
      channel.delete();
    }, 10000);
    channel.send({embed: {
      color: 3447003,
      author: {
        name: client.user.username,
        icon_url: client.user.avatarURL
      },
      title: ``,
      description: "This ticket will close in 10 seconds. If this was a mistake type anything to stop the timer.",
      fields: [{
          name: "Thank you!",
          value: "Thank you for using our ticket system! Good luck and have fun playing on our servers."
        },
      ],
      timestamp: new Date(),
      footer: {
        icon_url: client.user.avatarURL,
        text: "Support Ticket System © H4rry#6701"
      }
    }
    });
    logTicketClosed(_ticketid, deleter, reason);
    client.on('message', message => {
      if(message.channel === channel && message.author === deleter && timer != null) {
        clearTimeout(timer);
        timer = null;
        message.channel.send({embed: {
          color: 3447003,
          author: {
            name: client.user.username,
            icon_url: client.user.avatarURL
          },
          title: `Timer Stopped`,
          description: "The timer has been stopped, the ticket will remain open.",
          timestamp: new Date(),
          footer: {
            icon_url: client.user.avatarURL,
            text: "Support Ticket System © H4rry#6701"
          }
        }});
      }
    });
    return 0;
  }

1 个答案:

答案 0 :(得分:1)

我现在开始工作了!我定义了一个名为timer_running的新变量,它将在计时器启动时设置为true,在计时器停止时设置为false。这样我就可以正常工作了。

  function closeTicket (_ticketid, channel, deleter, reason) {
    var timer_running = false;
    var timer = setTimeout(function() {
      channel.delete();
    }, 10000);
    timer_running = true;
    channel.send({embed: {
      color: 3447003,
      author: {
        name: client.user.username,
        icon_url: client.user.avatarURL
      },
      title: ``,
      description: "This ticket will close in 10 seconds. If this was a mistake type anything to stop the timer.",
      fields: [{
          name: "Thank you!",
          value: "Thank you for using our ticket system! Good luck and have fun playing on our servers."
        },
      ],
      timestamp: new Date(),
      footer: {
        icon_url: client.user.avatarURL,
        text: "Support Ticket System © H4rry#6701"
      }
    }
    });
    logTicketClosed(_ticketid, deleter, reason);
    client.on('message', message => {
      if(message.channel === channel && message.author === deleter && timer_running === true) {
        clearTimeout(timer);
        timer_running = false;
        message.channel.send({embed: {
          color: 3447003,
          author: {
            name: client.user.username,
            icon_url: client.user.avatarURL
          },
          title: `Timer Stopped`,
          description: "The timer has been stopped, the ticket will remain open.",
          timestamp: new Date(),
          footer: {
            icon_url: client.user.avatarURL,
            text: "Support Ticket System © H4rry#6701"
          }
        }});
      }
    });
  }