1
votes

I'm trying to send a welcome DM to users that join my discord server, but I have the bot setup in multiple servers. I'm trying to check the guild then send a message based on which guild it is in, but it's not working. I've looked and the popular question like this on stackoverflow uses commands and ctx, which cannot be used in on_member_join().

@client.event
async def on_member_join(member):
    guild = client.get_guild(762921541204705321)
    if guild == 762921541204705321:
        await member.create_dm()
        await member.dm_channel.send("Welcome!")
1

1 Answers

1
votes

According to the documentation, when you call get_guild() it doesn't return the guild ID, it returns a Guild object. From the source code, it appears that this guild class does have its comparison operator overloaded, so it cannot deal with comparisons between a Guild object and an integer ID.

The solution to your problem is to just compare the number ID with the Guild.id attribute:

@client.event
async def on_member_join(member):
    # client.get_guild returns a Guild object!
    guild = client.get_guild(762921541204705321)

    # Get the ID from the 'id' attribute on the guild object and compare.
    if guild.id == 762921541204705321:
        await member.create_dm()
        await member.dm_channel.send("Welcome!")