简体   繁体   中英

discord bot - delete all channels in a discord server using discord.js v13

I need to delete all the channels with a certain name in my discord server using discord js v13.

For example if there are 5 channels with the name "general" then I want to delete them and make sure that the rest of the channels don't get deleted. All the posts that I found were for v12.

I'm pretty new to discord.js, so I would appreciate some help!

This is my code:

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');

});

// Login to Discord with your client's token
client.login(token);


Try this

client.on("message", message => {
    if(message.content === "!nuke"){
        message.guild.channels.forEach(channel => channel.delete())
    }
});

Using

client.once('ready', () => {
    console.log('Ready!');
    let guild = client.guilds.cache.get('YOUR_GUILD_ID');
    guild.channels.cache.forEach((channel) => { // check each channel in guild your command was executed in
        if(channel.name == "general") channel.delete() // delete channel if its name is "general"
    })
})

should help!

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