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.

117 lines
4.2KB

  1. from discord.ext import commands, tasks
  2. import discord
  3. from lib.BotClass import BotClass
  4. from lib.config_manager import ConfigManager
  5. from lib import database
  6. class MembersCount(commands.Cog):
  7. def __init__(self, bot: BotClass):
  8. self.members_count_string = '{} membres'
  9. self.bot = bot
  10. self.query_update = 'UPDATE config SET members_count_channel_id = $1 WHERE server_id = $2'
  11. @commands.Cog.listener()
  12. async def on_ready(self):
  13. db = await database.get_database()
  14. rows = await db.fetch('SELECT members_count_channel_id FROM config WHERE members_count_channel_id IS NOT NULL')
  15. for guild_config in rows:
  16. channel: discord.VoiceChannel = self.bot.get_channel(guild_config['members_count_channel_id'])
  17. if channel is None:
  18. return
  19. await channel.edit(name=self.members_count_string.format(channel.guild.member_count))
  20. @commands.Cog.listener()
  21. async def on_member_join(self, member: discord.Member):
  22. await self._do_channel_name_update(member)
  23. @commands.Cog.listener()
  24. async def on_member_remove(self, member: discord.Member):
  25. await self._do_channel_name_update(member)
  26. @commands.group()
  27. @commands.has_role('l4p1n-commander')
  28. async def users_counter(self, ctx: commands.Context):
  29. if ctx.invoked_subcommand is None:
  30. return await ctx.send('Comptage des membres dans la liste des channels\n'
  31. '`start`: Crée un channel vocal fermé et commence à compter les membres\n'
  32. '`stop`: Supprime le channel et arrête le comptage')
  33. @users_counter.command()
  34. @commands.has_role('l4p1n-commander')
  35. @commands.bot_has_permissions(manage_channels=True)
  36. async def start(self, ctx: commands.Context):
  37. guild: discord.Guild = ctx.guild
  38. configured_channel = await ConfigManager.get('members_count_channel_id', guild.id)
  39. if configured_channel is not None:
  40. return await ctx.send('Le comptage des membres a déjà été configuré')
  41. member_count = guild.member_count
  42. try:
  43. voice: discord.VoiceChannel = await guild.create_voice_channel(
  44. self.members_count_string.format(member_count),
  45. overwrites={
  46. guild.default_role: discord.PermissionOverwrite(connect=False),
  47. guild.me: discord.PermissionOverwrite(manage_channels=True)
  48. })
  49. await voice.edit(position=0)
  50. except discord.Forbidden:
  51. return await ctx.send('Ne peut pas créer le channel vocal fermé: permissions insuffisantes')
  52. db = await database.get_database()
  53. await db.execute(
  54. self.query_update,
  55. voice.id, guild.id
  56. )
  57. await ctx.send(':white_check_mark: Le channel a été créé avec succès')
  58. @users_counter.command()
  59. @commands.has_role('l4p1n-commander')
  60. async def stop(self, ctx: commands.Context):
  61. guild: discord.Guild = ctx.guild
  62. channel_id = await ConfigManager.get('members_count_channel_id', guild.id)
  63. db = await database.get_database()
  64. if channel_id is None:
  65. return await ctx.send('Aucun channel de comptage n\'a été configuré')
  66. channel: discord.VoiceChannel = guild.get_channel(channel_id)
  67. if channel is None:
  68. await db.execute(self.query_update, None, guild.id)
  69. return await ctx.send('Le channel de comptage a déjà été supprimé. Edition de la configuration uniquement')
  70. await channel.delete()
  71. await db.execute(self.query_update, None, guild.id)
  72. await ctx.send('Le compteur des membres a été arrêté')
  73. async def _do_channel_name_update(self, member: discord.Member):
  74. guild: discord.Guild = member.guild
  75. channel_id = await ConfigManager.get('members_count_channel_id', guild.id)
  76. if channel_id is None:
  77. return
  78. channel: discord.VoiceChannel = guild.get_channel(channel_id)
  79. if channel is None:
  80. return
  81. await channel.edit(name=self.members_count_string.format(guild.member_count))
  82. def setup(bot: BotClass):
  83. bot.add_cog(MembersCount(bot))