Member of channel in discord - javascript

I am developing discord bot via Discord.js and I need to get all the members that are in voice channel in this moment. How can I do that?
I tried
message.guild.channels.find(c => c.id === id here)
but it says that find is not a function.
Also, I tried
client.channels.get("name", "name of channel")
but it says that get is not a function.

DJS uses a cache, so the find function is client.channels.cache.find.

Related

Is it possible to find a channel that the bot have permission to type?

I want to let the bot auto find a channel where it can send messages, but it doesn't work.
Here is the code:
const channel = guild.channels.cache.find(c =>
c.type === 'text' &&
c.me.hasPermission('SEND_MESSAGES')
)
Your variable c is a GuildChannel and it doesn't have a me property.
If you want to check if your bot has permission to send messages in a channel, you can use the channel#permissionsFor() method. It accepts a GuildMemberResolvable or a RoleResolvable and returns Permissions. The returned value has a has() method that you can use to check if the permissions include the SEND_MESSAGES flag.
You can use guild.me as a GuildMemberResolvable for the permissionsFor() method. guild.me returns the bot as a GuildMember of this guild.
So, to find the first text channel where the bot can send a message, your code should look something like this:
// discord.js v12
const channel = guild.channels.cache.find(
(ch) =>
ch.type === 'text' &&
ch.permissionsFor(guild.me).has('SEND_MESSAGES')
);
Your hasPermission function is outdated with discord.js v13, you have now to use:
c.me.permissions.has("SEND_MESSAGES")
More informations here: https://discordjs.guide/popular-topics/permissions.html#channel-overwrites

Self Permission Check

How do I check if the bot (Self) has permissions to send messages and make it an if statement? Im using discord js and I've seen forums like member.roles.cache has, but none cover the bot itself, so how do I check that?
As Elitezen said, you can get GuildMember object using Guild#me but to check if your bot can send messages for example in guild where the command was executed you have to use this:
if(message.guild.me.permissions.has("SEND_MESSAGES")) { // check if bot has SEND_MESSAGES permission
// your code
}
You can use Guild#me to get the client's GuildMember object in that guild.
// guild = message.guild or any guild object
if (guild.me.roles.cache.has(...)) {
}
Update: in Discord.JS V14 you now have to use guild.members.me

Discord.js Bot Welcomes Member, Assign a Role and send them a DM

So When A New Member Joins The Guild [the discord server]. The Bot Should Send A Message In a certain Channel (ID = 766716351007686696), Send Them A Direct Message, And Then Add A Role (Human Bean). This Is The code I Have Now and it isn't working, error at the bottom
client.on('guildMemberAdd', member =>{
const channel = message.guild.channels.cache.find(c => c.id === "766716351007686696")
const channelwelcomeEmbed = new Discord.MessageEmbed()
.setColor('#ffd6d6')
.setTitle('Welcome!')
.setDescription(`${member} just joined the discord! Make sure to read #rules!`)
.setTimestamp();
channel.send(channelwelcomeEmbed);
const dmwelcomeEmbed = new Discord.MessageEmbed()
.setColor('#ffd6d6')
.setTitle('Welcome!')
.setDescription("For Help Using #Pro Bot#7903, Send The Command `!help` In Server")
.setTimestamp();
member.send(dmwelcomeEmbed);
let role6 = message.guild.roles.cache.find(role => role.name == "Human Bean"); //BASIC ROLE, EVERYONE GETS IT
if(!role6) return message.reply("Couldn't find that Role .")
member.roles.add(role6);
});
Error Message is;
const channel = message.guild.channels.cache.find(c => c.id === "766716351007686696")
^
ReferenceError: message is not defined
Your code looks fine, the problem is that the event isn't triggered. Thats because discord turned off "privileged intents" by default.
Some intents are defined as "Privileged" due to the sensitive nature of the data. Those intents are:
GUILD_PRESENCES
GUILD_MEMBERS
One effect of that is what you are experiencing, the not working guildMemberAdd event.
The good news is that you can fix this with one easy step. Simply enable Privileged Gateway Intents in the discord developer portal and it should work just fine.
If you want to read more about it
Discord.js Official Guide - Gateway Intents
Discord Developer Documentation - Gateway Intents
Gateway Update FAQ
Discord API Github - Issue 1363 - Priviledged Intents
Discord Blog - The Future of Bots on Discord
None of my discord.js guildmember events are emitting, my user caches are basically empty, and my functions are timing out?
Fix: const channel = member.guild.channels.cache.get('CHANNEL ID')
You need to use member instead of message. Because guildMemberAdd function using member.
client.on('guildMemberAdd', member => {

discord.js - How to get the name/id of the bot's voiceChannel

I have tested variable = client.voiceChannel but it always returns an undefined variable. So, there is a method to do that ?
If you want to know all the voice channels where the bot is you can use client.voiceConnections.
All active voice connections that have been established, mapped by guild ID
An example could be this:
client.voiceConnections.map(voiceConnection => console.log(voiceConnection));
If you have the Guild.id then you can pass it to get the exact channel id
bot.voiceConnections.get(GuildID).channel.id

Discord.js client.addMemberToRole not working

So, I'm programming a Discord bot, and one of the things I want it to do is to assign roles to members given certain conditions. After looking through the documentation, specifically here, I figured bot.addMemberToRole would be a good command to use. However, when I ran it, I got this error message:
TypeError: bot.addMemberToRole is not a function
I was understandably confused, as the documentation clearly says that this IS a function. I have tried doing bot.addMemberToRole(member, role);, addMemberToRole(member, role);, and several other iterations. This is my most recent attempt:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.addMemberToRole(member, role, function(err){
if(err){
console.log(err);
}
});
I have also done just this:
bot.addMemberToRole(member, role);
Both gave the same TypeError as above.
I have no idea why it doesn't work. I followed the documentation exactly, the member and role variables I pass into it are the proper type, and other Discord.js commands work just fine in my bot. Any help would be appreciated.
You're using an old version of the docs so that function doesn't exist anymore. They should really get rid of those. You're looking for GuildMember.addRole(Role or String).
To add a member to a role, you need a GuildMember and a Role object (or the name of the role). Assuming you have the User object and the Guild object (your bot has a list of the guilds/servers it's joined and most events will have the guild they're associated with), you can get the GuildMember by using Guild.fetchMember(User). From there, you can add the role on the GuildMember using either the string or object based version of addRole.
Here's an example of how to do it upon receiving a message from a user which is very easy since the message has a GuildMember associated with it.
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('message', (message) => {
const guildMember = message.member;
guildMember.addRole('bot-added-role');
});

Categories