You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
6.1 KiB

  1. import discord
  2. import json
  3. import config
  4. from helpers.restrictions import get_user_restrictions
  5. class Logs:
  6. """
  7. Logs join and leave messages, bans and unbans, and member changes.
  8. """
  9. def __init__(self, bot):
  10. self.bot = bot
  11. async def on_member_join(self, member):
  12. await self.bot.wait_until_ready()
  13. log_channel = self.bot.get_channel(config.log_channel)
  14. # We use this a lot, might as well get it once
  15. escaped_name = self.bot.escape_message(member)
  16. # Check if user account is older than 15 minutes
  17. age = member.joined_at - member.created_at
  18. if age < config.min_age:
  19. sent = True
  20. msg = f"šŸšØ **Account too new**: {member.mention} | "\
  21. f"{escaped_name}\n"\
  22. f"šŸ—“ __Creation__: {member.created_at}\n"\
  23. f"šŸ•“ Account age: {age}\n"\
  24. f"šŸ· __User ID__: {member.id}"
  25. if not sent:
  26. msg += "\nThe user has disabled direct messages,"\
  27. " so the reason was not sent."
  28. await log_channel.send(msg)
  29. return
  30. msg = f"āœ… **Join**: {member.mention} | "\
  31. f"{escaped_name}\n"\
  32. f"šŸ—“ __Creation__: {member.created_at}\n"\
  33. f"šŸ•“ Account age: {age}\n"\
  34. f"šŸ· __User ID__: {member.id}"
  35. # Handles user restrictions
  36. # Basically, gives back muted role to users that leave with it.
  37. rsts = get_user_restrictions(member.id)
  38. roles = [discord.utils.get(member.guild.roles, id=rst) for rst in rsts]
  39. await member.add_roles(*roles)
  40. # Real hell zone.
  41. with open("data/userlog.json", "r") as f:
  42. warns = json.load(f)
  43. try:
  44. if len(warns[str(member.id)]["warns"]) == 0:
  45. await log_channel.send(msg)
  46. else:
  47. embed = discord.Embed(color=discord.Color.dark_red(),
  48. title=f"Warns for {escaped_name}")
  49. embed.set_thumbnail(url=member.avatar_url)
  50. for idx, warn in enumerate(warns[str(member.id)]["warns"]):
  51. embed.add_field(name=f"{idx + 1}: {warn['timestamp']}",
  52. value=f"Issuer: {warn['issuer_name']}"
  53. f"\nReason: {warn['reason']}")
  54. await log_channel.send(msg, embed=embed)
  55. except KeyError: # if the user is not in the file
  56. await log_channel.send(msg)
  57. async def on_member_remove(self, member):
  58. await self.bot.wait_until_ready()
  59. log_channel = self.bot.get_channel(config.log_channel)
  60. msg = f"ā¬…ļø **Leave**: {member.mention} | "\
  61. f"{self.bot.escape_message(member)}\n"\
  62. f"šŸ· __User ID__: {member.id}"
  63. await log_channel.send(msg)
  64. async def on_member_ban(self, guild, member):
  65. await self.bot.wait_until_ready()
  66. log_channel = self.bot.get_channel(config.log_channel)
  67. msg = f"ā›” **Ban**: {member.mention} | "\
  68. f"{self.bot.escape_message(member)}\n"\
  69. f"šŸ· __User ID__: {member.id}"
  70. await log_channel.send(msg)
  71. async def on_member_unban(self, guild, user):
  72. await self.bot.wait_until_ready()
  73. log_channel = self.bot.get_channel(config.log_channel)
  74. msg = f"āš ļø **Unban**: {user.mention} | "\
  75. f"{self.bot.escape_message(user)}\n"\
  76. f"šŸ· __User ID__: {user.id}"
  77. # if user.id in self.bot.timebans:
  78. # msg += "\nTimeban removed."
  79. # self.bot.timebans.pop(user.id)
  80. # with open("data/timebans.json", "r") as f:
  81. # timebans = json.load(f)
  82. # if user.id in timebans:
  83. # timebans.pop(user.id)
  84. # with open("data/timebans.json", "w") as f:
  85. # json.dump(timebans, f)
  86. await log_channel.send(msg)
  87. async def on_member_update(self, member_before, member_after):
  88. await self.bot.wait_until_ready()
  89. msg = ""
  90. log_channel = self.bot.get_channel(config.log_channel)
  91. if member_before.roles != member_after.roles:
  92. # role removal
  93. role_removal = []
  94. for index, role in enumerate(member_before.roles):
  95. if role not in member_after.roles:
  96. role_removal.append(role)
  97. # role addition
  98. role_addition = []
  99. for index, role in enumerate(member_after.roles):
  100. if role not in member_before.roles:
  101. role_addition.append(role)
  102. if len(role_addition) != 0 or len(role_removal) != 0:
  103. msg += "\nšŸ‘‘ __Role change__: "
  104. roles = []
  105. for role in role_removal:
  106. roles.append("_~~" + role.name + "~~_")
  107. for role in role_addition:
  108. roles.append("__**" + role.name + "**__")
  109. for index, role in enumerate(member_after.roles):
  110. if role.name == "@everyone":
  111. continue
  112. if role not in role_removal and role not in role_addition:
  113. roles.append(role.name)
  114. msg += ", ".join(roles)
  115. if member_before.name != member_after.name:
  116. msg += "\nšŸ“ __Username change__: "\
  117. f"{self.bot.escape_message(member_before)} ā†’ "\
  118. f"{self.bot.escape_message(member_after)}"
  119. if member_before.nick != member_after.nick:
  120. if not member_before.nick:
  121. msg += "\nšŸ· __Nickname addition__"
  122. elif not member_after.nick:
  123. msg += "\nšŸ· __Nickname removal__"
  124. else:
  125. msg += "\nšŸ· __Nickname change__"
  126. msg += f": {self.bot.escape_message(member_before.nick)} ā†’ "\
  127. f"{self.bot.escape_message(member_after.nick)}"
  128. if msg:
  129. msg = f"ā„¹ļø **Member update**: {member_after.mention} | "\
  130. f"{self.bot.escape_message(member_after)}{msg}"
  131. await log_channel.send(msg)
  132. def setup(bot):
  133. bot.add_cog(Logs(bot))