2

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?

Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
Sutorei
  • 23
  • 4
  • Does this answer your question? [Discord.py rewrite get list of members in guild](https://stackoverflow.com/questions/57633155/discord-py-rewrite-get-list-of-members-in-guild) – filipporomani Apr 07 '21 at 18:03
  • 1
    Unfortunately, it doesn't. I'm trying to get a specific user without using commands. – Sutorei Apr 07 '21 at 20:31

3 Answers3

1

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!')
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
ChrisDewa
  • 642
  • 4
  • 11
  • If I use commands in my code, the bot doesn't seem to recognize/reacts to any inputs anymore. The following is the code (I don't get any exceptions) `import discord from discord import Member from discord.ext import commands import asyncio import random client = discord.Client() intents = discord.Intents(guilds=True, messages=True, members=True) bot = commands.Bot(command_prefix='.', intents=intents) @bot.command() async def hug(ctx, member: discord.Member):` Is there another way without using commands? – Sutorei Apr 07 '21 at 20:26
  • 1
    Because you have both `bot` and `client`.. you have to choose between them. Try to use the same code you had just, pass your intents object in the client constructor. – ChrisDewa Apr 07 '21 at 21:47
1

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.

Nathan Wolf
  • 336
  • 3
  • 12
  • That's the thing: There was no exception. As soon as it checked if member is filled, it returned false.. – Sutorei Apr 07 '21 at 20:09
1

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

DragonCoder
  • 172
  • 2
  • 12