From d3289a0d658fd7d0967bd98517870d4544d5a017 Mon Sep 17 00:00:00 2001 From: SonOfLope Date: Fri, 19 Jul 2024 15:46:12 -0400 Subject: [PATCH] adds new command to show server configs and also file cleanup --- .github/workflows/ci.yml | 2 +- docker-compose-dev.yml => docker-compose.yml | 0 events.py | 6 +-- slash_commands.py | 21 ++++++++-- .../docker-compose.yml | 0 text_format.py | 6 ++- trema_database.py | 38 ++++++++++++++++++- 7 files changed, 62 insertions(+), 11 deletions(-) rename docker-compose-dev.yml => docker-compose.yml (100%) rename docker-compose-tests.yml => tests/docker-compose.yml (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9abc10f..20077dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: uses: actions/checkout@v2 - name: Start containers - run: docker-compose -f "docker-compose-tests.yml" up -d + run: docker-compose -f "tests/docker-compose.yml" up -d - name: Setup Python uses: actions/setup-python@v2 diff --git a/docker-compose-dev.yml b/docker-compose.yml similarity index 100% rename from docker-compose-dev.yml rename to docker-compose.yml diff --git a/events.py b/events.py index 0eb2028..335f008 100644 --- a/events.py +++ b/events.py @@ -48,13 +48,11 @@ async def on_member_remove(member): guild_id = guild.id leave_msg = trema_db.get_server_leave_msg(guild_id) if leave_msg is not None: + mention_dict = generate_mention_dict(guild, member) + leave_msg = make_mention(leave_msg, mention_dict) welcome_chan = _get_welcome_chan(guild, trema_db) - leave_msg = make_mention(leave_msg, member) await welcome_chan.send(leave_msg) - - - @trema_bot.event async def on_ready(): logger.info('{0.user} fonctionne.'.format(trema_bot)) \ No newline at end of file diff --git a/slash_commands.py b/slash_commands.py index fe1c95e..59767f3 100644 --- a/slash_commands.py +++ b/slash_commands.py @@ -118,7 +118,7 @@ def post_to_calidum(sender_username, request_service, request_details): def create_slash_cmds(trema_bot, trema_db, start_time, github_token): config = _create_config_cmds(trema_db) _create_config_reminder_cmds(trema_db, config) - _create_information_cmds(trema_bot, start_time) + _create_information_cmds(trema_bot, start_time, trema_db) _create_management_cmds(trema_bot, trema_db) webhook = _create_webhooks_cmds(trema_db) request = _create_requests_cmds(trema_db, github_token) @@ -198,7 +198,7 @@ async def aide(ctx): + "d'un paramètre.\n\n"\ + "Certains paramètres sont des messages affichés après un évènement concernant "\ + "un membre particulier. Pour mentionner ce membre, écrivez **@-** dans ces messages. "\ - + "Le signe **[@-]** au début d'une description indique que cette action est possible." + + "Le signe **[@-]** au début d'une description indique que cette action est possible." help_embed = Embed( title=embed_title, description=instructions, @@ -406,7 +406,7 @@ async def config_reminder_delay(ctx, await ctx.respond(embed=response_embed, ephemeral=True) -def _create_information_cmds(trema_bot, start_time): +def _create_information_cmds(trema_bot, start_time, trema_db): @trema_bot.command(name="ping", description="Répond avec pong") async def ping(ctx): latency = round(trema_bot.latency * 1000) @@ -440,6 +440,21 @@ async def info(ctx): await ctx.respond(embed=help_embed) + + @trema_bot.command(name="show_server_config", description="Affiche toutes les configurations du serveur") + @is_authorized(trema_db) + async def show_server_config(ctx): + guild_id = ctx.guild_id + config_values = trema_db.get_all_server_configs(guild_id) + + response_embed = Embed( + title="Configurations du Serveur", + description=config_values, + color=Color.green() + ) + + await ctx.respond(embed=response_embed, ephemeral=True) + def _create_management_cmds(trema_bot, trema_db): @trema_bot.command(name="annonce", description="Informations sur Trëma") diff --git a/docker-compose-tests.yml b/tests/docker-compose.yml similarity index 100% rename from docker-compose-tests.yml rename to tests/docker-compose.yml diff --git a/text_format.py b/text_format.py index 2ff3cde..0a7ff4e 100644 --- a/text_format.py +++ b/text_format.py @@ -19,10 +19,12 @@ def make_mention(text, mention_dict): Returns: str: the given text with mentions replaced. """ - for placeholder, mention in mention_dict.items(): - text = text.replace(placeholder, mention) + for placeholder in list(mention_dict.keys()): + if placeholder in text: + text = text.replace(placeholder, mention_dict.get(placeholder, "{mention non trouvé}")) return text + def generate_mention_dict(guild, newMember = None): mention_dict = { '{server}': guild.name, diff --git a/trema_database.py b/trema_database.py index a07c499..a78c671 100644 --- a/trema_database.py +++ b/trema_database.py @@ -89,9 +89,12 @@ def _get_collection(self, collection_name): def _get_document_attr(self, collection, doc_id, attr_key): collection = self._ensure_col_is_obj(collection) document = collection.find_one({"_id": doc_id}) + if document is None: + return None attr_value = document.get(attr_key) return attr_value + def get_leave_msg(self, welcome_id): leave_msg = self._get_welcome_attr(welcome_id, "leave_msg") return leave_msg @@ -267,8 +270,10 @@ def create_webhook(self, webhookName, channelID, unique_url, guild_id): def get_all_webhooks(self, guild_id): server_collection = self._get_collection("server") server_doc = server_collection.find_one({"_id": guild_id}) + if server_doc is None: + return [] return server_doc.get("webhooks", []) - + def delete_webhook(self, webhookName, guild_id): server_collection = self._get_collection("server") server_collection.update_one( @@ -345,6 +350,37 @@ def delete_request(self, guild_id, request_id): return False result = requests_collection.delete_one({"guild_id": guild_id, "_id": oid}) return result.deleted_count > 0 + + def get_all_server_configs(self, server_id): + def safe_get(attr_value, default="Non configuré"): + return default if attr_value is None else attr_value + + configs = { + "Nom du serveur": safe_get(self._get_server_attr(server_id, "name")), + "Rôle d'administrateur": safe_get(self._get_server_attr(server_id, "admin_role")), + "Date d'adhésion": safe_get(self._get_server_attr(server_id, "joined_at")), + "ID du canal d'annonces": safe_get(self._get_server_attr(server_id, "announce_chan_id")), + "ID du canal de bienvenue": safe_get(self.get_server_welcome_chan_id(server_id)), + "Message de bienvenue": safe_get(self.get_server_welcome_msg(server_id)), + "Message de départ": safe_get(self.get_server_leave_msg(server_id)), + "Message de rappel": safe_get(self.get_server_reminder_msg(server_id)), + "Délai de rappel (minutes)": safe_get(self.get_server_reminder_delay(server_id), default=0) // 60, + "Webhooks": safe_get(self.get_all_webhooks(server_id), default=[]), + } + + formatted_configs = [] + for key, value in configs.items(): + if isinstance(value, list): + if value: + formatted_value = "\n".join([f"- {item}" for item in value]) + else: + formatted_value = "Aucun" + else: + formatted_value = value + + formatted_configs.append(f"**{key}**: {formatted_value}") + + return "\n\n".join(formatted_configs) mongo_user = os.getenv('MONGO_USER') mongo_password = os.getenv('MONGO_PASSWORD')