简体   繁体   中英

discord.js List channels with a certain role

Trying to find a way to add into my bot, if someone types for example +info it will list all the channels on the server with a certain role. So if the role is VIP, doing +info would list every channel on the server which has the role VIP.

Have been trying to read the doc's but can't find anything that shows what I want. I'm guessing it would have to use a different module. Can any one help with this? Cheers

You could loop through all the channels in the guild and check whether the User has the VIEW_CHANNEL permission, to check if they have access to the channel. Perhaps something like this:

const listedChannels = []; 
message.guild.channels.forEach(channel => { 
    if(channel.permissionsFor(message.author).has('VIEW_CHANNEL')) listedChannels.push(channel.name);
});
message.channel.send(`You have access to: ${listedChannels.join(',')}`);

What this does, is it iterates through the collection of channels in the guild and if the author of the message has permissions to actually view the channel, then it will add that channel name to the array. Then, it sends a message saying a list of channels the user has access to.

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