0

I have seen several posts on this topic, but they have not really helped me. I want to check in a on_member_join event if the avatar of the user is a default or not. If a default is the case, then the user should be banned, if not then not.

I had already worked out a code, but it bans the member even if the profile picture is not default:

@client.event
async def on_member_join(member):
    has_avatar = client.check(lambda ctx: ctx.avatar_url != ctx.author.default_avatar_url) # Seen from another post
    if has_avatar:
        await member.ban(reason="Default avatar")
    else:
        channel1 = client.get_channel(ChannelID)
        await channel1.send("No default avatar")

I also tried to compare the member.avatar with a default avatar but that did not work out. What am I doing wrong?

Dominik
  • 3,612
  • 2
  • 11
  • 44

3 Answers3

0

I looked around for you. I checked the member and user class documentation. I couldn't find anything on the profile picture. I'm not sure if this is possible.

  • The [question](https://stackoverflow.com/questions/59428178/how-do-i-check-for-a-default-profile-pic-with-discord-py) I was looking at had this as a solution. However it is for a command, not an event. That is why I thought it is possible. – Dominik Apr 09 '21 at 16:35
0

This is not possible via the normal discord.py library, but I'd suggest an alternative. Use PIL to compare the user's avatar (with discord.py you can actually download the avatar).

What do I mean? Let's say, you have a folder "Default Avatars" with all of the default avatars. You compare the user's avatar with every file in the folder, if it matches any of the pictures, the avatar is default, otherwise it's a custom one.

I have never worked with PIL, but I've found this: Compare images Python PIL maybe, and just maybe it can help you with your problem. I'd suggest looking more into PIL as it seems like an interesting library.

komko
  • 112
  • 1
  • 1
  • 8
  • Really not possible? I found another [question](https://stackoverflow.com/questions/59428178/how-do-i-check-for-a-default-profile-pic-with-discord-py) that is kind of outdated but had this as a solution for a command and it seemed to work. However I just want it as an `event` – Dominik Apr 09 '21 at 16:36
  • Well, reading the question you found actually makes perfect sense. Sorry, my bad, glad you found that, saved yourself a ton of work. I think it might be possible in events as well. You are passing a member, reading the docs I've found: discord.WidgetMember.default_avatar. Might wanna try that out as well? Probably checking member.avatar_url with member.default_avatar_url. – komko Apr 12 '21 at 17:58
  • Thanks for the help. I will try that out besides the already accepted answer! :D – Dominik Apr 13 '21 at 18:45
  • Oh, I haven't seen the accepted answer, my bad. I guess it comes down to preference, both could seem to work, now, probably one could be faster, but I haven't tested, so I guess you should use whatever you feel like using. – komko Apr 14 '21 at 19:24
0

It is possible to check whether a user has an avatar or not. For this you just need member.avatar.

You can build your code based on that:

@client.event
async def on_member_join(member):
    channel1 = client.get_channel(YourChannelID)
    if not member.avatar: # If there is no avatar
        await member.ban(reason="No avatar!")
    else:
        await channel1.send("Avatar!")

Check out the docs for more information.

Dominik
  • 3,612
  • 2
  • 11
  • 44