如何在只有机器人公会的情况下检查用户是否在服务器中 [python discord bot]

时间:2021-05-06 20:01:13

标签: python discord discord.py bots

所以我终于几乎让我的不和谐机器人工作了。问题是,虽然只有机器人的公会 (bot.guilds),但我太笨了,无法访问它的成员。这是一些伪代码:(我在实际机器人中使用了 datetime、asyncio 和其他函数)

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! ?")
                    

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

Guild.members 返回一个 discord.Member 实例列表,您正在将一个字符串与其进行比较。你应该得到一个成员实例,然后比较它。

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! ?")

同样 @{member} 也不起作用,一个成员提到的内部看起来像这样 <@!{member_id}>,幸好 discord.Member 实例已经有 mention 属性

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

参考: