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

Multi tool use
How to get username from client id after the user has left the guild
I have: bot.users.get(id) but when user leave the server an error pops up. Can I get the username with the ID differently if the user is not on the server?
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. What can I do?
1 Answer
1
Since client.users
stores only cached users, you won't be able to retrieve their username in a reliable way from there.
client.users
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)
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
channel.fetchMessages()
message.author.id == your_id
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.
TextChannel.search()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How do you plan on getting the id if the user is not on the server anymore. Input?
– Rishav
Jun 29 at 10:53