Getting a discord id from username - python

I am making a bot that has a database of things and sometimes someone would want to see what things a different person has, this would be possible by using discord id, but I just don't was everyone sending their discord ids around.
I was trying to use something like this:
def get_user_id(name_en, tag_en, guild_id):
guild = client.get_guild(guild_id)
user = discord.utils.get(guild.members, name= "Atom" , discriminator= "#1803")
print(user)
But that just outputs an empty message.
So is there a way to get a discord id from a username?
Also is this possible with making the discord bot in a different programming language? (if not possible in discord.py)
Thanks

You can use Guild.get_member_named()
The name can have an optional discriminator argument, e.g. “Jake#0001” or “Jake” will both do the lookup. However the former will give a more precise result. Note that the discriminator must have all 4 digits for this to work.
def get_user_id(name_en, tag_en, guild_id):
guild = client.get_guild(guild_id)
user = guild.get_member_named(name_en)
print(user)

Related

Get all connected members of discord channel

I'm working on a move command, which moves members with the role "Officer" into another channel. But I'm struggeling to get all members in the channel. The most logical solution would be:
When I print(members) I just get [] although I was connected to the channel.
I tried the .keys() solution too but i dont get the object back, but my ID in a dictionary. Then I tried get_member but the result is always None.
guildG is a global variable and set in on_ready() as follows:
My intents:
Added the intents to the bot constructor. Thats the trick :)

Get Discord user ID from username

If I have a user's Discord name and discriminator as a string (e.g ExampleUser#1234) how can I get their user ID from it? I've found get_user(id), but that returns a user object from an ID. I'm using Python and Discord.py.
In one line just do
discord.utils.get(client.get_all_members(), name="ExampleUser", discriminator="1234").id
notice the .id at the end, that gives you the ID of that person.
Also for the discriminator part don't include the hash(#)
As stated in this excellent answer by Sam Rockett (Finding a User ID by Discord Discrim via Python (1st ans) you can try this:
p = client.get_all_members()
found_members = filter(lambda m: m.discriminator==your_discrim, p)
member = discord.utils.get(found_members, name=your_username)
id = member.id
P.S. This snippet only works for finding members who share a server with the bot. To find users who do not share a server with the bot, you must have already an ID.
Discord Bots Hub
Hello Discord User
I will tell you the best way to do this. You have to search the member user and matching username with member user.
message.guild.members.find(m => m.user.username === 'USERNAME').user.id
https://thediscordbots.blogspot.com/2019/10/what-are-discord-bots-really-and-which.html

Obtaining username from user id | discord.py

Well I have been working with databases for a while with discord in order to obtain major lists of user id's in a queue, although I am having problem's obtaining the user from user id as it returns none
For Example
members = list(privateduos[matchid])
user = discord.User(id=int(members[0]))
await client.say("Say `test` " + str(user))
await client.wait_for_message(content="test", author=user)
This is the Output
The client.wait_for_message doesnt seem to detect the message author in the code as well, any solutions?
The method client.get_user_info is deprecated since the last Migration, you should use Client.fetch_user() instead
See this link for the details : https://discordpy.readthedocs.io/en/latest/migrating.html
Use await client.get_user_info(members[0])
From there on, (assuming that you have allocated the returned value to user) you can do user.name to obtain the username.
(Or fetch other information about the user as stated here.)

(discord.py) Getting a list of all of the members in a specific voice channel

So I'm attempting to write a raiding bot for my raiding discord using the discord.py library in python. This scripts is supposed to be forming a list of members in the voice channel for raiding. For some reason, this script is not working. Whenever memids is printed, it just prints an empty list.
If anyone is familiar with discord.py and could tell me why this isn't working, please do. It's really troubling me and I have tried everything in my knowledge to fix it.
#find raiding
voice_channel = discord.utils.get(ctx.message.server.channels, id = '440014722587426816')
#finds the members
members = voice_channel.voice_members
memids = []
for member in members:
memids.append(member.id)
print(memids)
If you know the id of the channel you can do it this way. Works for me :D
channel = client.get_channel(1234567890) #gets the channel you want to get the list from
members = channel.members #finds members connected to the channel
memids = [] #(list)
for member in members:
memids.append(member.id)
print(memids) #print info
I faced the same problem. voice_channel.members would return either empty or incomplete lists some times.
The docs say:
voice_states
Returns a mapping of member IDs who have voice states in this channel.
Note: This function is intentionally low level to replace members when the member cache is unavailable.
https://discordpy.readthedocs.io/en/latest/api.html#voicechannel
I guess the members can't be trusted to return accurate connected member list consistently.
I solved this problem with the following code:
member_ids = voice_channel.voice_states.keys()
You need to enable "SERVER MEMBERS INTENT:
If your bot tracks server members or downloads the entire member list, you may need the server members intent to receive member events and the member list." on the discord developer page on the bot tab.
There isn't much to go on from your question. I believe your problem is that the id that you provided to utils.get(...) isn't the correct id of the voice channel. This is probably the reason to why you're always getting an empty list.
voice_members
A list of Members that are currently inside this voice
channel. If type is not ChannelType.voice then this is always an empty
array.
If you're not fully sure about the voice channel's actual id, I suggest you search by the name and type (discord.ChannelType.voice):
voice_channel = discord.utils.get(ctx.message.server.channels, name="channelname", type=discord.ChannelType.voice)
I you know the channel id, I suggest using
voice_channel = client.get_channel(channel_id)
instead (documentation here). If you're using discord.py-rewrite, you can also use:
voice_client = ctx.guild.get_channel(channel_id)
if the channel you're looking for is in the context guild (documentation here).

Get all of the users from a specific channel Slack API

I am trying to make a Slack Bot using python, and I have a issue, I am not able to get the users from a specific channel, I only succeed if I take all of the users. Basically I want only those from (eg. random channel).
Until now I tried to get the team's ID from every user and compare it to a channel ID, but that failed because everyone is having the same ID and I can't figure out why.
Here is the snippet of the code:
def users_of_the_channel():
global slack_client
#this is the variable which is initialized with SlackClient(BOT_TOKEN)
api_call = slack_client.api_call( "users.list",channel="C0XXXXXXX")
if api_call.get('ok'):
channels = api_call.get('members')
for channel in channels:
print ("this is cool : ", channel['team_id'])
The issue I believe is that when I initialize the api_call variable I call the function with the users.list argument, I tried with usergroups.list and usergroups.users.list but with no success.
Basically to keep it short I need the list with the users from a channel, and the documentation hasn't helped me.
users.list does not take channel id as input, which you are providing in your API call.
Slack provide different bot token for different teams.
Simply call api with bot token of team of which you require user list and you will get the list of members.
client = SlackClient(SLACK_BOT_TOKEN)
request = client.api_call("users.list")
if request['ok']:
for item in request['members']:
print item['name']
For more details about optional arguments you can provide with api call, refer documentation(https://api.slack.com/methods/users.list).
You probably can use the new conversations API to retrieve that (specifically conversations.members endpoint):
https://api.slack.com/methods/conversations.members

Categories

Resources