From 8486453d18f7f36a6c9721c2b6f2cd5a676e0bd5 Mon Sep 17 00:00:00 2001 From: Paillat Date: Tue, 15 Aug 2023 11:03:23 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(makeprompt.py):=20refactor?= =?UTF-8?q?=20message=20history=20fetching=20logic=20into=20a=20separate?= =?UTF-8?q?=20function=20for=20better=20code=20organization=20and=20reusab?= =?UTF-8?q?ility=20=E2=9C=A8=20feat(makeprompt.py):=20add=20`is=5Fignorabl?= =?UTF-8?q?e`=20function=20to=20check=20if=20a=20message=20content=20is=20?= =?UTF-8?q?ignorable=20based=20on=20certain=20conditions=20=E2=9C=A8=20fea?= =?UTF-8?q?t(makeprompt.py):=20add=20`fetch=5Fmessages=5Fhistory`=20functi?= =?UTF-8?q?on=20to=20fetch=20message=20history=20from=20a=20channel=20with?= =?UTF-8?q?=20optional=20limit=20and=20original=20message=20parameters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/makeprompt.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/makeprompt.py b/src/makeprompt.py index a9b96ed..63ca25b 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -29,6 +29,26 @@ async def replace_mentions(content, bot): content = content.replace(mention, f"@{user.name}") return content +def is_ignorable(content): + if content.startswith("-") or content.startswith("//"): + return True + return False + +async def fetch_messages_history(channel:discord.TextChannel, limit, original_message): + messages = [] + if original_message == None: + async for msg in channel.history(limit=100, oldest_first=True): + if not is_ignorable(msg.content): + messages.append(msg) + if len(messages) == limit: + break + else: + async for msg in channel.history(limit=100, before=original_message, oldest_first=True): + if not is_ignorable(msg.content): + messages.append(msg) + if len(messages) == limit: + break + return messages async def chatgpt_process( self, messages, message: discord.Message, api_key, prompt, model @@ -274,18 +294,7 @@ async def chat_process(self, message): except: pass - # if the message is not a reply - if original_message == None: - messages = await message.channel.history(limit=prompt_size).flatten() - messages.reverse() - # if the message is a reply, we need to handle the message history differently - else: - messages = await message.channel.history( - limit=prompt_size, before=original_message - ).flatten() - messages.reverse() - messages.append(original_message) - messages.append(message) + messages = await fetch_messages_history(message.channel, prompt_size, original_message) # if the pretend to be feature is enabled, we add the pretend to be text to the prompt if pretend_enabled: