Slight refactor

Rene Teigen 2 years ago
parent 5504ea2802
commit 07849ca42e

@ -621,7 +621,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
ctx = self.redo_users[after.author.id].ctx ctx = self.redo_users[after.author.id].ctx
await response_message.edit(content="Redoing prompt 🔄...") await response_message.edit(content="Redoing prompt 🔄...")
edited_content = await self.replace_mention(after, after.content) edited_content = await self.mention_to_username(after, after.content)
if after.channel.id in self.conversation_threads: if after.channel.id in self.conversation_threads:
# Remove the last two elements from the history array and add the new <username>: prompt # Remove the last two elements from the history array and add the new <username>: prompt
@ -706,7 +706,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
if not user_api_key: if not user_api_key:
return return
prompt = await self.replace_mention(message, content) prompt = await self.mention_to_username(message, content)
await self.check_conversation_limit(message) await self.check_conversation_limit(message)
@ -788,17 +788,16 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
response_text = response_text.replace("<|endofstatement|>", "") response_text = response_text.replace("<|endofstatement|>", "")
return response_text return response_text
async def replace_mention(self, ctx, message): async def mention_to_username(self, ctx, message):
if discord.utils.raw_mentions(message): if not discord.utils.raw_mentions(message):
for mention in discord.utils.raw_mentions(message): return message
try:
user = await discord.utils.get_or_fetch(ctx.guild, 'member', mention)
username = user.display_name
except:
username = str(mention)
message = message.replace(f"<@{str(mention)}>", username)
return message
else: 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 return message
# ctx can be of type AppContext(interaction) or Message # ctx can be of type AppContext(interaction) or Message
@ -983,7 +982,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
response_text = f"***{prompt}***\n\n{response_text}" response_text = f"***{prompt}***\n\n{response_text}"
# If gpt3 tries writing a user mention try to replace it with their name # If gpt3 tries writing a user mention try to replace it with their name
response_text = await self.replace_mention(ctx, response_text) response_text = await self.mention_to_username(ctx, response_text)
# If the user is conversing, add the GPT response to their conversation history. # If the user is conversing, add the GPT response to their conversation history.
if ( if (
@ -1167,7 +1166,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
presence_penalty: float, presence_penalty: float,
): ):
user = ctx.user user = ctx.user
prompt = await self.replace_mention(ctx, prompt.strip()) prompt = await self.mention_to_username(ctx, prompt.strip())
user_api_key = None user_api_key = None
if USER_INPUT_API_KEYS: if USER_INPUT_API_KEYS:
@ -1249,7 +1248,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
return return
if opener: if opener:
opener = await self.replace_mention(ctx, opener) opener = await self.mention_to_username(ctx, opener)
if not opener and not opener_file: if not opener and not opener_file:
user_id_normalized = user.id user_id_normalized = user.id

Loading…
Cancel
Save