DiscordJS - Fetch Multiple Users at Once - discord

Instead if fetching every user by itself, I am trying to fetch an array of users at once.
This is the code I am trying to use:
var idsArr= ['682999909523259483','234269235071811584','503303866016727050'];
const membersArr = await interaction.guild.members.fetch({
idsArr,
});
console.log(membersArr);
Even though I only give it an array of 3 IDs, it logs every member of the server.
Any suggestion on how to fix that?

I just searched through official discord.js examples, and found this:
// Fetch by an array of users including their presences
guild.members.fetch({ user: ['66564597481480192', '191615925336670208'], withPresences: true })
.then(console.log)
.catch(console.error);
Maybe you should do this instead:
var idsArr= ['682999909523259483','234269235071811584','503303866016727050'];
const membersArr = await interaction.guild.members.fetch({
user: idsArr,
});
console.log(membersArr);
(Notice field name user instead of idsArr)

Related

How do I fetch all reactions from a specific message? [Discord.js]

So, I want to make a command for my bot which can fetch all reactions from a given message ID and store these three categories into arrays, for each reaction:
the name of the reaction (eg. :smile:)
the users who reacted (eg. RandomUser#4654, ILikePotatoes#1639)
and how many people reacted with this emoji (eg. Count: 2)
I've tried using the ReactionCollector, but it doesn't work for reactions added prior to the event being called. I've also tried using an external module called discord-fetch-all but all it does is either sending Promise { <Pending> } or undefined when I use .then().
PS: I've already set up a command handler that takes a message ID for argument.
Thank you in advance for helping.
You can get the specific message you want to be check for reactions by using:
const messageReacted = client.channels.cache.get('channelId').messages.fetch('messageId')
Then, you can go through each reaction in the message's cached reactions by using forEach
Then, in the .forEach, you can get the emoji name by using reaction._emoji.name, the number of users who used this reaction by reaction.count and all the users who reacted on the message by fetching them: await reaction.users.fetch().
Your final code might look something like:
const messageReacted = await client.channels.cache
.get('channelId')
.messages.fetch("messageId");
messageReacted.reactions.cache.forEach(async(reaction) => {
const emojiName = reaction._emoji.name
const emojiCount = reaction.count
const reactionUsers = await reaction.users.fetch();
});

Discord.js V13 dont know how to check if a user ID is in the current guild

I have been trying to check if a certain user ID exists in the current guild,
I have been looking at countless stack overflow questions and documentations hoping i could find the answer but nothing I try actually works.
i use Discord.js V13 with the latest node.js version
const server = client.guilds.fetch( message.guild.id );
if ( !server.member(args[0]) ) return commandFailed('a user with this ID does not exist');
// args[0] contains the userID
could someone please help me with this.
First: fetch returns a promise, you must handle that promise. However, you don't need to fetch the server since the server is already accessible through message.guild.
Second: Guild#member() is deprecated, try fetching the member directly and checking if a member returned.
I'll be using Async/Await for this example, make sure you're inside an Async function.
// const server = await client.guilds.fetch(message.guild.id); is not needed
const server = message.guild;
const member = await server.members.fetch(args[0]);
if (!member) return commandFailed('a user with this ID does not exist');
Take a look at this thread, which explains exactly what you want:
let guild = client.guilds.get('guild ID here'),
USER_ID = '123123123';
if (guild.member(USER_ID)) {
// there is a GuildMember with that ID
}

I want a discord bot send a dm to somebody by their id

if (message.content === "!test"){
client.users.cache.get('id').send('message');
console.log("message sent")
}
This method doesn't work and I wasn't able to find any other methods that worked
Is there a spelling mistake or this method is outdated?
I've actually encountered this error myself once. There are two problems you might be facing here:
The user isn't cached by discord.js yet
There isn't a DMChannel open for that user yet
To solve the first one, you have to fetch the user before doing anything with it. Remember this is a Promise, so you'll have to either await for it to complete or use .then(...).
const user = await client.users.fetch('id')
Once you've fetched your user by their ID, you can then create a DMChannel which, again, returns a promise.
const dmChannel = await user.createDM()
You can then use your channel like you normally would
dmChannel.send('Hello!')
Try to create a variable like this:
var user = client.users.cache.find(user => user.id === 'USER-ID')
And then do this:
user.send('your message')

Fetching Bans with Discord.JS

I am trying to make my ban count appear in a embed and I am struggling with it. Here is the code:
const log = new Hyperz.MessageEmbed()
.setTitle('Ban Database')
.setColor('#FE2E2E')
.setDescription(`Member Tag: <#${memberbannedid}>\nMember ID: ${memberbannedid}\nTotal Bans: ${message.guild.fetchBans()}`)
.setFooter('Ban Database of DevGuard')
message.guild.channels.cache.get('818633337320767548').send(log)```
fetchBans() returns a collection (once resolved), You are trying to print out the entire collection. To get the count use Collection#size
As Cameron R pointed out, Guild#fetchBans() returns a promise. You will need to resolve the promise first inorder to get the size. Make sure you are in an async function if you plan to use the await method.
const fetchedBans = await message.guild.fetchBans();
//... Your Embed
.setDescription(`Member Tag: <#${memberbannedid}>\nMember ID: ${memberbannedid}\nTotal Bans: ${fetchedBans.size}`)

Why doesn't Collection#find work for offline members who have no roles? (discord.js)

My Goal
My goal is to figure out why Collection#find returns undefined when I try to find a user in my server, but they're offline and have no roles.
Expectation
Usually the console logs an array of all the properties of the user
Actual Result
The console logs Collection#find, as undefined if the user in my server is offline and has no roles
What I've Tried
I've tried using Collection#get, but it turns out that it returns the same response. I've tried searching this up on Google, but no one has asked this question before.
Reproduction Steps
const Discord = require('discord.js');
const client = new Discord.Client();
const {prefix, token} = require('./config.json');
client.once('ready', () => {
console.log('Client is online');
const user = client.users.cache.find(user => user.tag === 'Someone#1234');
console.log(user);
};
client.login(token);
Make sure that whoever is helping you, whether it's an alt account or your friend, that they have no roles, and they're completely offline in your server
Output:
Client is online undefined
I had the same problem. I don't know why this happens, but I used this line to fix that:
((await m.guild.members.fetch()).filter(mb => mb.id === "The ID")).first()`
Basically, this collect all the members, then filters them with the property you want, and the first() at the end is to make it a single object instead of a collection.
Instead of the user.tag property, try using the user.username and the user.discriminator properties. This worked for me
const user = client.users.cache.find(user => user.username === 'Someone' && user.discriminator === '1234');
Also check spelling, capitalization, ect.
A simple solution to my problem is just create an async function and use await guild.members.fetch() to cache the users and then use Collection#find to get the user.

Resources