Kaveen Kumarasinghe 1 year ago
parent 66efa5abd5
commit a0ca4bc307

@ -499,6 +499,12 @@ class Commands(discord.Cog, name="Commands"):
min_value=-2, min_value=-2,
max_value=2, max_value=2,
) )
@discord.option(
name="use_threads",
description="Set this to false to start a channel conversation",
required=False,
default=False,
)
@discord.guild_only() @discord.guild_only()
async def converse( async def converse(
self, self,
@ -512,6 +518,7 @@ class Commands(discord.Cog, name="Commands"):
top_p: float, top_p: float,
frequency_penalty: float, frequency_penalty: float,
presence_penalty: float, presence_penalty: float,
use_threads: bool,
): ):
await self.converser_cog.converse_command( await self.converser_cog.converse_command(
ctx, ctx,
@ -524,6 +531,7 @@ class Commands(discord.Cog, name="Commands"):
top_p, top_p,
frequency_penalty, frequency_penalty,
presence_penalty, presence_penalty,
use_threads=use_threads,
) )
@add_to_group("gpt") @add_to_group("gpt")

@ -306,6 +306,12 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
): ):
"""end the thread of the user interacting with the bot, if the conversation has reached the limit close it for the owner""" """end the thread of the user interacting with the bot, if the conversation has reached the limit close it for the owner"""
normalized_user_id = opener_user_id if opener_user_id else ctx.author.id normalized_user_id = opener_user_id if opener_user_id else ctx.author.id
# Check if the channel is an instance of a thread
thread = False
if isinstance(ctx.channel, discord.Thread):
thread = True
if ( if (
conversation_limit conversation_limit
): # if we reach the conversation limit we want to close from the channel it was maxed out in ): # if we reach the conversation limit we want to close from the channel it was maxed out in
@ -379,12 +385,13 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
][0] ][0]
self.conversation_thread_owners[owner_id].remove(ctx.channel.id) self.conversation_thread_owners[owner_id].remove(ctx.channel.id)
# Attempt to close and lock the thread. # Attempt to close and lock the thread.
try: if thread:
thread = await self.bot.fetch_channel(channel_id) try:
await thread.edit(locked=True) thread = await self.bot.fetch_channel(channel_id)
await thread.edit(name="Closed-GPT") await thread.edit(locked=True)
except Exception: await thread.edit(name="Closed-GPT")
traceback.print_exc() except Exception:
traceback.print_exc()
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
else: else:
@ -395,12 +402,13 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
) )
# Attempt to close and lock the thread. # Attempt to close and lock the thread.
try: if thread:
thread = await self.bot.fetch_channel(thread_id) try:
await thread.edit(locked=True) thread = await self.bot.fetch_channel(thread_id)
await thread.edit(name="Closed-GPT") await thread.edit(locked=True)
except Exception: await thread.edit(name="Closed-GPT")
traceback.print_exc() except Exception:
traceback.print_exc()
async def send_settings_text(self, ctx): async def send_settings_text(self, ctx):
"""compose and return the settings menu to the interacting user""" """compose and return the settings menu to the interacting user"""
@ -1053,39 +1061,29 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
if await Moderation.simple_moderate_and_respond(opener, ctx): if await Moderation.simple_moderate_and_respond(opener, ctx):
return return
# if user.id in self.conversation_thread_owners:
# await ctx.respond(
# "You've already created a thread, end it before creating a new one",
# delete_after=5,
# )
# return
# Add a variable to store the channel or thread
target = None
if use_threads: if use_threads:
if private: if private:
embed_title = f"{user.name}'s private conversation with GPT3" embed_title = f"{user.name}'s private conversation with GPT"
thread = await ctx.channel.create_thread( thread = await ctx.channel.create_thread(
name=user.name + "'s private conversation with GPT3", name=embed_title,
auto_archive_duration=60, auto_archive_duration=60,
) )
target = thread target = thread
else: else:
embed_title = f"{user.name}'s conversation with GPT3" embed_title = f"{user.name}'s conversation with GPT"
message_embed = discord.Embed(title=embed_title, color=0x808080) message_embed = discord.Embed(title=embed_title, color=0x808080)
message_thread = await ctx.send(embed=message_embed) message_thread = await ctx.send(embed=message_embed)
thread = await message_thread.create_thread( thread = await message_thread.create_thread(
name=user.name + "'s conversation with GPT3", name=user.name + "'s conversation with GPT",
auto_archive_duration=60, auto_archive_duration=60,
) )
target = thread target = thread
else: else:
target = ctx.channel target = ctx.channel
if private: if private:
embed_title = f"{user.name}'s private conversation with GPT3" embed_title = f"{user.name}'s private conversation with GPT"
else: else:
embed_title = f"{user.name}'s conversation with GPT3" embed_title = f"{user.name}'s conversation with GPT"
embed = discord.Embed(title=embed_title, color=0x808080) embed = discord.Embed(title=embed_title, color=0x808080)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@ -1098,7 +1096,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
) )
# Set the overrides for the conversation # Set the overrides for the conversation
self.conversation_threads[thread.id].set_overrides( self.conversation_threads[target.id].set_overrides(
temperature, top_p, frequency_penalty, presence_penalty temperature, top_p, frequency_penalty, presence_penalty
) )
@ -1134,7 +1132,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
"frequency_penalty", None "frequency_penalty", None
) )
presence_penalty = opener_file.get("presence_penalty", None) presence_penalty = opener_file.get("presence_penalty", None)
self.conversation_threads[thread.id].set_overrides( self.conversation_threads[target.id].set_overrides(
temperature, top_p, frequency_penalty, presence_penalty temperature, top_p, frequency_penalty, presence_penalty
) )
if ( if (
@ -1156,17 +1154,17 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
# Append the starter text for gpt3 to the user's history so it gets concatenated with the prompt later # Append the starter text for gpt3 to the user's history so it gets concatenated with the prompt later
if minimal or opener_file or opener: if minimal or opener_file or opener:
self.conversation_threads[thread.id].history.append( self.conversation_threads[target.id].history.append(
EmbeddedConversationItem(self.CONVERSATION_STARTER_TEXT_MINIMAL, 0) EmbeddedConversationItem(self.CONVERSATION_STARTER_TEXT_MINIMAL, 0)
) )
elif not minimal: elif not minimal:
self.conversation_threads[thread.id].history.append( self.conversation_threads[target.id].history.append(
EmbeddedConversationItem(self.CONVERSATION_STARTER_TEXT, 0) EmbeddedConversationItem(self.CONVERSATION_STARTER_TEXT, 0)
) )
# Set user as thread owner before sending anything that can error and leave the thread unowned # Set user as thread owner before sending anything that can error and leave the thread unowned
self.conversation_thread_owners[user_id_normalized].append(thread.id) self.conversation_thread_owners[user_id_normalized].append(target.id)
overrides = self.conversation_threads[thread.id].get_overrides() overrides = self.conversation_threads[target.id].get_overrides()
await target.send(f"<@{str(ctx.user.id)}> is the thread owner.") await target.send(f"<@{str(ctx.user.id)}> is the thread owner.")

Loading…
Cancel
Save