Browse Source

Comptage des membres dans un channel vocal fermé. Closes #41

Squashed commit of the following:

commit 2c1e610b81
Author: l4p1n <l4p1n@l4p1n.ch>
Date:   Fri Jun 21 15:53:50 2019 +0200

    Mise à jour du nombre de membres lors de la connection du bot

commit 3f7bfa38c7
Author: l4p1n <l4p1n@l4p1n.ch>
Date:   Fri Jun 21 15:47:39 2019 +0200

    ajout de la mise à jour des comptes lorsqu'un membre arrive ou part du serveur

commit 0371e67fbc
Author: l4p1n <l4p1n@l4p1n.ch>
Date:   Fri Jun 21 15:38:38 2019 +0200

    cas où un channel de comptage a été config+

commit 65701819c4
Author: l4p1n <l4p1n@l4p1n.ch>
Date:   Fri Jun 21 15:35:45 2019 +0200

    Ajout de la désactivation du comptage dans un channel vocal

commit 301f5fbf4d
Author: l4p1n <l4p1n@l4p1n.ch>
Date:   Fri Jun 21 14:47:24 2019 +0200

    Ajout du comptage des membres dans un channel vocal
master
l4p1n 9 months ago
parent
commit
afffff4b18
Signed by untrusted user: l4p1n <l4p1n@l4p1n.ch> GPG Key ID: 8A2C2F68B58DC35C
4 changed files with 129 additions and 3 deletions
  1. +2
    -1
      app.py
  2. +116
    -0
      cmds/members_count.py
  3. +7
    -0
      db/migrations/20190620195223_add_members_count_channel_in_config.sql
  4. +4
    -2
      db/schema.sql

+ 2
- 1
app.py View File

@@ -22,7 +22,8 @@ extensions = [
'cmds.backup',
'cmds.event_utils',
'cmds.socket_server',
'cmds.scheduled_messages'
'cmds.scheduled_messages',
'cmds.members_count'
]
if __name__ == '__main__':
bot = BotClass([config.BOT_COMMAND_PREFIX])


+ 116
- 0
cmds/members_count.py View File

@@ -0,0 +1,116 @@
from discord.ext import commands, tasks

import discord

from lib.BotClass import BotClass
from lib.config_manager import ConfigManager
from lib import database


class MembersCount(commands.Cog):
def __init__(self, bot: BotClass):
self.members_count_string = '{} membres'
self.bot = bot
self.query_update = 'UPDATE config SET members_count_channel_id = $1 WHERE server_id = $2'

@commands.Cog.listener()
async def on_ready(self):
db = await database.get_database()
rows = await db.fetch('SELECT members_count_channel_id FROM config WHERE members_count_channel_id IS NOT NULL')

for guild_config in rows:
channel: discord.VoiceChannel = self.bot.get_channel(guild_config['members_count_channel_id'])

if channel is None:
return

await channel.edit(name=self.members_count_string.format(channel.guild.member_count))


@commands.Cog.listener()
async def on_member_join(self, member: discord.Member):
await self._do_channel_name_update(member)

@commands.Cog.listener()
async def on_member_remove(self, member: discord.Member):
await self._do_channel_name_update(member)

@commands.group()
@commands.has_role('l4p1n-commander')
async def users_counter(self, ctx: commands.Context):
if ctx.invoked_subcommand is None:
return await ctx.send('Comptage des membres dans la liste des channels\n'
'`start`: Crée un channel vocal fermé et commence à compter les membres\n'
'`stop`: Supprime le channel et arrête le comptage')

@users_counter.command()
@commands.has_role('l4p1n-commander')
@commands.bot_has_permissions(manage_channels=True)
async def start(self, ctx: commands.Context):
guild: discord.Guild = ctx.guild
configured_channel = await ConfigManager.get('members_count_channel_id', guild.id)

if configured_channel is not None:
return await ctx.send('Le comptage des membres a déjà été configuré')

member_count = guild.member_count

try:
voice: discord.VoiceChannel = await guild.create_voice_channel(
self.members_count_string.format(member_count),
overwrites={
guild.default_role: discord.PermissionOverwrite(connect=False),
guild.me: discord.PermissionOverwrite(manage_channels=True)
})

await voice.edit(position=0)
except discord.Forbidden:
return await ctx.send('Ne peut pas créer le channel vocal fermé: permissions insuffisantes')

db = await database.get_database()

await db.execute(
self.query_update,
voice.id, guild.id
)

await ctx.send(':white_check_mark: Le channel a été créé avec succès')

@users_counter.command()
@commands.has_role('l4p1n-commander')
async def stop(self, ctx: commands.Context):
guild: discord.Guild = ctx.guild

channel_id = await ConfigManager.get('members_count_channel_id', guild.id)
db = await database.get_database()

if channel_id is None:
return await ctx.send('Aucun channel de comptage n\'a été configuré')

channel: discord.VoiceChannel = guild.get_channel(channel_id)

if channel is None:
await db.execute(self.query_update, None, guild.id)
return await ctx.send('Le channel de comptage a déjà été supprimé. Edition de la configuration uniquement')

await channel.delete()
await db.execute(self.query_update, None, guild.id)
await ctx.send('Le compteur des membres a été arrêté')

async def _do_channel_name_update(self, member: discord.Member):
guild: discord.Guild = member.guild
channel_id = await ConfigManager.get('members_count_channel_id', guild.id)

if channel_id is None:
return

channel: discord.VoiceChannel = guild.get_channel(channel_id)

if channel is None:
return

await channel.edit(name=self.members_count_string.format(guild.member_count))


def setup(bot: BotClass):
bot.add_cog(MembersCount(bot))

+ 7
- 0
db/migrations/20190620195223_add_members_count_channel_in_config.sql View File

@@ -0,0 +1,7 @@
-- migrate:up
ALTER TABLE config
ADD COLUMN members_count_channel_id BIGINT DEFAULT NULL;

-- migrate:down
ALTER TABLE config
DROP COLUMN members_count_channel_id;

+ 4
- 2
db/schema.sql View File

@@ -84,7 +84,8 @@ CREATE TABLE public.config (
auto_channel bigint,
auto_role bigint,
log_events boolean DEFAULT false NOT NULL,
log_events_type integer DEFAULT '-1'::integer NOT NULL
log_events_type integer DEFAULT '-1'::integer NOT NULL,
members_count_channel_id bigint
);


@@ -429,4 +430,5 @@ INSERT INTO public.schema_migrations (version) VALUES
('20190514185144'),
('20190514185729'),
('20190530163611'),
('20190531190905');
('20190531190905'),
('20190620195223');

Loading…
Cancel
Save