Create a Discord bot that will remain in a channel for a specific amount of time before automatically leaving, but will continue to

I'm currently working on a music bot that I want to automatically leave after a certain amount of time, unless 24/7 mode is enabled and the queue ends. The problem I'm facing is that when new audio starts playing, the bot still leaves after the set time.

 .on ("queueEnd", (player) => setTimeout(() => {

        // Don't leave if 24/7 mode is active
        let QueueEmbed = new MessageEmbed()
        .setAuthor("The queue has ended", this.botconfig.IconURL)
        .setColor(this.botconfig.EmbedColor)
        .setTimestamp();

          client.channels.cache.get(player.textChannel).send( { embeds: [ QueueEmbed ] } );
  
        
          if (!this.config["24/7"]) player.destroy();
      }, 30000)); 

If 24/7 mode is set to false, the bot remains for the specified time. However, I need it to cancel the destroy action if new audio starts playing again, and I'm struggling to figure out how to achieve that.

Answer №1

If you want to stop the code within a setTimeout() function from executing, one way is to save the output of the setTimeout() in a variable and use clearTimeout() to cancel the timeout whenever necessary:

//Save the timeout in a variable
const myTimeout = setTimeout(() => { 
    console.log("Goodbye, World!") 
}, 5000)

//Use this to cancel the timeout
clearTimeout(myTimeout)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a TypeError in React: "Object(...) is not a function" when using Material UI

Recently, I've been working on incorporating Material UI into my react application. However, despite trying both Material UI versions 0.20.1 and 0.20.0, I keep encountering this error message: TypeError: Object(...) is not a function The error seems ...

JavaScript HTML Object Manipulation and Templating System

I am searching for a JavaScript library that is capable of performing the following: var items = [1, 2]; var html = div( ul({ id: "some-id", class: "some-class" })(items.each(function(item) { return li(item); })); html == ...

Tips for Concealing All Characters Except One in a Password Field

I have been researching this particular question extensively, but I have yet to come across a definitive solution. What I am looking to achieve is displaying a user's password on their account page with all characters hidden except for the first lette ...

Discover the HTML of a different website using Javascript

I'm currently developing a basic webcrawler that will display all the links found on a specified website. Here's what I envision my program doing: - enter a URL: http://www.example.com/ - the program retrieves the HTML source and locates all th ...

Image Animation Using a Rotational Control (HTML/JavaScript/...)

I am interested in achieving a specific effect with a series of images that transition from dark to light. My idea is to have a dial or slider underneath or over the frame so that when it is moved to the left, the black image is displayed, and when moved t ...

Filling a ButtonGroup in React with Buttons from an array

When trying to populate a ButtonGroup with Buttons using array.map(), I encounter an issue where the buttons do not appear. Interestingly, I used the same method successfully to populate a DropdownButton with MenuItems. Here is the functional DropdownButt ...

How do I initiate PUT and DELETE requests from my HTML code?

I am currently in the process of developing a web application that will manage items within a list. Previously, I used buttons with event listeners and JavaScript functions to handle editing and deleting items. However, I am now transitioning towards build ...

The XHR XML responseType in its unadulter

Is it feasible to adjust XHR settings in order to receive an HTML document? xhr.responseType = "document"; Upon receiving the response, xhr.responseXML contains an HTML document parsed against the HTML namespace URI. To verify this, you can use: xhr.res ...

Searching for the regulations on type-casting in JavaScript

Seeking a definitive set of guidelines regarding automatic typecasting and when it occurs. I am in the process of developing some rules for new developers, an example being: 90 > '100' // comparison as integers '90' > 100 // ...

Error: Token ID X was not found in the Stripe API when creating a charge

Just starting out with node/express and I'm experimenting with setting up a basic dummy charge using Stripe 3.3.2 and checkout.js. After submitting the checkout form, the token successfully reaches the backend; however, when attempting to create a cha ...

Exploring alternatives to setTimeOut() for precise timing of Javascript events, especially when incorporating third party events

Here is the HTML element stack that I am working with: <div class="btn-group btnSortAssType" data-toggle="buttons"> <label class="btn ink-reaction btn-primary active"> <input type="checkbox" value="m">Model </label> ...

Where is the best location for my code in AngularJS?

Currently, I am working on a galaxy simulation project using AngularJS and Threejs. My intention behind incorporating Angular JS is to grasp its functionality better and implement it in future projects as well. However, I find myself struggling with deter ...

How do load functions/methods execute in a specific order?

$(document).ready(), $(window).load(function(), <body onload="load()">, data-ng-init="init()", $(function(){...}); It is a challenge to determine the sequence in which different "load" functions are executed across various browsers. There are approx ...

What is the best method for managing the order in which scripts are executed?

I am currently developing a small browser game using p5.js. The structure of my HTML is as follows: <!-- ... --> <body> <script type="text/javascript" src="js/vendor/p5.min.js"></script> <script type=& ...

Error message in logs: "Module not found in Firebase Cloud Functions"

I am currently exploring how to Serve Dynamic Content with Cloud Functions using NextJS. Everything is functioning properly locally when I execute firebase serve. However, upon deploying my project with firebase deploy and attempting to run my function, ...

JQuery integration resulting in disappearance of Input-group-btn element

Allow me to explain my current project. I am in the process of developing a Modal that enables users to input a password There will be an option to toggle between showing or hiding the text in the password field using a button positioned on the right sid ...

What is the process for accessing the content of a URI on the console page?

let request = new XMLHttpRequest(); request.open('GET', 'https://learnwebcode.github.io/json-example/animals-1.json'); request.onload = function() { console.log(request.responseText); }; request.send(); I am continuously seeing t ...

The switch statement within Angular returns null when using getElementById()

In my single page application, I am using ng-switch to switch between different 'pages'. One of these pages contains a modal that is opened using the following function: var modal = document.getElementById('myModal'); vm.openModal = fu ...

Preserve information on page reload in AngularJS

I am currently working on an AngularJS application with Spring REST as the backend. My experience with AngularJS is quite new. Within my UI page, I have a table displaying a list of objects with an edit button for each record. Clicking the edit button ope ...

The second jQueryUI datepicker instance flickers and vanishes when the show() function is triggered

I currently have two jQueryUI datepickers integrated into my webpage. The initialization code for both is as follows: jQuery("#departureDate").datepicker({ beforeShow: function() { getDatesForCalendar("outbound"); }, numberOfMonths: 3 ...