Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

Python Discord get_member() returns a value of None?

Im using the following code to get a user thats in the server however it always returns a value of none. The bot has admin perms and we are spelling the users username correctly each time. I'm not including the numbers at the end even though I have tried that. Please can someone show me where I'm going wrong. Thanks

@client.command(name="getuser" ,pass_context=True, no_pm=True)
@commands.guild_only()
async def get_user(ctx):

    msg = ctx.message
    
    server = msg.guild

    await ctx.send(f"The @ of the user")

    def getUsername(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel 

    Key = await client.wait_for("message", check=getUsername)

    user_name = ctx.guild.get_member_named(name=str(Key.content))
    
    if not user_name:
        try:
            user_name = ctx.guild.get_member(int(Key.content))
            await ctx.send(f"Result: " + str(user_name))
        except:
            print("User Not Found")
    if not user_name:
        try: 
            user_name = self.bot.get_user(int(Key.content))
            await ctx.send(f"Result: " + str(user_name))
        except:
            print("User Not Found")
        
    if not user_name:
        await ctx.send(f'Could not find user.')
        
        

    print(f"Result: " + str(user_name))
question from:https://stackoverflow.com/questions/65598442/python-discord-get-member-returns-a-value-of-none

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

get_x and utils.get both return None if they can't find anything matching. Common causes include:

  • Not subscribed to the relevant intent.
  • Wrong key
  • Bot not logged in, and trying to grab objects from cache
  • Bot cannot "see" the object.
  • Objects retuned by fetch_x will not have a populated cache.

Pretty sure you didn't enable intents.members, see: Privileged Intents

Also, those three if-statements are useless, you can put everything in one


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...