Can't get user avatar - discord

I'm trying to get user's avatars for a welcome message but when someone joins it never shows it. I've tried many different methods. Here is my current code.
bot.on('guildMemberAdd', member => {
const exampleEmbed = new MessageEmbed()
.setColor('#0000FF')
.addField('Welcome!', `Welcome to the server, ${member}! Go to <#channel> for the rules, then grab some roles at <#channel>!`)
.setThumbnail(member.user.displayAvatarURL)
member.guild.channels.cache.get('693219932904751177').send(exampleEmbed);
});

All you need to do it add a () behind the .displayAvatarURL
So the line would look like this:
.setThumbnail(member.user.displayAvatarURL())

Related

Is there an easy way to make my bot mention the person its moving?

so basically I wanted to make my bot move people to afk as soon as they deafen. and I have a command to make it generate messages in chat, but the question is, can I make it # them as well? and if so how?
code:
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_VOICE_STATES", "GUILD_MEMBERS", "GUILD_PRESENCES"] });
client.on("voiceStateUpdate", (oldState, newState) =>
{
if (newState.selfDeaf)
{
console.log('User has deafened');
newState.member.voice.setChannel("695840093167943751");
client.channels.cache.get("664487959940169738").send('Undeafen Bitch');
}
To tag a user in discord, the format is <#USERID> so if a user's id is 1, you'd have to include <#1> in what you are sending.
So onto your code, you'd have to change the last line to something like the following:
client.channels.cache.get("664487959940169738").send('<#123> Undeafen Bitch');
But we can't hardcode the id since it'll be unique for each user moved. This should automatically tag the user who got moved:
client.channels.cache.get("664487959940169738").send(`<#${newState.member.id}> Undeafen Bitch`);

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.

Creating a Discord bot to notify people when to certain text on a website has changed

const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '';
bot.on('ready', () =>{
console.log('This bot is online');
})
bot.on('message', msg=>{
if(msg.content === "?rates"){
msg.reply('.')
}
})
bot.login(token);
This is what I have so far it is very basic, I understand, I'm just trying to get some sort of idea how to process. What I want is as soon as a website gets updated or changed. I would like it to tag everyone and in a certain channel and specifies what has changed. I know this will be a long process but I'm in for the ride :) would appreciate any help.
You need a webhook on the website and listen to it with your bot, if you have control over the site, this may help you, otherwise you could look if the site has one or perhaps ask an owner.
A probably working but not very nice (and not very clean) solution would be to save the text of the website every 5 seconds or so and compare it to the previous save. If it changed, you notify the members through sending a message.

How can i get mentioned user's username in discord.js

Read the title and will be glad if anyone can help me guys
For an example messages.mentions.first(). Like that, is there anything for mentioned user's username?
The best way would be to use the message.mentions.users.first(), as this will actually give you the first user mentioned in a message. (message.mentions is not a collection). If there are no users mentioned, the first will result in undefined, so you will probably want to check for that. Once you have a user, you will want to access the username property.
Example:
// Assuming we just need a message event
client.on('message', (message) => {
const user = message.mentions.users.first();
if (user === undefined) {
return; // Do not proceed, there is no user.
}
const name = user.username;
// Do stuff with the username
})
Sources:
message.mentions
message.mentions.users
message.mentions.members.first().user.username;
This would give you the username of the first mention.
You could also do message.mentions.users.first().username;

How to get username from client id after the user has left the guild

I have statistics. I am downloading from the User Id database and I am replacing the ID with its name, but if the user has left the server, there is an error because there is no such user.
I have bot.users.get(id) but when a user leaves the server, an error pops up.
Can I get the username with the ID differently if the user is not on the server?
Sorry for being late, like literally. You can perform a request to get a user from Discord by a UserResolvable with Client#fetchUser(<UserResolvable>);.
In practice, the code should look like this;
const Discord = require("discord.js");
const Client = new Discord.Client();
Client.login("YoUr.nIcE.toKe.n");
Client.on("ready", function () { // Should do that when the client is ready.
const User = Client.fetchUser("123456789012345678");
console.log(User); // Some user object.
});
Hope I helped you nonetheless.
~Q
Since client.users stores only cached users, you won't be able to retrieve their username in a reliable way from there.
DISCLAIMER: This method is really inefficient, but discord.js hasn't been made for this kind of work. If you want to make it easier, just write Unknown user (client_id)
If you're not able to get the username from a user list you could try to use messages: use channel.fetchMessages() on every channel of your guild until you find a message in which message.author.id == your_id, when you find it you can get the username with message.author.username
There's also another solution: a self-bot. Keep in mind that this one is not supported by Discord itself, and could result in a permanent ban for the account you're using.
With that said, if you use a self-bot you can use the TextChannel.search() method to quickly find a message with your author id and then grab the username from the author.

Resources