There is already similar post (How to find a user by username in the client.users collection) but it doesn't answer my question.
I could use this to find user by username:
client.users.find("username", "TESTname");
The problem is that 2 users can have the same username but different discriminator. I could use this to find user by discriminator:
client.users.find(user => user.discriminator == '3173')
The problem with this method is that two users can have the same discriminator. Is there any way to find user by both username and discriminator?
Edit:
#Elitezen suggested I can use user.tag. This works, but only if the message is from that user. I would like to get user from my discord but other than user who wrote the message.
This works only if user is the same as the message user. If it is not the same, it will return null even if this user is inside my discord. Any way to get user's data even if the user is not the same?
client.on('message', async msg => {
client.users.find(user => user.tag == '<name>#<discriminator>');
})
Using the <User>.tag property will return a string containing both the username and discriminator)
client.users.find(user => user.tag == '<name>#<discriminator>');
Related
I've looked at other answers but I'm still confused. Basically I have a firebase auth system and am trying to enforce unique usernames.
So far, I have 2 collections, a users collection and a username collection. Inside the users collection the documents contain user info. Inside the usernames collection each document id is the username and inside the documents are the userId.
To prevent duplicate usernames on user signup I was thinking to make a query to usernames collection and check if the username exists, from there either display an error to the user or continue with sign up.
code:
// Check if user exists
const usernamesRef = collection(db, 'usernames');
const usernameExists = query(usernamesRef, where('usernames', '==', username));
if(usernameExists) {
// handle error
} else {
createUserWithEmailAndPassword(auth, email, password)
//
}
My question is that is there any way to circumvent this, or is there a better way to do this. I've never done something like this before so I'm looking for input / suggestions. Thanks.
see if you need this cloud function
https://bigcodenerd.org/enforce-cloud-firestore-unique-field-values/
so, I am running a boy scout server and for it, I am making a bot. we have been struggling to get everyone's real name for better organization. the bot will ask for their name and they will type !name Travis_c. then I want it to store their discord username in a variable and send me a message saying trullycool => Travis_C. with that information, it would be super simple to change their nickname on the server. I am running into issues with getting the user who sent the previous message, into a variable.
my code is
`
case 'name':
name = args[1];
const channel = message.guild.channels.cache.find(channel => channel.name === "names");
user = message.author()
if(!channel) return;
message.channel.send(`${user} => ${name}`)
`
I am having issues on user = message.author()
I know if I want to reply to something I do message.reply but that wouldn't work in my situation. thanks in advance!
message.author is not a function so there is no need for () at the end of it.
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;
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.
In my discord server, as a verification method, I want my bot to have all users react to the message and then get given the verified role, and remove the old role. The current code I have doesn't grant or remove roles, but doesn't error.
client.on("messageReactionAdd", function(users) {
users.addRole(users.guild.roles.find("name", setup.verify));
users.removeRole(users.guild.roles.find("name", setup.default));
});
There is a problem in the current code. The messageReactionAdd event returns a User object, which does not contain the property .guilds which you attempt to access: users.guilds.r- .
Instead of this perhaps you should the id of the User object and plug that into message.guild.members.get("id here"). Then use the returned Member object with your current code. That should work!
References:
Guild Member
, Message Reaction Event, User Object
Would something like this work?
I'm not sure it will I haven't tested it. This should add the role if the message they reacted to consists of the word "role1" and if they react to a different message it will remove the role. I don't know if this is what you're going for but in theory this should work.
client.on("MessageReactionAdd", function(users) {
if (message.content === "role1") {
users.addRole(users.guild.roles.find("name", setup.verify))
} else if (!message.content === "role1") {
user.removeRole(users.guild.role.find("name", setup.default))
}
});
Are the names of the roles; setup.verify & setup.default?
If not, that's why it is not working.
Try:
client.on("messageReactionAdd", function(users) {
users.addRole(users.guild.roles.find("id", ROLEIDHERE));
users.removeRole(users.guild.roles.find("id", ROLEIDHERE));
});
where ROLEIDHERE put the role id to find the role id just tag the role and before you hit send put \ before the tag and it will supply the id