You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 requires await since 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 need await.

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_user if you know the user is in a shared server (no await needed), otherwise stick with fetch_user.

内容的提问来源于stack exchange,提问作者Linkachus 17

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:38:12