Voice channels are not automatically deleted after a timeout in Discord.JS version 12

Currently, I am in the process of making some final adjustments to upgrade my Discord bot from version 11.4 to 12.

However, I have encountered an issue with private channels not being deleted after a specific period of time. I attempted to modify voiceChannel to voice.channel, but unfortunately, it did not resolve the problem.

Below is the code snippet:

        bot.on("voiceStateUpdate", oldMember => {
            deleteEmptyChannelAfterDelay(oldMember.voiceChannel);
        });
    
        function deleteEmptyChannelAfterDelay(voiceChannel, delayMS = 5000){
            if(!voiceChannel) return; 
            if(voiceChannel.members.first()) return;
            if(!voiceChannel.health) voiceChannel.health = 0;
            voiceChannel.health += 1;
            setTimeout(function(){  
                if(!voiceChannel) return;
                if(voiceChannel.members.first()) return;
                voiceChannel.health -= 1;
                if(voiceChannel.health > 0) return;
                if(!voiceChannel.name.includes('\'s Room')) return;
                voiceChannel.delete()   
                    .catch(error => console.log(error));
            }, delayMS);
        } 

I tried seeking assistance on Discord.js guides and forums, but unfortunately, I could not find any solution. Any help would be greatly appreciated! Thank you.

Answer №1

With the latest update to DiscordJS V12, there have been some changes to the voiceStateUpdate event. Instead of receiving parameters like oldMember and newMember, it now provides oldState and newState which are instances of VoiceState. To access the channel where the voiceStateUpdate occurred, you will now need to use VoiceState.channel instead of member.voiceChannel.

To ensure your code remains functional, make sure to update your event callback as follows:

bot.on("voiceStateUpdate", oldState => {
    deleteEmptyChannelAfterDelay(oldState.channel);
});

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

What is the best way to retrieve the response from an Observable/http/async call in Angular?

My service returns an observable that makes an http request to my server and receives data. However, I am consistently getting undefined when trying to use this data. What could be causing this issue? Service: @Injectable() export class EventService { ...

Tips for keeping the mobile menu active and responsive

Struggling to implement this menu on my website, I encountered a problem: how do I keep the menu active? I envision the menu bar changing when users navigate to the Home page, appearing like this: https://i.sstatic.net/z2fbX.png I attempted to use the cl ...

Another choice for object-fit:contain workaround for IE

Is there an alternative option to object-fit: contain that is compatible with Internet Explorer? Below is a snippet of my code: .cbs-Item img { height: 132px ! important; width: 132px ! important; object-fit: contain; <div class="cbs-Ite ...

Modifying CSS style according to the contents of an HTML element

Creating a room allocation page with multiple panel boxes using Bootstrap. Each box represents a bed - highlighted in red if occupied and green if vacant. Clicking on a green panel redirects to the room allocation page, while clicking on a red panel goes t ...

What is the process for creating an Account SAS token for Azure Storage?

My goal is to have access to all containers and blobs in storage. The Account SAS token will be generated server-side within my Node.js code, and the client will obtain it by calling the API I created. While Azure Shell allows manual creation of a SAS toke ...

There seems to be a runtime error in the Javascript code, as an

When attempting an AJAX call to a controller from my view, I encountered a JavaScript runtime error: function expected. Here is the script in question: <script type="text/javascript> var jsonData = []; var ms1 = $('#ms ...

How can I instruct Babel to compile code for ES2021 compatibility?

My babel.config.js file looks like this: module.exports = function (api) { api.cache(true); return { "presets": [ "@babel/preset-react", [ "@babel/preset-env", ...

Tips for How to Put a Delay on Ajax Requests and Display a Progress Bar During Loading

I am using checkboxes in the sidebar. When a user selects a checkbox from the sidebar, it displays the post normally. Is there a way to add a progress bar to delay the Ajax result? This is my Ajax code: <script> jQuery(document).ready(function($){ ...

Refresh the content inside a Div element automatically without having to reload the entire page, all without using any external

I need help updating the content of a specific div element. I'm currently working on a chat box feature and I want to dynamically refresh only the div without having to reload the entire page every time a new message is entered. The div contains embed ...

Is it necessary to uncheck the row checkbox when inputs are left empty after blurred?

Two issues have cropped up here. When clicking on an input within a row, the corresponding checkbox should be checked. Currently, only the first text input is able to check the checkbox due to the use of .prev(). Is there a different approach that can be t ...

Using Javascript to modify file permissions in Google Drive

I'm new to writing and seeking amazing solutions for all the issues I encounter. I have a website hosted on Google Drive, utilizing its SDK for Javascript. Everything functions exceptionally well, except for one problem. I need to adjust the permissi ...

How to add vertical bars within ng-repeat in AngularJS

I attempted to retrieve values from an array variable using ng-repeat within unordered lists. Although the values are displaying correctly and everything is functioning properly, I added vertical bars after each value to represent sub-categories on my page ...

Converting an Array with Key-Value pairs to a JSON object using JavaScript

After using JSON.stringify() on an array in JavaScript, the resulting data appears as follows: [ { "typeOfLoan":"Home" }, { "typeOfResidency":"Primary" }, { "downPayment":"5%" }, { "stage":"Just Looki ...

Issues with obtaining a reply

Encountering some issues while attempting to make an ajax call. The logic for this is stored in chat.js (included in the HTML head section) and it makes a request to getChatHistory.php chat.js: function retrieveChatData(user1, user2){ var response = &a ...

AngularJS and Handlebars (npm)

Is it possible for angularJS to function as a substitute for the "view engine" in nodeJS? I am seeking insights on the optimal method to use. (Do MEAN Stack developers utilize view engines? Or do they prefer using res.sendFile along with tools like ui-ro ...

Why is there an issue with the way I am defining this Javascript variable?

In my JavaScript file, milktruck.js, I have defined an object called TruckModel. My goal is to create an array of TruckModel objects because in my multiplayer game, I cannot predict how many players will enter or exit at any given time. The issue I am fa ...

Tips on transforming a JSON array object into a JSON array

**Background:** I have been utilizing lodash to eliminate the empty key from my JSON data. However, upon removal of the keys, it transforms my array into an object. For instance: { "projection": "Miller", "series": [ { "mapPolygons": { ...

Is there a way to retrieve a specific number of characters occurring after a specific word within a string?

Suppose I have a variable like this: var MV = "Cat234 Dog347 Fruit228 and I am very strong"; By using the match method, I can extract the numbers from the string. However, I want to specifically get the numbers that appear after the word Dog: var B = MV ...

How can I use HTML and Javascript to toggle the visibility of a 'select' element, like a dropdown menu

After adding a dropdown and label within a div, I expected them to hide and unhide when the div is manipulated with a function. However, it seems that this functionality is not working as expected. function ToggleDiv(DivID) { if (document.getElementBy ...

Exploring Angular's Http Get Service

After including an Angular service in my project, I encountered a problem where it neither gives an error nor gets called. I am unsure of the issue, especially since the second object is being called successfully. Below is the code for the controller: va ...