Check status of user - node.js

How do i check the status of a user in my server? as user.presence.status is depreciated. And I won't use
let guild = await client.guilds.cache.find(guild => guild.id === "xxx");
let guildMembers = await guild.members.fetch({ withPresences: true });
var onlineMembers = await guildMembers.filter(member => !member.user.bot && member.presence?.status != "offline");
Since this just checks who's online but doesn't actually return the status of the specific user I need

You can access it through <GuildMember>.presence.status, provided that the appropriate intents are enabled.
For help on intents you may want to look at the guide.
If you have the ID, you can do:
const member = await guild.members.fetch(ID);
console.log(member.presence.status)

Related

Check if user has a role in the new discord.js version

Hey I wanted to check if a user had a role, although all other methods are quite outdated and I've tried a couple of things, none of them work.
My attempt:
var guild = client.guilds.cache.get("Guild ID")
var buyerRole = guild.roles.cache.get("Role ID")
await guild.members.fetch()
const guildMember = guild.members.cache.get(message.author.id)
if(guildMember.roles.find(buyerRole.id)){...
Well, you're going to want to use member.roles.cache.some() to achieve this.
var guild = client.guilds.cache.get("Guild ID")
var buyerRole = guild.roles.cache.get("Role ID")
const guildMember = await guild.members.fetch('User ID');
if(guildMember.roles.cache.some(x => x.id == buyerRole.id)){...
.some will always return a boolean, so it's better for you to use it like that.
After a bit of debugging, it turns out that you can use
message.author._roles instead of message.author.roles in order to get the complete list array.

Discord.js get user by nickname

I'm having a hard time figuring out
Is is possible to find a user by his or her nickname via the discord.js npm package.
How can I query a user by his or her nickname?
i've tried several things but nothing returns the nickname and I can't quite figure out how their documentation works.
So far I haven't been able to tell how to do it.
I've done as so in my code.
const { Client } = require('discord.js')
const discordClient = new Client()
discordClient.on('message', message => {
if (message.author.bot) return
if (message.content.startsWith('!gpwarn')) {
// The command is something like '!gpwarn thomas'.
// If his nick name is thomas but his username is john it doesn't work.
})
Yes, and it's pretty simple. Do this:
const user = client.users.cache.find(user => user.username == "The user's name");
Reference:
UserManager
To meet your exact requirements you would search the guild members cache for the provided nickname. However, I personally would suggest using either a direct tag or a UserID for this, as multiple people can have the same nickname in the server.
const U = message.guild.members.cache.find(E => E.nickname === 'NICKNAME')
Getting by Tag:
const U = message.mentions.members.first()
Getting by ID:
const U = message.guild.members.cache.find(U => U.id === 'IDHERE')
First you need to identify the nickname argument, you can do this by splitting, slicing, and joining message.content.
Next I recommend you fetch all of the GuildMembers from the guild using GuildMemberManager#fetch(), This way you don't run into the error of a member being uncached.
Handle the promise using async/await and use Collection#find() to return the member.
const { Client } = require('discord.js')
const discordClient = new Client()
discordClient.on('message', async message => {
if (message.author.bot) return
if (message.content.startsWith('!gpwarn')) {
// Returns all of the text after !gpwarn
const query = message.content.split(' ').slice(1).join(' ').toLowerCase()
const members = await message.guild.members.fetch()
const memberToWarn = members.find(m => m.nickname.toLowerCase() === query)
if (!memberToWarn) // No member found error
console.log(memberToWarn) // [GuildMember]
}
})

Discord.js: Verify channel

I'm trying to make my own bot for my server, for now i'm focusing on the verify. By acting to the check mark emoji it'll add the verified role, and then it should remove only the user reaction, but instead it'll remove every reaction right away
client.on('messageReactionAdd', async (reactionReaction, user) => {
const message = reactionReaction.message;
const verifyChannel = message.guild.channels.cache.find(c => c.name === 'approvazione');
const member = message.guild.members.cache.get(user.id);
if (member.user.bot) return;
const verify = message.guild.roles.cache.get('728000975046180988');
if (reactionReaction.emoji.name === '✅' && message.channel.id === verifyChannel.id) {
member.roles.add(verify).catch(console.error);
await reactionReaction.remove(member).catch(console.error);
}
here is the message sent by the bot with it's own reaction
and here is the same message after i reacted, and both mine and the bot reaction are removed, i just want my reaction to be removed
If you look at the docs it takes no parameter for the user:
https://discord.js.org/#/docs/main/stable/class/MessageReaction?scrollTo=remove
This was changed in v12, the method now is to use .users.remove:
reactionReaction.users.remove(member);

Is there a way to get invites from ALL the guilds my bot is in?

Any ideas on how to get invites from a discord you aren't in but my bot is in?
I guess you want to get invited to every guild your bot is in
client.guilds.cache.forEach(guild => {
guild.channels.cache.filter(x => x.type != "category").random().createInvite()
.then(inv => console.log(`${guild.name} | ${inv.url}`));
});
Rez's answer is a common one but not a secure one, the random channel you get could be a channel you don't have permission to create an invite in.
This approach is safer, (might be a bit slower since async/await):
(This needs to be in an async function)
const invites = [];
//cant use async inside of forEach
//https://www.coreycleary.me/why-does-async-await-in-a-foreach-not-actually-await/
for (const [guildID, guild] of client.guilds.cache) {
//if no invite was able to be created or fetched default to string
let invite = "No invite";
//fetch already made invites first
const fetch = await guild.fetchInvites().catch(() => undefined);
//if fetch worked and there is atleast one invite
if (fetch && fetch.size) {
invite = fetch.first().url;
invites.push({ name: guild.name, invite });
continue;
}
for (const [channelID, channel] of guild.channels.cache) {
//only execute if we don't already have invite and if the channel is not a category
if (!invite && channel.createInvite) {
const attempt = await channel.createInvite().catch(() => undefined);
if (attempt) {
invite = attempt.url;
}
}
}
invites.push({ name: guild.name, invite });
}
console.log(invites)

Check if the user adding a reaction has a role

I want to check if the user reacting to a message has a role or not, so that if the user dont have a rank the bot will ignore the rest of the command, but I dont know how.
example: if a user with the admin rank reacted with :eggsa: emoji, the bot would continue with the command, but if i only had member rank the bot would ignore me.
client.on('messageReactionAdd', reaction => {
const eggsa = client.emojis.find(emoji => emoji.name === "eggsa");
if (reaction.emoji.name === 'eggsa') {
const message = reaction.message;
const kanal = reaction.message.guild.channels.find('name', 'sitater');
var embed = new Discord.RichEmbed()
.setAuthor(reaction.message.content)
.setTimestamp()
.setFooter(reaction.message.author.username, reaction.message.author.avatarURL)
kanal.send({embed});
}
});
the code works in this stage, only everyone can use it
I would be really grateful if someone could help me >:)
If you look at the messageReactionAdd docs you can see that along with the reaction, you can also get the user which added the reaction. So instead of
client.on('messageReactionAdd', reaction => {
You'd have
client.on('messageReactionAdd', (reaction, user) => {
So then you have the user and which is of type User. Because the user isn't of type GuildMember you first need to fetch the correct Guild member before you can check his/her role.
The easiest way to do this is by getting the Message to which the reaction was added with const msg = reaction.message;. Then you can get the guild from the message with const guild = msg.guild;. Now you can access the guild members with const guildMembers = guild.members;. Lastly you'd need to find the correct member with const guildMember = guildMembers.get(user.id);.
Now that you have your Guild Member you can access his/her Roles and thus check whether he/she has or does not have a certain role

Resources