From 04c44bc470d578e824fa05b3d05c6ded75991291 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 16 May 2024 21:37:10 -0400 Subject: [PATCH] only trigger updating clan messages in updated directories. Update clan info message if the template was updated. --- cogs/clan_messages/__init__.py | 29 ++++++++++++++++++++++++++--- utils.py | 5 +++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/cogs/clan_messages/__init__.py b/cogs/clan_messages/__init__.py index 209e27b..3db7212 100644 --- a/cogs/clan_messages/__init__.py +++ b/cogs/clan_messages/__init__.py @@ -10,8 +10,9 @@ from fastapi import FastAPI from git import Repo from nextcord.ext import commands +from classes.gitea_webhook import GiteaWebhook from config import DakinyConfig -from utils import REPO_DIR, update_clan_messages +from utils import REPO_DIR, edit_clan_council_info, update_clan_messages app = FastAPI() REPO_URL = "https://gitea.cheezers.org/imcompiling/dakiny-messages.git" @@ -38,16 +39,38 @@ class ClanMessages(commands.Cog, name="Clan Messages"): server = uvicorn.Server(config) await server.serve() - async def update_messages(self): + async def update_messages(self, hook_info: GiteaWebhook): """ Pulls from git and then calls method to update all the discord messages as described by the files in the git repo. """ + # Get all modified/added files + modified_files: list[str] = [ + file + for list in [commit.modified + commit.added for commit in hook_info.commits] + for file in list + ] + + template_updates = { + file.split("/")[1] for file in modified_files if "message_templates" in file + } + channel_updates = { + file.split("/")[1] + for file in modified_files + if file.split("/")[1] not in template_updates + } + self.origin.pull() guild: nextcord.Guild = self.bot.get_guild(self.config.params.guild_id) - await update_clan_messages(guild) + await update_clan_messages(guild, channel_updates) + + # if the council members template has changed, update the message + if "council_members.txt" in template_updates: + await edit_clan_council_info( + self.bot.get_guild(self.config.params.guild_id), self.config + ) return {"message": "Successfully updated all discord messages."} diff --git a/utils.py b/utils.py index d10796a..d42a605 100644 --- a/utils.py +++ b/utils.py @@ -4,6 +4,7 @@ File contains util functions. import os from datetime import datetime +from typing import Iterable import nextcord from sqlalchemy import select, true, update @@ -395,7 +396,7 @@ def record_event_host(config: DakinyConfig, user: int): session.commit() -async def update_clan_messages(guild: nextcord.Guild): +async def update_clan_messages(guild: nextcord.Guild, channel_updates: Iterable[str]): """ Updates all clan messages to contents in MESSAGES_DIR. Does not to any git work. Is expected to be called after git is pulled. @@ -411,7 +412,7 @@ async def update_clan_messages(guild: nextcord.Guild): dirs = [ dir_name - for dir_name in os.listdir(MESSAGES_DIR) + for dir_name in channel_updates if os.path.isdir(f"{MESSAGES_DIR}/{dir_name}") ]