bad_coder
bad_coder

Reputation: 201

How do I check if a user is in a server while only having the bot's guilds [python discord bot]

So I finally managed to almost get my discord bot working. The problem is, while only having the bot's guilds (bot.guilds), I am too stupid to access the members of it. Here is some pseudo-code: (I'm using datetime, asyncio and other functions in the actual bot)

def check_for_bds():
    any_bds = True # done by a function 
    bd_list = ["member1#1234", "member2#1234"] # a list of the members who have their birthday
    if any_bds:
        for guild in bot.guilds:
            for member in bd_list:
                if member in guild.members: # this doesn't work as guild.members isn't accessable like this
                    await guild.system_channel.send(f"@{member} has their birthday today! Happy Birthday! 🥳")
                    

Any help would be much appreciated :)

Upvotes: 0

Views: 356

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

Guild.members return a list of discord.Member instances and you're comparing a string to it. You should get a member instance and then compare it.

for guild in bot.guilds:
    for member in bd_list:
        name, discriminator = member.split("#") # getting the name and discriminator
        member = discord.utils.get(guild.members, name=name, discriminator=discriminator) # Getting the instance
        if member in guild.members:
            await guild.system_channel.send(f"@{member} has their birthday today! Happy Birthday! 🥳")

Also @{member} will not work, a member mention internally looks like this <@!{member_id}>, fortunatelly a discord.Member instance already has the mention attribute

await guild.system_channel.send(f"{member.mention} has their birthday today! Happy Birthday! 🥳")

References:

Upvotes: 1

Related Questions