Why isn't my member-variable filled if I use discord.utils.get() or discord.utils.find()?

Viewed 339

I've tried to find a specific user. I'm using discord.py with discord.utils.get() but "member" never seems to be filled.

elif "hug" in message.content.lower():
    args = message.content.split(' ')
    if len(args) == 2:
        author = message.author
        member: Member = discord.utils.get(message.guild.members, name=args[1])
        if member:
            await message.channel.purge(limit=1)
           await message.channel.send(f'Hey, {member.mention}! {author.mention} sends a lot of hugs and love!')

I even tried discord.utils.find() in combination with lambda:

elif "hug" in message.content.lower():
    args = message.content.split(' ')
    print(str(args[0]) + ' ' + str(args[1]))
    if len(args) == 2:
        author = message.author
        member: Member = discord.utils.find(lambda m: args[1] in m.name, message.guild.members)
        if member:
            await message.channel.purge(limit=1)
            await message.channel.send(f'Hey, {member.mention}! {author.mention} sends a lot of hugs and love!')

Is there something that I'm missing?

3 Answers

There are a couple of reasons why member could be None. Most common is that you're not using the correct intents. You should pass the members=True argument to discord.Intents() and enable member intents in Discord's developer portal.

intents = discord.Intents(guids=True, messages=True, members=True)
bot = commands.Bot(commands_prefix='.', intents=intents)

Just a suggestion. The easiest way to do what you're trying to do, is using the discord.ext.commands module.

from discord.ext import commands

@bot.commands()
async def hug(ctx, member: discord.Member):
    await ctx.message.delete()
    await ctx.send(f'Hey, {member.mention}! {ctx.author.mention} sends a lot of hugs and love!')

do you use Intents?

If not implement it like this:

intents = discord.Intents.default()  
intents.members = True

and then when you instantiate the client give it the intents like following:

client = discord.Client(intents=intents)

Hope that helps ;)

I think you want await discord.Guild.fetch_member(member_id). Can you please send the exception you had? the discord.utils methods you use might need to be awaited.

Related