Obtaining username from user id | discord.py - python

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.)

Related

Getting a discord id from username

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)

Tweepy Check Blocked User?

Is there a way to check if the user is blocked like an is_blocked method? The way I have it set up right now is to get a list of my blocked users and comparing the author of a Tweet to the list, but it's highly inefficient, as it constantly runs into the rate limit.
Relevent Code:
blocked_screen_names = [b.screen_name for b in tweepy.Cursor(api.blocks).items()]
for count, tweet in enumerate(tweepy.Cursor(api.search, q = query, lang = 'en', result_type = 'recent', tweet_mode = 'extended').items(500)):
if tweet.user.screen_name in blocked_screen_names:
continue
No, you'll have to do that the way you're currently doing it.
(Also, just a side note, you're better off checking your blocked users via their account ID rather than their screen name, because this value will not change, whereas a user's screen name can)
For future reference, just check the Twitter API documentation, where you can get the answer for something like this straight away :) save yourself the waiting for someone to answer it for you here!
You'll notice that both the documentation for V1 and V2 do not contain an attribute as you have described:
V1 User Object:
https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet
V2 User Object:
https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/user

Fire Store's on_snapshot() function in Python executes twice. How to make it execute once?

This is my callback that executes when a user is added to a collection
# Create a callback on_snapshot function to capture changes
def on_snapshot_user(col_snapshot, changes, read_time):
print(u'Callback received query snapshot user.')
for change in changes:
if change.type.name == 'ADDED':
doc = change.document.to_dict()
email = doc["email"]
group = doc["survey_group"]
gender = doc["survey_gender"]
age = doc["survey_age"]
userInfo = User.query.filter_by(email=email).first()
if userInfo is None:
User.insert(User(email))
userInfo = User.query.filter_by(email=email).first()
userInfo.group = group
userInfo.gender = gender
userInfo.age = age
User.update(userInfo)
email is a primary key. When a user is added to Firestore, this triggers twice and it gives an error of a duplicate key.
How do I make this execute once in Python as metadata.PendingWrites is not yet supported in Python?
I am developing in Flask. I'm new to it so would appreciate any kind of help.
Edit:
Some context - I am adding the data I get to a PostgreSQL database. I am planning to do so as I need to build a kind of leaderboard and I'm planning to store the user info + their points in a postgreSQL table.
There are some steps to resolve multiple execution issue :
1. First would be to create a doc reference variable for the document you want to execute.
Create a doc_ref variable for the document you want to populate.
Syntax : doc_ref = db.Collection(collection_name)
2. Second step would be to watch the document.
doc_watch would be the variable created for watching the document.
Syntax : doc_watch = doc_ref.on_snapshot_functionName(on_snapshot_functionName)
3. Third step would be to Terminate watch on the document so it will be executed once.
You can use unsubscribe() to terminate the watch.
Syntax : doc_watch.unsubscribe()
For further details related to real time updates in Cloud Firestore, you can refer to the documentation [1].
[1] : https://cloud.google.com/firestore/docs/query-data/listen

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

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