I want to check if certain user is a server member using their ID, but when I try to check it - my bot says user is not a server member even when he clearly is!
Here is what I tried:
if(message.guild.members.cache.get('ID')) {
// ...code
}
What can I do to check it?
The member isn't cached. Always fetch a user and don't rely on the cache.
const targetMember = await message.guild.members.fetch('ID');
if (targetMember) {
// Member is in the guild
} else {
// Member is not in the guild
}
Related
Discord bots can have a Verified check mark. Is there a way to check if a Discord bot is verified using Discord.js?
I'm doing this mainly to avoid raids or nuke bots
client.on('guildMemberAdd', member => {
if (member.user.bot) {
// if is verified:
// member.roles.add("<BOT_ROLE_ID>")
// else:
// member.ban(...)
}
})
And if it can't be done using Discord.js, then is there another way using the member.user.id property or something else?
Update: user flags may be the answer
You can check the User#flags property as you mentioned in your update. The available flags can be found in the documentation. You'll need to check if the user has the VERIFIED_BOT flag.
if (member.user.flags.has('VERIFIED_BOT')) {...}
Or you can import UserFlags enums and use UserFlags.VerifiedBot:
const { UserFlags } = require('discord.js')
// ...
if (member.user.flags.has(UserFlags.VerifiedBot)) {...}
try doing:
client.on('guildMemberAdd', member => {
if (member.user.bot) {
if is member.user.verified:
member.roles.add("<BOT_ROLE_ID>")
else:
member.ban(...)
}
})
I am not sure but this should work.
For more you can visit: https://discord.js.org/#/docs/discord.js/main/class/ClientUser?scrollTo=verified
I'm trying to check if the user has a role on a specific server regardless on which server they use the bot.
For example, if I have 2 servers, server A and server B, I want to check if the user has the role "Beginner" on server A, even if I use the command in server B.
I couldn't find a way to do this on the internet, message.member.roles seems to only return the roles of the server the command was written in.
You can do it like this:
function hasRole(guildId, roleId) {
// Get second guild
const guild = client.guilds.cache.get(guildId);
// Get member in that guild, by ID
const member = guild.members.cache.get(message.author.id);
// If member is in that guild,
if (member) {
// return whether they have this role
return member.roles.cache.has(roleId);
}
}
Of course the bot has to be in both servers.
A solution is to find the server then find the member from inside the server.
To find the guild you will use the guild.fetch(...) method and from there you then use the guildMember.fetch(...) function to find the member.
Example:
client.on("messageCreate", function(message) {
// Find the guild
let guild = client.guilds.fetch("guild-id");
// Find the member in the guild
let member = guild.members.fetch(message.author.id);
if (member)
message.reply("You are in the server " + guild.name);
});
I'm trying to DM all server members by a bot trough a command and it only DMs 4 people that are Administrators
message.guild.members.cache.forEach(member => { // Looping through each member of the guild.
// Trying to send a message to the member.
// This method might fail because of the member's privacy settings, so we're using .catch
member.send(`hi`)
.catch(() => (`Couldn't DM member ${member.user.tag}`));
message.channel.send(`Success`)
.catch(console.error);
});
This operation can be extremely time-consuming. #SuleymanCelik's answer was partially correct because not every user is stored in the bot member cache. To get every single user in the server, you need to make a fetch() call for all the users like this.
guild.members
.fetch()
.then(members => members.forEach(member => {
member
.send("Hello!")
.catch(() => {
console.error(`Failed to send ${member.user.tag} a message`)
})
}))
you can send a message to all users whose privacy settings are not turned on in this way
message.guild.members.cache.forEach(member => { // Looping through each member of the guild.
// Trying to send a message to the member.
// This method might fail because of the member's privacy settings, so we're using .catch
member.send("Hello, this is my message!").catch(e => console.error(`Couldn't DM member ${member.user.tag}`));
});
Hey So I Have a Bot That Requires Two Bots to Work with each other I need to know How to Check if a bot is in the guild if not then to Tell the user to invite it.
Heres My Current Code:
if(message.guild.users.id === "ID") {
//If its in the Server
} else {
//If it isnt
}
I recommend fetching all members of a guild to ensure all members are cached. This will return a promise, resolve the promise to get the Collection of the Guild Members. Use has() with the bot's id, This will return true if the id was found in the Collection, false otherwise.
message.guild.members.fetch().then(memberList => {
if (memberList.has("ID")) {
// If it's in the server
} else {
// if it's not
}
}).catch(console.error);
You can also try to fetch the bot directly and check if a GuildMember object returned.
message.guild.members.fetch("ID")
.then(botObject => {
// If found
})
.catch(err => {
// If not found
});
You will need to enable the GuildMember's Intent for fetching to work.
How to make a bot be able to send a private message to a specific person? (using user ID)
no, message.author is not what I need!
I'm using discord.js and node.js
client.users.fetch('123456789').then((user) => {
user.send("My Message");
});
Dont forget replace "123456789" with the user id :D
Make sure this code is inside of an async function
// Getting the user object
const user = await <Client>.users.fetch('userID');
if (user) user.send('message').catch(error => {
// code if error occurs
});
This can be done with the User.send() method.
Here's an example of how it can be done by fetching the user
Make sure this is an async function, or the await will return an error.
const user = await <client>.users.fetch("USER ID"); // Replace <client> with your client variable (usually client or bot)
user.send("Message to send");