[英]discord py how can i give a role to server member user with id
Explanation about what my code does: my code checks if a member on my server has the specific keyword "TEST" in her activity and gives a rank for it to the user.关于我的代码做什么的解释:我的代码检查我服务器上的成员在她的活动中是否有特定的关键字“TEST”,并为用户提供一个排名。 And if the keyword gets removed from the activity, the rank gets removed too.
如果关键字从活动中删除,排名也会被删除。 Offline members are not affected by this.
线下会员不受此影响。 So I had to save the member is in a file.
所以我不得不将成员保存在一个文件中。
Now i have a problem:现在我有一个问题:
await bot.guilds[0].get_member(int(member_id)).add_roles(role)
AttributeError: 'int' object has no attribute 'id'
And thats my code:这就是我的代码:
@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()
How can I fix this?我怎样才能解决这个问题?
You can do this without files, simply get the guild using id.您可以在没有文件的情况下执行此操作,只需使用 id 获取公会即可。 Then check for
guild.members
activities.然后检查
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.