简体   繁体   English

如何停止执行功能?

[英]How can I stop a function from executing?

So I basically want to create a countdown for my discord ticket bot. 所以我基本上想为我的不和谐票机器人创建一个倒计时。 If someone types in -close[...] the channel will be deleted after 10 seconds. 如果有人输入-close [...],该频道将在10秒后删除。 But if the person who did this command types something in the channel the countdown will stop and the channel won't be deleted. 但是,如果执行此命令的人员在频道中键入内容,则倒数计时将停止并且该频道不会被删除。

That works fine so far. 到目前为止,一切正常。 But if I abort the countdown on every other message that I send to the channel the embed will be sent where it says "Countdown stopped" also if I type -close [...] again this message pops up but the channel will still be deleted after 10 seconds. 但是,如果我中止发送给频道的所有其他消息的倒数,则嵌入将被发送到显示“倒数停止”的位置,如果再次键入-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;
  }

I got it to work now! 我现在开始工作了! I defined a new variable called timer_running which would be set to true when the timer starts and to false when it stops. 我定义了一个新的名为timer_running变量,它将在计时器启动时设置为true ,在计时器停止时设置为false That way I got it to work now. 这样我就可以正常工作了。

  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"
          }
        }});
      }
    });
  }

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

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