Discord.py get user with Name#0001 - python

How do I get the user/member object in discord.py with only the Name#Discriminator? I searched now for a few hours and didn't find anything. I know how to get the object using the id, but is there a way to convert Name#Discriminator to the id?
The user may not be in the Server.

A bot can only get a User instance, that it isn't in a server with, from a unique ID.
You can use both discord.utils.get and get_all_members to get a Member using your input though:
member = discord.utils.get(bot.get_all_members(), name='name', discriminator='discriminator')
# discriminator should not contain #
P.S afaik utils.get takes any kwarg that could be an attribute for a class

There's no way to do it if you aren't sure they're in the server. If you are, you can search through the servers' members, but otherwise, it wouldn't make sense. Usernames/Discriminators change all the time, while IDs remain unique, so it would become a huge headache trying to implement that. Try doing what you want by ID, or searching the server.

Related

Get all Memebers with a certain Role

I have been trying for quite some time to get a list of all members with a specific role, but I fail every time.
I know that there is role.members but I have no idea how to use it in order to get a list of all members with a specific role.
I have looked through many questions (to get a list of members with a specific role) here on Stack Overflow, but I was not able to find a working solution
How can I get a list of all members with a specific role?
If you are sure role is a discord.Role model, role.members should return a List of members. I assume you are getting an empty list, if it is then you are probably missing Member Intents to see the members of a Role and this is probably the reason you are only getting an empty list.
Discord.py has it is own explanation about this topic that you can read here.
(Also, i saw that discord.py didn't mention it but if you want to turn on all of the intents rather than specifying it, you can use discord.Intents.all method to make an intent that has everything turned on in it.)

Python Telethon Can't find any entity corresponding to this entity

I try to access a group and want to read all massages. The problem is not the code, the problem is that I get the error: ValueError: Cannot find any entity corresponding to "Whale Alert".
But the weird thing is if I try it with other session names, sometimes it works. Does somebody know how I can get the right group 100%?
What other options do I have as input entity or session name? Maybe this could be the reason.
Here is my code:
with TelegramClient('Me', api_id, api_hash) as client:
for message in client.iter_messages('Whale Alert'):
#Do Sth
The full error literally tells you what to study. It points to the docs:
https://docs.telethon.dev/en/latest/concepts/entities.html#summary
This error is often encountered when trying to use an entity (user, channel, chat etc.) by only its ID. This will only work if your session has ever "seen" and thus cached the entity. That's why it's not suprising that different sessions might give you different results.
When using strings telethon checks if it's a username and if it's not it will try to get the entity from cache. in the other session, you have the name saved in your cache.
Possible solutions:
Use the username of the chat instead of the name
Use get_dialogs() to fill up the cache
I solved this error by printing all chats and finding the id of the chat I am interested. After manually obtaining the id, get_entity(id) worked for me.
for dialog in client.iter_dialogs():
if dialog.is_channel:
print(f'{dialog.id}:{dialog.title}')

Is there a way to get the id's of all members who are connected to a voice channel in a guild?

Every way I found on the internet apparently doesn't work anymore with the latest Discord.py version.
If there is no way, is there a way to get all voice-channel id's? This would also fit what I want to achieve. I cant provide code since I deleted everything I've tried but I tried for two hours.
To start with you need a VoiceChannel object.
Once you have this object, you can use the members attribute to get a list of members that are connected to the voice channel. You can then access the member objects one by one getting the ids.
Example:
member_ids = []
for member in voice_channel.members
member_ids.append(member.id)

Discord.py - How to track if someone joined from a specific invite?

I'm trying to figure out how to see who joined from a specific invite within my guild - is this possible? The end goal is for the bot to grant a role to a user if they joined from a specific invite, and grant a seperate role if they joined from a different invite
Yes, it is, but you'll have to do the heavy lifting.
Here's the reference for the Invite object. It has a uses attribute which stores the amount of times it's been used to join your server: so if you store all the possible invites for your server in memory, and check which one went up whenever a user joins (in on_member_join(), you'll be able to tell which one they used.
This medium article shows a way to do it. There's no other way I'm aware of, or that I can find in the documentation.

Finding a user by their username#discrim

Is it possible to get someone's user ID who you know their username and discrim, but is not in a mutual server with you?
Thanks.
I don't think so. It makes sense since this can easily be abused. Imagine all the spam bots if bots can see all discord users.
Below are two ways to get user info using discord.py, but note that they cannot be used as you ask.
client.get_user_info can be used to get user info even if you don't share a server, but it takes the unique ID as an argument.
server.get_member_named returns the unique user ID and takes username plus the optional discriminator as input, but requires that you share server/guild with the user to work.

Categories