I wanted my bot to print the permissions that were added and removed but my bot is not returning anything even if I changed the permissions
Here is my code:
async def on_server_role_update(before, after):
list_a = []
list_b = []
list_a.append(str([p[0] for p in before.permissions]))
list_b.append(str([p[0] for p in after.permissions]))
dif_add = set(list_b).difference(list_a)
dif_rem = set(list_a).difference(list_b)
print("Added Permissons: " + ", ".join(dif_add))
print("Removed Permissons: " + ", ".join(dif_rem))
First of all in the updated discord py it's not on_server_role_update
but on_guild_role_update
, old version is not supported anymore so I would advise you to update.
Second, try printing list_a
and list_b
after they are set, you'll see that you only get a list of all permission names: ['add_reactions', 'administrator', 'attach_files', 'ban_members', ....
how would you compare that?
Also you're missing quite crucial statement:
if before.permissions != after.permissions:
You need that because on_server_role_update
aka on_guild_role_update
will trigger for multiple events like name change, description change, mentionable change, position change, color change etc
So for a nice clean code:
if before.permissions != after.permissions:
diff = list(set(after.permissions).difference(set(before.permissions)))
# diff will be a list which has changed roles and their state AFTER the change, example:
# [('manage_channels', True), ('view_audit_log', True), ('manage_roles', True), ('manage_guild', False)]
for changed_perm in diff:
print(f"Changed role permission {changed_perm[0]} to {changed_perm[1]}")
Example output:
Changed role permission manage_channels to True
Changed role permission view_audit_log to True
Changed role permission manage_roles to True
Changed role permission manage_guild to False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.