info_on_leave.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #Gives supervisors an information, when a department member leaves the server.
  8. class infoleaving(commands.Cog):
  9. def __init__(self, bot: discord.Bot):
  10. self.bot = bot
  11. def _load_config(self):
  12. config = configparser.ConfigParser()
  13. configFilePath = r'config.cfg'
  14. config.read(configFilePath)
  15. return config
  16. def _get_info_channel(self):
  17. config = self._load_config()
  18. info_channel_id = int(config["Einweisung"]["info_channel_id"])
  19. info_channel = self.bot.get_channel(info_channel_id)
  20. if info_channel is None:
  21. print(f"Log channel with ID {info_channel_id} not found.")
  22. return None
  23. return info_channel
  24. @commands.Cog.listener()
  25. async def on_member_remove(self, member):
  26. config = self._load_config()
  27. enable_info = config.getboolean("Einweisung","enable_channel_info_on_leaving")
  28. if not enable_info:
  29. return
  30. info_channel = self._get_info_channel()
  31. if info_channel is None:
  32. return
  33. department_roles_ids = [int(role_id.strip()) for role_id in config.get("Role Management", "department1_ranks").split(",")]
  34. department_roles = [member.guild.get_role(role_id) for role_id in department_roles_ids]
  35. if any(role.id in department_roles for role in member.roles):
  36. embed = discord.Embed(
  37. title="Departmentmeber left the server.",
  38. description=f"{member.mention} has left the server. Name: {member.name}, {member.nick}, ID: {member.id}",
  39. color=discord.Color.dark_red(),
  40. timestamp=discord.utils.utcnow()
  41. )
  42. embed.set_footer(text=f"User ID: {member.id}")
  43. await info_channel.send(embed=embed)
  44. else:
  45. return
  46. def setup(bot: discord.Bot):
  47. bot.add_cog(infoleaving(bot))