From f4009e08f79911a3d07afb81d700623b3fcaf534 Mon Sep 17 00:00:00 2001 From: Kaveen Kumarasinghe Date: Sat, 14 Jan 2023 19:18:46 -0500 Subject: [PATCH] context menu buttons --- README.md | 8 +++++--- cogs/commands.py | 19 ++++++++++++++++++- cogs/image_service_cog.py | 9 ++++++--- cogs/text_service_cog.py | 9 +++++++++ gpt3discord.py | 2 +- services/image_service.py | 2 +- services/text_service.py | 9 ++++++--- 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bf827e9..01e0af8 100644 --- a/README.md +++ b/README.md @@ -26,17 +26,19 @@ SUPPORT SERVER FOR BOT SETUP: https://discord.gg/WvAHXDMS7Q (You can NOT use the - **Edit Requests** - Ask GPT to edit a piece of text with a given instruction using a specific OpenAI edits model! `/gpt edit`! +- **Context menu commands** - Allow people to prompt GPT and DALL-E directly by right clicking a message: + + + - **Automatic retry on API errors** - The bot will automatically retry API requests if they fail due to some issue with OpenAI's APIs, this is becoming increasingly important now as their APIs become under heavy load. -- **Allow each individual user to enter their own API Key!** - Each request that a user makes will be made using their own API key! Check out the User-Input API Key section in this README for more details. +- **AI-BASED SERVER MODERATION** - GPT3Discord now has a built-in AI-based moderation system that can automatically detect and remove toxic messages from your server. This is a great way to keep your server safe and clean, and it's completely automatic and **free**! Check out the commands section to learn how to enable it! - **Permanent memory with embeddings and PineconeDB finished!** - An initial alpha version of permanent memory is now done! This allows you to chat with GPT3 infinitely and accurately, and save tokens, by using embeddings. *Please read the Permanent Memory section for more information!* -- **AI-BASED SERVER MODERATION** - GPT3Discord now has a built-in AI-based moderation system that can automatically detect and remove toxic messages from your server. This is a great way to keep your server safe and clean, and it's completely automatic and **free**! Check out the commands section to learn how to enable it! - # Features - **Directly prompt GPT3 with `/gpt ask `** diff --git a/cogs/commands.py b/cogs/commands.py index ec03909..d15b88c 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -61,7 +61,6 @@ class Commands(discord.Cog, name="Commands"): """ System commands """ - @add_to_group("system") @discord.slash_command( name="settings", @@ -396,3 +395,21 @@ class Commands(discord.Cog, name="Commands"): @discord.guild_only() async def setup(self, ctx: discord.ApplicationContext): await self.converser_cog.setup_command(ctx) + + """ + Text-based context menu commands from here + """ + @discord.message_command( + name="Ask GPT", guild_ids=ALLOWED_GUILDS, checks=[Check.check_gpt_roles()]) + async def ask_gpt_action(self, ctx, message: discord.Message): + await self.converser_cog.ask_gpt_action(ctx, message) + + + """ + Image-based context menu commands from here + """ + @discord.message_command( + name="Draw", guild_ids=ALLOWED_GUILDS, checks=[Check.check_dalle_roles()]) + async def draw_action(self, ctx, message: discord.Message): + await self.image_draw_cog.draw_action(ctx, message) + diff --git a/cogs/image_service_cog.py b/cogs/image_service_cog.py index 8698e02..e2350fb 100644 --- a/cogs/image_service_cog.py +++ b/cogs/image_service_cog.py @@ -40,7 +40,7 @@ class DrawDallEService(discord.Cog, name="DrawDallEService"): print("Draw service initialized") self.redo_users = {} - async def draw_command(self, ctx: discord.ApplicationContext, prompt: str): + async def draw_command(self, ctx: discord.ApplicationContext, prompt: str, from_action=False): user_api_key = None if USER_INPUT_API_KEYS: user_api_key = await TextService.get_user_api_key( @@ -66,8 +66,11 @@ class DrawDallEService(discord.Cog, name="DrawDallEService"): except Exception as e: print(e) traceback.print_exc() - await ctx.respond("Something went wrong. Please try again later.") - await ctx.send_followup(e) + await ctx.respond("Something went wrong. Please try again later.", ephemeral=from_action) + await ctx.send_followup(e, ephemeral=from_action) + + async def draw_action(self, ctx, message): + await self.draw_command(ctx, message.content, from_action=True) async def local_size_command(self, ctx: discord.ApplicationContext): await ctx.defer() diff --git a/cogs/text_service_cog.py b/cogs/text_service_cog.py index 907fee3..aa77656 100644 --- a/cogs/text_service_cog.py +++ b/cogs/text_service_cog.py @@ -11,6 +11,7 @@ import json import discord +from models.check_model import Check from services.environment_service import EnvService from services.message_queue_service import Message from services.moderations_service import Moderation @@ -686,6 +687,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): top_p: float, frequency_penalty: float, presence_penalty: float, + from_action=None, ): user = ctx.user prompt = await self.mention_to_username(ctx, prompt.strip()) @@ -709,6 +711,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): presence_penalty_override=presence_penalty, from_ask_command=True, custom_api_key=user_api_key, + from_action=prompt, ) async def edit_command( @@ -971,3 +974,9 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): # Otherwise, process the settings change await self.process_settings(ctx, parameter, value) + + """ + Text-based context menu commands from here + """ + async def ask_gpt_action(self, ctx, message: discord.Message): # message commands return the message + await self.ask_command(ctx, message.content, 0.7, 0.9, 0,0,message.content) diff --git a/gpt3discord.py b/gpt3discord.py index f8a1dc6..ceb366a 100644 --- a/gpt3discord.py +++ b/gpt3discord.py @@ -27,7 +27,7 @@ from services.usage_service import UsageService from services.environment_service import EnvService -__version__ = "6.1" +__version__ = "6.5" """ diff --git a/services/image_service.py b/services/image_service.py index 524fd73..ff98845 100644 --- a/services/image_service.py +++ b/services/image_service.py @@ -50,7 +50,7 @@ class ImageService: except ValueError as e: message = f"Error: {e}. Please try again with a different prompt." await ctx.channel.send(message) if not from_context else await ctx.respond( - message + message, ephemeral=True ) return diff --git a/services/text_service.py b/services/text_service.py index c6c64c5..47d19cb 100644 --- a/services/text_service.py +++ b/services/text_service.py @@ -34,6 +34,7 @@ class TextService: custom_api_key=None, edited_request=False, redo_request=False, + from_action=None, ): new_prompt = ( prompt + "\nGPTie: " @@ -255,7 +256,7 @@ class TextService: str(response["choices"][0]["text"]) ) - if from_ask_command: + if from_ask_command or from_action: # Append the prompt to the beginning of the response, in italics, then a new line response_text = response_text.strip() response_text = f"***{prompt}***\n\n{response_text}" @@ -455,8 +456,10 @@ class TextService: # Error catching for OpenAI model value errors except ValueError as e: - if from_context: - await ctx.send_followup(e) + if from_action: + await ctx.respond(e, ephemeral=True) + elif from_context: + await ctx.send_followup(e, ephemeral=True) else: await ctx.reply(e) converser_cog.remove_awaiting(