不和谐py我如何给具有ID的服务器成员用户一个角色

时间:2020-11-06 17:33:44

标签: python python-3.x discord.py discord.py-rewrite

我的代码作用的解释:我的代码检查服务器上的成员在其活动中是否具有特定的关键字“ TEST”并将其排名给用户。而且,如果关键字从活动中删除,则排名也将删除。离线成员不受此影响。所以我不得不将成员保存在文件中。

现在我有一个问题:

await bot.guilds[0].get_member(int(member_id)).add_roles(role)
AttributeError: 'int' object has no attribute 'id'

那是我的代码:

@tasks.loop(seconds=120)
async def check_activity():
    role = 773634102149775390
    file = open('role_members.txt', 'r')
    member_list = [str(member.id) for member in bot.get_all_members() if not str(member.status) == 'offline' and 'TEST' in str(member.activity)]
    for line in file.readlines():
        if line[:-1] not in member_list:
            member = await bot.guilds[0].get_member(int(line))
            await member.remove_roles(role)
    a_file = open('role_members.txt', 'a')
    for member_id in member_list:
        await bot.guilds[0].get_member(int(member_id)).add_roles(role)
        a_file.write(member_id + '\n')
            
        
check_activity.start()

我该如何解决?

1 个答案:

答案 0 :(得分:1)

您可以在没有文件的情况下执行此操作,只需使用id获取行会即可。然后检查guild.members个活动。

@tasks.loop(seconds=120)
async def check_activity():
    role_name = 'Test'
    guild = bot.get_guild(123456798)
    role = discord.utils.get(guild.roles, name=role_name)
    for member in guild.members:
        try:  # if he is online.
            status = member.activities[0].name
            if role_name in status:
                await member.add_roles(role)
            else:
                await member.remove_roles(role)
        except:  # he is offline
            pass