logs.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import discord
  2. from discord.ext import commands
  3. from discord.commands import Option
  4. from discord.commands import slash_command
  5. import configparser
  6. import time
  7. class actionlog(commands.Cog):
  8. def __init__(self, bot: discord.Bot):
  9. self.bot = bot
  10. #Delted Message Log
  11. @commands.Cog.listener()
  12. async def on_message_delete(self, message):
  13. config = configparser.ConfigParser()
  14. configFilePath = r'config.cfg'
  15. config.read_file(open(configFilePath))
  16. enable_log = config.getboolean("Logs","enable_action_log")
  17. if enable_log == "false":
  18. return
  19. log_channel_id = int(config["Logs"]["action_log"])
  20. log_channel = self.bot.get_channel(log_channel_id)
  21. if message.author.bot:
  22. return
  23. else:
  24. embed = discord.Embed(
  25. title="Message Deleted",
  26. description=f"A message by {message.author.mention} was deleted in {message.channel.mention}.",
  27. color=discord.Color.red(),
  28. timestamp=message.created_at
  29. )
  30. embed.add_field(name="Message Content", value=message.content or "No content", inline=False)
  31. embed.set_footer(text=f"User ID: {message.author.id} | Message ID: {message.id}")
  32. await log_channel.send(embed=embed)
  33. #Edited Message Log
  34. @commands.Cog.listener()
  35. async def on_message_edit(self, before, after):
  36. config = configparser.ConfigParser()
  37. configFilePath = r'config.cfg'
  38. config.read_file(open(configFilePath))
  39. enable_log = config.getboolean("Logs","enable_action_log")
  40. if enable_log == "false":
  41. return
  42. log_channel_id = int(config["Logs"]["action_log"])
  43. log_channel = self.bot.get_channel(log_channel_id)
  44. if before.author.bot:
  45. return
  46. else:
  47. embed = discord.Embed(
  48. title="Message Edited",
  49. description=f"A message by {before.author.mention} was edited in {before.channel.mention}.",
  50. color=discord.Color.orange(),
  51. timestamp=after.edited_at or discord.utils.utcnow()
  52. )
  53. embed.add_field(name="Before", value=before.content or "No content", inline=False)
  54. embed.add_field(name="After", value=after.content or "No content", inline=False)
  55. embed.set_footer(text=f"User ID: {before.author.id} | Message ID: {before.id}")
  56. await log_channel.send(embed=embed)
  57. #Member Join Log
  58. @commands.Cog.listener()
  59. async def on_member_join(self, member):
  60. config = configparser.ConfigParser()
  61. configFilePath = r'config.cfg'
  62. config.read_file(open(configFilePath))
  63. enable_log = config.getboolean("Logs","enable_action_log")
  64. if enable_log == "false":
  65. return
  66. log_channel_id = int(config["Logs"]["action_log"])
  67. log_channel = self.bot.get_channel(log_channel_id)
  68. embed = discord.Embed(
  69. title="Member Joined",
  70. description=f"{member.mention} has joined the server.",
  71. color=discord.Color.green(),
  72. timestamp=discord.utils.utcnow()
  73. )
  74. embed.set_footer(text=f"User ID: {member.id}")
  75. await log_channel.send(embed=embed)
  76. #Member Leave Log
  77. @commands.Cog.listener()
  78. async def on_member_remove(self, member):
  79. config = configparser.ConfigParser()
  80. configFilePath = r'config.cfg'
  81. config.read_file(open(configFilePath))
  82. enable_log = config.getboolean("Logs","enable_action_log")
  83. if enable_log == "false":
  84. return
  85. log_channel_id = int(config["Logs"]["action_log"])
  86. log_channel = self.bot.get_channel(log_channel_id)
  87. embed = discord.Embed(
  88. title="Member Left",
  89. description=f"{member.mention} has left the server.",
  90. color=discord.Color.dark_red(),
  91. timestamp=discord.utils.utcnow()
  92. )
  93. embed.set_footer(text=f"User ID: {member.id}")
  94. await log_channel.send(embed=embed)
  95. #Role Update Log
  96. @commands.Cog.listener()
  97. async def on_guild_role_update(self, before, after):
  98. config = configparser.ConfigParser()
  99. configFilePath = r'config.cfg'
  100. config.read_file(open(configFilePath))
  101. enable_log = config.getboolean("Logs","enable_action_log")
  102. if enable_log == "false":
  103. return
  104. log_channel_id = int(config["Logs"]["action_log"])
  105. log_channel = self.bot.get_channel(log_channel_id)
  106. embed = discord.Embed(
  107. title="Role Updated",
  108. description=f"The role **{before.name}** has been updated.",
  109. color=discord.Color.blue(),
  110. timestamp=discord.utils.utcnow()
  111. )
  112. embed.add_field(name="Before", value=f"Name: {before.name}\nColor: {before.color}\nPermissions: {before.permissions}", inline=False)
  113. embed.add_field(name="After", value=f"Name: {after.name}\nColor: {after.color}\nPermissions: {after.permissions}", inline=False)
  114. embed.set_footer(text=f"Role ID: {before.id}")
  115. await log_channel.send(embed=embed)
  116. #Role added log
  117. @commands.Cog.listener()
  118. async def on_guild_role_create(self, role):
  119. config = configparser.ConfigParser()
  120. configFilePath = r'config.cfg'
  121. config.read_file(open(configFilePath))
  122. enable_log = config.getboolean("Logs","enable_action_log")
  123. if enable_log == "false":
  124. return
  125. log_channel_id = int(config["Logs"]["action_log"])
  126. log_channel = self.bot.get_channel(log_channel_id)
  127. embed = discord.Embed(
  128. title="Role Created",
  129. description=f"The role **{role.name}** has been created.",
  130. color=discord.Color.green(),
  131. timestamp=discord.utils.utcnow()
  132. )
  133. embed.set_footer(text=f"Role ID: {role.id}")
  134. await log_channel.send(embed=embed)
  135. #Role deleted log
  136. @commands.Cog.listener()
  137. async def on_guild_role_delete(self, role):
  138. config = configparser.ConfigParser()
  139. config.read("config.ini")
  140. enable_log = config.getboolean("Logs","enable_action_log")
  141. if enable_log == "false":
  142. return
  143. log_channel_id = int(config["Logs"]["action_log"])
  144. log_channel = self.bot.get_channel(log_channel_id)
  145. embed = discord.Embed(
  146. title="Role Deleted",
  147. description=f"The role **{role.name}** has been deleted.",
  148. color=discord.Color.red(),
  149. timestamp=discord.utils.utcnow()
  150. )
  151. embed.set_footer(text=f"Role ID: {role.id}")
  152. await log_channel.send(embed=embed)
  153. #User Role update log
  154. @commands.Cog.listener()
  155. async def on_member_update(self, before, after):
  156. config = configparser.ConfigParser()
  157. configFilePath = r'config.cfg'
  158. config.read_file(open(configFilePath))
  159. enable_log = config.getboolean("Logs","enable_action_log")
  160. if enable_log == "false":
  161. return
  162. log_channel_id = int(config["Logs"]["action_log"])
  163. log_channel = self.bot.get_channel(log_channel_id)
  164. before_roles = set(before.roles)
  165. after_roles = set(after.roles)
  166. added_roles = after_roles - before_roles
  167. removed_roles = before_roles - after_roles
  168. for role in added_roles:
  169. embed = discord.Embed(
  170. title="Role Added",
  171. description=f"The role **{role.name}** has been added to {after.mention}.",
  172. color=discord.Color.green(),
  173. timestamp=discord.utils.utcnow()
  174. )
  175. embed.set_footer(text=f"User ID: {after.id} | Role ID: {role.id}")
  176. await log_channel.send(embed=embed)
  177. for role in removed_roles:
  178. embed = discord.Embed(
  179. title="Role Removed",
  180. description=f"The role **{role.name}** has been removed from {after.mention}.",
  181. color=discord.Color.red(),
  182. timestamp=discord.utils.utcnow()
  183. )
  184. embed.set_footer(text=f"User ID: {after.id} | Role ID: {role.id}")
  185. await log_channel.send(embed=embed)
  186. def setup(bot: discord.Bot):
  187. bot.add_cog(actionlog(bot))