From f93e9c1bbfcd600dd0009ffc5e169e15a119ea4d Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Thu, 5 Jan 2023 19:04:21 +0000 Subject: [PATCH 1/4] Implement /gpt end --- README.md | 2 +- cogs/gpt_3_commands_and_converser.py | 32 +++++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b7dfe05..15d0115 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ These commands are grouped, so each group has a prefix but you can easily tab co - Note that the above options for `/gpt converse` can be combined (you can combine minimal, private, and opener!) -`/gpt end-chat` - End a conversation with the bot. +`/gpt end` - End a conversation with the bot. `/dalle draw ` - Have DALL-E generate images based on a prompt diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index 856e41c..d57cbea 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -223,13 +223,16 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): return (cond1) and cond2 - async def end_conversation(self, message, opener_user_id=None): + async def end_conversation(self, message=None, opener_user_id=None): normalized_user_id = opener_user_id if opener_user_id else message.author.id self.conversating_users.pop(normalized_user_id) - await message.reply( - "You have ended the conversation with GPT3. Start a conversation with !g converse" - ) + if message: + await message.reply( + "You have ended the conversation with GPT3. Start a conversation with /gpt converse" + ) + else: + pass # Close all conversation threads for the user channel = self.bot.get_channel(self.conversation_threads[normalized_user_id]) @@ -260,7 +263,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): name="/gpt converse", value="Start a conversation with GPT3", inline=False ) embed.add_field( - name="/gpt end-chat", + name="/gpt end", value="End a conversation with GPT3. You can also type `end` in the conversation.", inline=False, ) @@ -850,16 +853,25 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): @add_to_group("gpt") @discord.slash_command( - name="end-chat", + name="end", description="End a conversation with GPT3", guild_ids=ALLOWED_GUILDS, ) @discord.guild_only() async def end_chat(self, ctx: discord.ApplicationContext): - await ctx.defer() - await ctx.respond( - "This has not been implemented yet. Please type `end` in your conversation thread to end the chat." - ) + await ctx.defer(ephemeral=True) + user_id = ctx.user.id + if user_id in self.conversating_users: + try: + await self.end_conversation(opener_user_id=user_id) + await ctx.respond("Your conversation has ended!", ephemeral=True, delete_after=10) + except Exception as e: + print(e) + traceback.print_exc() + await ctx.response.edit_message( + e, ephemeral=True, delete_after=30 + ) + pass @discord.slash_command( name="help", description="Get help for GPT3Discord", guild_ids=ALLOWED_GUILDS From db9dd047a93e288c9a37a2bad269c50be8b0c05b Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Thu, 5 Jan 2023 19:22:32 +0000 Subject: [PATCH 2/4] Changed how the message type is checked end_conversation --- cogs/gpt_3_commands_and_converser.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index d57cbea..7d76979 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -223,16 +223,16 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): return (cond1) and cond2 - async def end_conversation(self, message=None, opener_user_id=None): + async def end_conversation(self, message, opener_user_id=None): normalized_user_id = opener_user_id if opener_user_id else message.author.id self.conversating_users.pop(normalized_user_id) - if message: + if isinstance(message, discord.ApplicationContext): + await message.respond("Your conversation has ended!", ephemeral=True, delete_after=10) + else: await message.reply( "You have ended the conversation with GPT3. Start a conversation with /gpt converse" ) - else: - pass # Close all conversation threads for the user channel = self.bot.get_channel(self.conversation_threads[normalized_user_id]) @@ -863,14 +863,10 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): user_id = ctx.user.id if user_id in self.conversating_users: try: - await self.end_conversation(opener_user_id=user_id) - await ctx.respond("Your conversation has ended!", ephemeral=True, delete_after=10) + await self.end_conversation(ctx) except Exception as e: print(e) traceback.print_exc() - await ctx.response.edit_message( - e, ephemeral=True, delete_after=30 - ) pass @discord.slash_command( From 11ae8de8e0d85567bb783dc348c6e3fb9f69bbbe Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Thu, 5 Jan 2023 19:30:24 +0000 Subject: [PATCH 3/4] Add a response if you're not in a conversation --- cogs/gpt_3_commands_and_converser.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index 7d76979..02c5343 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -868,6 +868,8 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): print(e) traceback.print_exc() pass + else: + await ctx.respond("You're not in any conversations", ephemeral=True, delete_after=10) @discord.slash_command( name="help", description="Get help for GPT3Discord", guild_ids=ALLOWED_GUILDS From 962cb3429c54eb584cc454d17796834d7bad2506 Mon Sep 17 00:00:00 2001 From: Kaveen Kumarasinghe Date: Thu, 5 Jan 2023 16:11:34 -0500 Subject: [PATCH 4/4] add some convo safety checks --- cogs/gpt_3_commands_and_converser.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index 02c5343..9b1b993 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -704,6 +704,8 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): await ctx.send_followup(e) else: await ctx.reply(e) + if ctx.user.id in self.awaiting_responses: + self.awaiting_responses.remove(ctx.user.id) # General catch case for everything except Exception: