我正在使用discord.py重写。我花了三天的时间使这两行代码正常工作:
await member.remove_roles(tryMember, tryRole)
.
.
.
await member.add_roles(tryMember, tryRole)
这是周围的代码,包括我的疑难解答。我有要使用的用户ID#和角色ID#。这是一个后台过程。
memberList = []
for member in guild.members:
...
... decide on one of 3 roles for the user as proposedRole. # role ID number
...
for roleID in (serverSettings["inactive_r"], serverSettings["active_r"], serverSettings["very_active_r"]):
if roleID != proposedRole:
try:
print (roleID)
tryRole = discord.utils.find(lambda r: r.id == roleID, guild.roles)
tryMember = discord.utils.find(lambda m: m.id == member.id, guild.members)
print (tryRole) #only prints a member's name#number
print (tryMember) #only prints the role name
await member.remove_roles(tryMember, tryRole)
print (f'Removed {tryRole} from {tryMember} in {guild.name}')
except Exception as e:
print ('There was an error running this command: ' + str(e))
... And repeat all this but adding the right role instead of removing the inocrrect roles
每个成员在控制台上打印的内容是这样的:
709425978044186725 # the correct role ID
inactive # the correct role name (I was expecting an object)
ArdenFutura#2469 # the correct member name (I was expecting an object)
There was an error running this command: 404 Not Found (error code: 10011): Unknown Role
# the e exception
664856898838855681 #aaaaand repeat...
active
ArdenFutura#2469
There was an error running this command: 404 Not Found (error code: 10011): Unknown Role
726974763649728593
very active
ArdenFutura#2469
There was an error running this command: 404 Not Found (error code: 10011): Unknown Role
这两行代码已经进行了3天。请帮忙。
答案 0 :(得分:1)
您可能会对API参考的older version感到困惑。 remove_roles
过去有两个参数:
remove_roles(member, *roles)
但是,new documentation指出仅需要一个参数:
remove_roles(*roles, reason=None, atomic=True)
roles
扮演一个角色。如果要删除多个角色,则将传入在列表之前带有星号的角色列表(remove_roles(*[roleOne, roleTwo])
)。
reason
和atomic
是str和bool类型的可选参数。
对于您而言,您正在尝试从tryRole
中删除tryMember
。结果将是:
await tryMember.remove_roles(tryRole)