Discord机器人通过用户ID发私信报错:Client无get_user_info成员
Fixing the "Instance of 'Client' has no 'get_user_info' member" Error
Hey there! That pylint error is popping up because the get_user_info method you're using is deprecated and removed in modern versions of discord.py (v1.0+). Let's get your DM functionality working correctly.
What to Use Instead
Discord.py replaced get_user_info with two more precise methods:
client.fetch_user(user_id): Fetches user data directly from the Discord API. Use this if you need to message a user who isn't in any server shared with your bot, or if you want the most up-to-date user info. This requiresawaitsince it's an asynchronous API call.client.get_user(user_id): Pulls user data from the bot's local cache. Use this for users who are in at least one server with your bot—it's faster and doesn't needawait.
Corrected Code Example
Here's how to rewrite your code to send a DM properly:
@client.event async def on_message(message): # Avoid responding to the bot's own messages to prevent loops if message.author == client.user: return if message.content.lower() == 'dm': # Replace this with the actual user ID (integer, not a string) target_user_id = 123456789012345678 try: # Fetch the user (works for any user, even outside shared servers) user = await client.fetch_user(target_user_id) # Send the DM directly via the user object await user.send("Your message") print("DM sent successfully!") except discord.NotFound: print("Error: That user doesn't exist, or the bot can't access them.") except discord.Forbidden: print("Error: The bot doesn't have permission to send a DM to this user.")
Key Notes
- User ID Format: The user ID should be an integer, not a string. Remove the quotes around
"USER'S ID"and replace it with the numerical ID. - Error Handling: Adding try/except blocks helps catch common issues like invalid user IDs or missing permissions.
- Cache vs API: Use
get_userif you know the user is in a shared server (noawaitneeded), otherwise stick withfetch_user.
内容的提问来源于stack exchange,提问作者Linkachus 17
相关产品推荐
相关产品推荐

