lunarflu HF staff commited on
Commit
70a5666
1 Parent(s): f42eedc

big updates to roles; track who creates/deletes + info on permission changes

Browse files
Files changed (1) hide show
  1. app.py +72 -4
app.py CHANGED
@@ -423,20 +423,41 @@ async def on_guild_channel_delete(channel):
423
  async def on_guild_role_create(role):
424
  try:
425
  # creating roles
 
 
 
 
 
 
 
426
  embed = Embed(description=f'Role {role.mention} was created', color=Color.green())
 
 
 
427
  await bot.log_channel.send(embed=embed)
 
428
  except Exception as e:
429
- print(f"on_guild_role_create Error: {e}")
 
430
 
431
-
432
  @bot.event
433
  async def on_guild_role_delete(role):
434
  try:
435
  # deleting roles, should ping @alerts for this
 
 
 
 
 
 
 
436
  embed = Embed(description=f'Role {role.name} ({role.mention}) was deleted', color=Color.red())
 
 
437
  await bot.log_channel.send(embed=embed)
 
438
  except Exception as e:
439
- print(f"on_guild_role_delete Error: {e}")
440
 
441
 
442
  @bot.event
@@ -455,7 +476,54 @@ async def on_guild_role_update(before, after):
455
  except Exception as e:
456
  print(f"on_guild_role_update Error: {e}")
457
 
458
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
459
  @bot.event
460
  async def on_voice_state_update(member, before, after):
461
  try:
 
423
  async def on_guild_role_create(role):
424
  try:
425
  # creating roles
426
+ async for entry in role.guild.audit_logs(action=discord.AuditLogAction.role_create, limit=5):
427
+ if entry.target.id == role.id:
428
+ creator = entry.user
429
+ break
430
+ else:
431
+ creator = None
432
+
433
  embed = Embed(description=f'Role {role.mention} was created', color=Color.green())
434
+ embed.add_field(name="Role Name", value=role.name, inline=True)
435
+ embed.add_field(name="Created By", value=creator.mention if creator else "Unknown", inline=True)
436
+ embed.set_footer(text=f"Role ID: {role.id}")
437
  await bot.log_channel.send(embed=embed)
438
+
439
  except Exception as e:
440
+ print(f"on_guild_role_create Error: {e}")
441
+
442
 
 
443
  @bot.event
444
  async def on_guild_role_delete(role):
445
  try:
446
  # deleting roles, should ping @alerts for this
447
+ async for entry in role.guild.audit_logs(action=discord.AuditLogAction.role_delete, limit=5):
448
+ if entry.target.id == role.id:
449
+ deleter = entry.user
450
+ break
451
+ else:
452
+ deleter = None
453
+
454
  embed = Embed(description=f'Role {role.name} ({role.mention}) was deleted', color=Color.red())
455
+ embed.add_field(name="Deleted By", value=deleter.mention if deleter else "Unknown", inline=True)
456
+ embed.set_footer(text=f"Role ID: {role.id}")
457
  await bot.log_channel.send(embed=embed)
458
+
459
  except Exception as e:
460
+ print(f"on_guild_role_delete Error: {e}")
461
 
462
 
463
  @bot.event
 
476
  except Exception as e:
477
  print(f"on_guild_role_update Error: {e}")
478
 
479
+
480
+
481
+
482
+
483
+
484
+ @bot.event
485
+ async def on_guild_role_update(before, after):
486
+ try:
487
+ # Track name changes
488
+ if before.name != after.name:
489
+ async for entry in after.guild.audit_logs(action=discord.AuditLogAction.role_update, limit=5):
490
+ if entry.target.id == after.id and 'name' in entry.changes:
491
+ changer = entry.user
492
+ break
493
+ else:
494
+ changer = None
495
+
496
+ embed = Embed(description=f'Role {before.mention} was renamed to {after.name}', color=Color.orange())
497
+ embed.add_field(name="Changed By", value=changer.mention if changer else "Unknown", inline=True)
498
+ await bot.log_channel.send(embed=embed)
499
+
500
+ if before.permissions != after.permissions:
501
+ # Find the user who changed the permissions
502
+ async for entry in after.guild.audit_logs(action=discord.AuditLogAction.role_update, limit=5):
503
+ if entry.target.id == after.id:
504
+ changer = entry.user
505
+ break
506
+ else:
507
+ changer = None
508
+
509
+ # what changed?
510
+ changed_permissions = []
511
+ for perm, value in after.permissions:
512
+ if getattr(before.permissions, perm) != value:
513
+ change_status = "enabled" if value else "disabled"
514
+ changed_permissions.append(f"{perm.replace('_', ' ').title()}: {change_status}")
515
+
516
+ embed = Embed(color=Color.red() if "administrator" in changed_permissions else Color.orange())
517
+ embed.set_author(name=f"{after.name} Role Updated", icon_url=after.guild.icon.url if after.guild.icon else "")
518
+ embed.add_field(name="Changed By", value=changer.mention if changer else "Unknown", inline=True)
519
+ embed.add_field(name="Changes", value="\n".join(changed_permissions) if changed_permissions else "No permissions changed", inline=False)
520
+ embed.set_footer(text=f"Role ID: {after.id}")
521
+ await bot.log_channel.send(embed=embed)
522
+
523
+ except Exception as e:
524
+ print(f"on_guild_role_update Error: {e}")
525
+
526
+
527
  @bot.event
528
  async def on_voice_state_update(member, before, after):
529
  try: