info_on_leave.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. if any(role.id in department_roles_ids for role in member.roles):
  35. embed = discord.Embed(
  36. title="Department Member left the server.",
  37. description=f"{member.mention} has left the server. \n Name: {member.nick}, ID: {member.id}",
  38. color=discord.Color.orange(),
  39. timestamp=discord.utils.utcnow()
  40. )
  41. embed.set_footer(text=f"User ID: {member.id}")
  42. await info_channel.send(embed=embed)
  43. else:
  44. return
  45. def setup(bot: discord.Bot):
  46. bot.add_cog(infoleaving(bot))