diff --git a/.gitignore b/.gitignore index 4244f47..a95b25f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ __pycache__ #user files .env .vscode +*.sqlite bot.pid usage.txt /dalleimages \ No newline at end of file diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index cd12ecf..c1822ef 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -621,7 +621,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): ctx = self.redo_users[after.author.id].ctx await response_message.edit(content="Redoing prompt 🔄...") - edited_content = after.content + edited_content = await self.mention_to_username(after, after.content) if after.channel.id in self.conversation_threads: # Remove the last two elements from the history array and add the new : prompt @@ -706,7 +706,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): if not user_api_key: return - prompt = content + prompt = await self.mention_to_username(message, content) await self.check_conversation_limit(message) @@ -788,6 +788,18 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): response_text = response_text.replace("<|endofstatement|>", "") return response_text + async def mention_to_username(self, ctx, message): + if not discord.utils.raw_mentions(message): + return message + else: + for mention in discord.utils.raw_mentions(message): + try: + user = await discord.utils.get_or_fetch(ctx.guild, 'member', mention) + message = message.replace(f"<@{str(mention)}>", user.display_name) + except: + pass + return message + # ctx can be of type AppContext(interaction) or Message async def encapsulated_send( self, @@ -969,12 +981,8 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): response_text = response_text.strip() response_text = f"***{prompt}***\n\n{response_text}" - # If GPT3 tries to ping somebody, don't let it happen - if re.search(r"<@!?\d+>|<@&\d+>|<#\d+>", str(response_text)): - message = "I'm sorry, I can't mention users, roles, or channels." - await ctx.send_followup(message) if from_context else await ctx.reply( - message - ) + # If gpt3 tries writing a user mention try to replace it with their name + response_text = await self.mention_to_username(ctx, response_text) # If the user is conversing, add the GPT response to their conversation history. if ( @@ -1018,9 +1026,12 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): custom_api_key=custom_api_key, ) - # Cleanse + # Cleanse again response_text = self.cleanse_response(response_text) + # escape any other mentions like @here or @everyone + response_text = discord.utils.escape_mentions(response_text) + # If we don't have a response message, we are not doing a redo, send as a new message(s) if not response_message: if len(response_text) > self.TEXT_CUTOFF: @@ -1155,7 +1166,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): presence_penalty: float, ): user = ctx.user - prompt = prompt.strip() + prompt = await self.mention_to_username(ctx, prompt.strip()) user_api_key = None if USER_INPUT_API_KEYS: @@ -1236,6 +1247,9 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): ) return + if opener: + opener = await self.mention_to_username(ctx, opener) + if not opener and not opener_file: user_id_normalized = user.id else: diff --git a/cogs/image_prompt_optimizer.py b/cogs/image_prompt_optimizer.py index 476a48e..0533c90 100644 --- a/cogs/image_prompt_optimizer.py +++ b/cogs/image_prompt_optimizer.py @@ -74,7 +74,8 @@ class ImgPromptOptimizer(discord.Cog, name="ImgPromptOptimizer"): user = ctx.user final_prompt = self.OPTIMIZER_PRETEXT - final_prompt += prompt + # replace mentions with nicknames for the prompt + final_prompt += await self.converser_cog.replace_mention(ctx, prompt) # If the prompt doesn't end in a period, terminate it. if not final_prompt.endswith("."): @@ -100,12 +101,8 @@ class ImgPromptOptimizer(discord.Cog, name="ImgPromptOptimizer"): # also relatively cost-effective response_text = response["choices"][0]["text"] - - if re.search(r"<@!?\d+>|<@&\d+>|<#\d+>", response_text): - await ctx.respond( - "I'm sorry, I can't mention users, roles, or channels." - ) - return + # escape any mentions + response_text = discord.utils.escape_mentions(response_text) # If the response_message is > 75 words, concatenate to the last 70th word # TODO Temporary workaround until prompt is adjusted to make the optimized prompts shorter.