简体   繁体   中英

Discord.js Sorting through all text channels on a server

module.exports = {
 name: 'cleartextchats',
 description:
  'clears desired amount of text chats created by the bot. usage: cleartextchats <amount>',
 execute(message, args) {
  var channelAmount = args[0];
  console.log('executing cleartextchats');
  for (var i = 0; i < channelAmount; i++) {
   var textChat = message.guild.channels.cache
    .filter((ch) => ch.type === 'text')
    .find(
     (targetch) => targetch.topic == 'volturnbot' && targetch.deleted == false
    );
   message.channel.send(`deleting ` + textChat.name);
   textChat.delete();
  }
 },
};

The for loop does not advance past the first deleted channel. I presume this is because the cache does not update unless I use a different/new message but the command is supposed to run from a message. I cant think of a way to implement this that would work for any amount of channels.

Currently whenever my bot creates text channels it updates the topic to include "volturnbot" so that it can delete its own channels without a category.

I presume this is because the cache does not update unless I use a different/new message but the command is supposed to run from a message.

I believe that assumption is correct. It is probably because the channel.deleted property is still false even after you delete the channel in the for loop, since there is probably a slight delay associated with channel deletion. There are two solutions I can think of:

A) Instead of using a for loop, you could perhaps use setInterval as a loop with a delay of 1000 milliseconds or so (might have to increase or decrease that depending on what works). You would need to create two new variables: one to contain the setInterval loop, and another to keep track of how many iterations of the loop have occurred. When the iterations variable is equal to the channelAmount variable, that would be when you use return clearInterval(intervalVariable) where intervalVariable is the variable containing the loop. (This is assuming a timeout gives the cache enough time to update after the channel is deleted).

B) Loop through the cached text channels instead of fetching one channel from the cache each time. Example:

var channelAmount = Number(args[0]);
console.log("executing cleartextchats")

var channels = message.guild.channels.cache.filter(ch => ch.deleted == false && ch.type === 'text' && ch.topic && ch.topic.match("volturnbot"));
var iterations = 0;

for (var textChat of channels) {
    iterations++;
    message.channel.send(`deleting ` + textChat.name)
    textChat.delete()

    if (iterations == channelAmount) break;
}

Honestly I prefer option B, and I am positive option B would work much better in terms of what you want to achieve here. You also mentioned that the channel topic "includes" the phrase "volturnbot", I'm not sure if that indicates the description could be more than exactly "volturnbot" so I made option B use topic.match("volturnbot") for that reason.

Because guild.channels.cache returns a collection, you have to use .array() also, instead of finding a single channel and then searching through the same cache, put all channels in a single array and loop through that.

    var channelAmount = Number(args[0]);
    var textChats = message.guild.channels.cache
        .filter((ch) => ch.type === 'text' && ch.topic === 'volturnbot')
        .array()
        
    for (var i = 0; i <= channelAmount; i++) {
        message.channel.send("deleting " + textChats[i].name)
        //textChats[i].delete()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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