Discord.js target, cache user id - discord.js

I want the bot to cache the user id, this is what I got
let target = message.mentions.users.first() || ??? || message.author;

Here you go:
message.guild.members.cache.get(args[0])

Related

How to remove a role in discord.py without discord.ext

I want to remove a role from a user without using discord.ext ( because of certain resons I can't ) but I don't know how to do it ( so without commands ( by commands I mean #bot.commands....), and without ctx )
If you want this is the code where I want to put the thing that will remove the role :
if message.content.startswith('$rmv_role'):
member4 = message.content
member5 = member4.split()
member6 = member5[1]
server_id = message.guild.id
guild = client.get_guild(server_id)
role_id = 999988854696194048
role = discord.utils.get(guild.roles, id=role_id)
await message.delete()
user_id = ''.join(c for c in member6 if c.isdigit())
user = get(bot.get_all_members(), id=user_id)
*the code that remove roles*
return
discord.Member has a method called remove_roles().
So, the code is following:
await user.remove_roles(role)

How to check if a specific thing exists in quick.db?

So i was making a discord game bot where you collect chracters by opening chests, but the problem is that the characters appear again when you already have them.
I used the method db.push
the code:
if(has === true){
console.log(has)
let embed = new discord.MessageEmbed()
.setTitle('Chest Opening | Clash Chest')
.setDescription("<#"+message.author+"> got:\n"+amount+" :coin:")
.setColor('RANDOM')
.setFooter('Created by Tahmid GS#1867')
message.channel.send(embed)
await db.fetch(`coin_${message.author.id}`)
db.add(`coin_${message.author.id}`, amount)
}
else if(has === false){
console.log(has)
await db.fetch(`troop_${message.author.id}`)
db.push(`troop_${message.author.id}`, nada)
let embed = new discord.MessageEmbed()
.setTitle('Chest Opening | Clash Chest')
.setDescription("<#"+message.author+"> got:\n"+amount+" :coin:\n||Common: "+nada+"||")
.setColor('RANDOM')
.setFooter('Created by Tahmid GS#1867')
message.channel.send(embed)
await db.fetch(`coin_${message.author.id}`)
db.add(`coin_${message.author.id}`, amount)
await db.fetch(`card_${message.author.id}`)
db.add(`card_${message.author.id}`, 1)
}
has is let has = db.has(troop_${message.author.id}, nada)
I used let has = db.has(troop_${message.author.id}.nada)
and let has = db.has(nada,troop_${message.author.id})
But it doesn't seem to work, in the console is "false"
nada is the random chracter
You are checking if the database has a specific key. In the database under the key troop_${message.author.id} there is an array. You want to check if that array includes the character or not.
let has = (db.get(`troop_${message.author.id}`) || []).includes(nada);
Also you might want to check for the possibility that db.get() returns undefined or something like that. In that case we call .includes() on an empty array. Instead of getting an error TypeError: Cannot read property 'includes' of undefined.

discord.js v12 info command always showing offline status

So I'm trying to make a command that show info about a specific user, the command works fine but the status of the user always shows as offline
This is the main part of the info code that shows the status of the user
if (user.presence.status === 'online') status = `${client.config.emojis.ONLINE}Online` ;
if (user.presence.status === 'idle') status = `${client.config.emojis.IDLE}Idle`;
if (user.presence.status === 'dnd') status = `${client.config.emojis.DND}Dnd`;
if (user.presence.status === 'offline') status = `${client.config.emojis.OFFLINE}Offline`;
if (user.presence.clientStatus != null && user.presence.clientStatus.desktop === 'online') plateforme = '🖥️ desktop'
if (user.presence.clientStatus != null && user.presence.clientStatus.mobile === 'online') plateforme = '📱 Mobile'
let permissions_arr = userInfo.permissions.toArray().join(', ');
let permissions = permissions_arr.toString()
permissions = permissions.replace(/\_/g, ' ');
const embedMember = new MessageEmbed()
embedMember.setFooter(`ID: ${userInfo.user.id}`,``,true)
embedMember.setThumbnail(userInfo.user.displayAvatarURL({ dynamic: true }))
embedMember.setColor(`${client.config.color.EMBEDCOLOR}`)
embedMember.setTitle(`${userInfo.user.tag}`)
embedMember.addField('Joined:', `${moment.utc(userInfo.joinedAt).format('MMMM Do YYYY \n -HH:mm')}`, true)
embedMember.addField('Account created:', `${moment.utc(userInfo.user.createdAt).format('MMMM Do YYYY \n -HH:mm')}`, true)//
embedMember.addField('Status:', `${status}`, true)
embedMember.addField('Roles:', `${userInfo.roles.cache.map(r => r.toString()).join(' ')}`)
message.channel.send(embedMember);
User status is part of the user's presence. In order to access the presence of guild members, you must subscribe to the GUILD_PRESENCES intent. This is what the construction of your Client object will need to look like:
const bot_intents = ["GUILD_PRESENCES"];
const client = new Discord.Client({intents: bot_intents, ws:{intents: bot_intents}});
Because presences are a privileged intent, you will also need to go to your bot's application page in the Discord Developer Portal and enable this setting:
You can, of course, add any other intents you may need/want to the bot_intents variable. This answer assumes you are not already using the GUILD_PRESENCES intent, as you did not mention it in your question.

How to make by bot to leave a server with a guild id

I want my bot to leave a discord server by using ;leave <GuildID>.
The code below does not work:
if (message.guild.id.size < 1)
return message.reply("You must supply a Guild ID");
if (!message.author.id == 740603220279164939)
return;
message.guild.leave()
.then(g => console.log(`I left ${g}`))
.catch(console.error);
You're most likely not supposed to be looking at message.guild.id, since that returns the ID of the guild you're sending the message in. If you want to get the guild ID from ;leave (guild id), you'll have to cut out the second part using something like .split().
// When split, the result is [";leave", "guild-id"]. You can access the
// guild ID with [1] (the second item in the array).
var targetGuild = message.content.split(" ")[1];
!message.author.id will convert the author ID (in this case, your bot ID) into a boolean, which results to false (since the ID is set and is not a falsy value). I'm assuming that you mean to have this run only if it the author is not the bot itself, and in that case, you're most likely aiming for this:
// You're supposed to use strings for snowflakes. Don't use numbers.
if (message.author.id == "740603220279164939") return;
And now, you just have to use the guild ID that you got from the message content and use that to leave the guild. To do that, just grab the Guild from your bot cache, and then call .leave(). All in all, your code should now look like this:
// Get the guild ID
var targetGuild = message.content.split(" ")[1];
if (!targetGuild) // targetGuild is undefined if an ID was not supplied
return message.reply("You must supply a Guild ID");
if (message.author.id == "740603220279164939") // Don't listen to self.
return;
client.guilds.cache.get(targetGuild) // Grab the guild
.leave() // Leave
.then(g => console.log(`I left ${g}`)) // Give confirmation after leaving
.catch(console.error);

How to get role members count in discord.js

I want to get member count who have specific role. role.member is collection. how can i get member count?
ps i'll use role id.
You can use role.members.size:
client.on('message', (message) => {
let guild = await message.guild.fetchMembers();
let roleID = '3933783737379';
let memberCount = guild.roles.get(roleID).members.size;
message.channel.send(memberCount + " members have this role!");
});
Note this only counts cached members so maybe you will have to use guild.fetchMembers() before.

Resources