Reputation: 29
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. 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.
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;
}
Upvotes: 0
Views: 120
Reputation: 29
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. 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"
}
}});
}
});
}
Upvotes: 2