Fetching all guild members [djs11] - discord.js

I was wondering is it possible to fetch through all members in the guild and get their IDs?
I made something like
msg.guild.members.forEach(guildMember => {
guildMember.fetchMembers().then(console.log(guildMember.id))
})
But I got an error so I guess I did it in
TypeError: guildMember.fetchMembers is not a function
Also, I am using discord v11
Thanks in advance.

You could simply console log all user IDs like this:
msg.guild.members.forEach(guildMember => {
console.log(guildMember.user.id);
})

Related

Discord.Js Message Link

So I've looked through the discord.js docs. I'm not too good at reading docs but is there a way to get the url of the message that initiated the command like:
console.log(`${message.content.URL}`)
To get the URL of a message, you can just use .url. The code would look something like:
client.on('messageCreate', (message) => {
const url = message.url
})

Getting nicknames of users of my Discord bot

I can see the names, ids and user numbers of the servers where my bot is located, but how can I get the list of users (nicknames)?
You can make use of guild.members.fetch() in order to get all members and then use the nickname property to receive their nicknames. Finally I removed all bots with a simple filter.
const members = (await message.guild.members.fetch())
.filter((m) => !m.user.bot)
.map((m) => m.displayName);
console.log(members);
Working example as a command:
client.on("message", async (message) => {
if (message.author.bot) return;
if (message.content === "!list") {
const members = (await message.guild.members.fetch())
.filter((m) => !m.user.bot)
.map((m) => m.displayName);
console.log(members);
}
});
client.login("your-token");
Thanks to #MrMythical who suggested using displayName only. That property automatically returns the normal username when no nickname has been set for a user.
Users do not have nicknames, only guild members, if you are trying to fetch a list of server member nicknames.
You can use the code snippet from below:
const map = message.guild.members.cache.filter(c=> !c.member.user.bot).map(c=>c.displayName).join('\n');
console.log(map)
The
message.guild.members.cache.filter(c=> !c.member.user.bot)
Filters bots from the list, the
.map(c=>c.displayName).join('\n');
maps the data and only the user nicknames and joins them by paragraph breaks.
If there are any issues, please comment!

Firestore TypeError: collection().where() is not a function

I am trying to filter my docs in firebase firestore by checking if a doc 'users' array contains the user's email address. The 'chat' collection contains docs with ids: 'user#email.com:friend#email.com' and a doc contains messages and users stored in array.
I would like to list chats for the current user.
The problem is that every time I would like to use the where() clause I get to following error:
TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_1___default.a.firestore(...).collection(...).where(...).onSnapshot(...).catch is not a function
This is my code:
firebase.firestore().collection("chats").where("users", "array-contains", currentUser.email)
.onSnapshot(async res => {
setChats(res.docs)
})
.catch(function (error) {
console.log("Error getting documents: ", error);
});
After read the "onSnapshot" method reference I understood it does not return anything. The method signature has a void return. You may have to pass the callback you want to be called as parameter of the onSnapshot method.
Actually you have an exemple in the Firestore documentation at the Handle listen errors section.
Bellow the snippet of code from the documentation:
db.collection("cities")
.onSnapshot(function(snapshot) {
// Handle changes
}, function(error) {
// Handle errors
});
I have same issue.I just not imported "where" method,couse when you click CTRL+space it is not showing for imports,and automatically it is not imports,I just written it by my hand.and it`s worked..

welcome event sometimes not firing all the way discord.js

My bot is suppose to welcome a member in both the main join/leave channel and also in the chat room so that way we can all welcome the user. For some reason there's a bug where sometimes it'll not send the welcome message to the chat room.
Error:
(node:194) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of null
The welcome event:
client.on('guildMemberAdd', async (member) => {
const welcomeEmbed = new Discord.RichEmbed()
.setAuthor(member.user.tag, member.user.avatarURL)
.setColor(`GREEN`)
.setDescription(`Welcome **${member.user.username}** to member.guild.name! Consider reading <# {HIDDEN}> to have a basic understand of what we do and do not allow.
Come hang out with us in <#{HIDDEN}> and have a good time!`)
.setFooter(`Member ID: ${member.id}`)
member.guild.channels.find(channel => channel.id === 'HIDDEN').send(welcomeEmbed);
const welcomeEmbed2 = new Discord.RichEmbed()
.setAuthor("New Member Joined!", member.user.avatarURL)
.setDescription(`**${member.user.tag} has joined the server!**`)
.setColor('GREEN')
member.guild.channels.find(channel => channel.id === 'HIDDEN').send(welcomeEmbed2); // This is the one providing the error sometimes
});
I've tried different things such as a .then or just recoding it in different ways to see if it'll work. So far, this has been the only thing my friends do not understand why it's providing errors
You can just fetch the channel by ID using client
client.channels.fetch("SOME_CHANNEL_ID").then(channel => {
channel.send(welcomeEmbed);
})

Get server information with invite link

How can I get the server name, etc. by the invite link with discord.js?
I searched Google but there was no information about this.
You can use Client#fetchInvite that returns a Promise of an Invite like this :
const {guild} = await client.fetchInvite("The Invite");
// 'guild' is a Guild.
// If you aren't in a async function, use this :
client.fetchInvite("The Invite").then((invite) => {
// invite.guild is a Guild.
});

Resources