diff --git a/utils.py b/utils.py index b462915..3efc71b 100644 --- a/utils.py +++ b/utils.py @@ -397,11 +397,10 @@ async def update_clan_messages(guild: nextcord.Guild): Updates all clan messages to contents in MESSAGES_DIR. Does not to any git work. Is expected to be called after git is pulled. - for ever directory, sort the files by number then post - posting involves checking if there is a message for that particular file then editing it or creating a - new one. the message could be deleted, which would cause a new message to be sent and database updated. + for ever directory, sort the files by number then edits/sends the message. Channel fetching/management is handled by another function. + Message editing and sending is handled by another function. Args: guild (nextcord.Guild): Guild object that all channels and messages belong to. @@ -426,27 +425,9 @@ async def update_clan_messages(guild: nextcord.Guild): message_name = f"{dir_name}/{file_name}" file_path = f"{dir_path}/{file_name}" with open(file_path, "r", encoding="utf8") as reader: - stmt = select(Messages.Id).where(Messages.Path == message_name) - msg_id = session.scalar(stmt) - if msg_id is None: - # don't have a message for this file - msg = await channel.send(reader.read(2000)) - new_message = Messages(Path=message_name, Id=msg.id) - session.add(new_message) - session.commit() - else: - try: - msg: nextcord.Message = await channel.fetch_message(msg_id) - await msg.edit(reader.read(2000)) - except nextcord.NotFound: - msg = await channel.send(reader.read(2000)) - stmt = ( - update(Messages) - .where(Messages.Path == message_name) - .values(Id=msg.id) - ) - session.execute(stmt) - session.commit() + await edit_clan_message( + session, channel, message_name, reader.read(2000) + ) async def get_clan_message_channel( @@ -508,3 +489,41 @@ async def get_clan_message_channel( session.execute(stmt) session.commit() return ret_channel + + +async def edit_clan_message( + session: Session, + channel: nextcord.TextChannel, + message_name: str, + message_content: str, +): + """ + Obtains a message id from database. If no id, then need to send a new message and record the id. + If there is a message, then edit the message. If that message is not found in discord, then need to + send a new message and record the id in the database. + + Args: + session (Session): Open connection to database. + channel (nextcord.TextChannel): Channel object that message belongs to. + message_name (str): Name of the message. Corresponds to the file that contains message content. + message_content (str): Content of the message to edit/send. + """ + stmt = select(Messages.Id).where(Messages.Path == message_name) + msg_id = session.scalar(stmt) + if msg_id is None: + # don't have a message for this file + msg = await channel.send(message_content) + new_message = Messages(Path=message_name, Id=msg.id) + session.add(new_message) + session.commit() + else: + try: + msg: nextcord.Message = await channel.fetch_message(msg_id) + await msg.edit(message_content) + except nextcord.NotFound: + msg = await channel.send(message_content) + stmt = ( + update(Messages).where(Messages.Path == message_name).values(Id=msg.id) + ) + session.execute(stmt) + session.commit()