From 303d3c07ac8d7b5c5a71c5759e4c3b2c86ebd096 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:20:29 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(chat.py):=20fix=20typo=20in?= =?UTF-8?q?=20message.channel.send()=20method=20call=20=F0=9F=94=92=20chor?= =?UTF-8?q?e(chat.py):=20add=20moderation=20check=20for=20message=20conten?= =?UTF-8?q?t=20in=20call=5Ffunction()=20to=20prevent=20sending=20blocked?= =?UTF-8?q?=20messages=20=F0=9F=94=92=20chore(chat.py):=20add=20moderation?= =?UTF-8?q?=20check=20for=20query=20in=20call=5Ffunction()=20to=20prevent?= =?UTF-8?q?=20sending=20blocked=20queries=20=F0=9F=94=92=20chore(makepromp?= =?UTF-8?q?t.py):=20add=20moderation=20check=20for=20content=20in=20chatgp?= =?UTF-8?q?t=5Fprocess()=20to=20prevent=20sending=20blocked=20content=20?= =?UTF-8?q?=F0=9F=94=92=20chore(makeprompt.py):=20add=20depth=20check=20to?= =?UTF-8?q?=20prevent=20recursive=20answering=20in=20chatgpt=5Fprocess()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cogs/chat.py | 7 +------ src/functionscalls.py | 11 ++++++++++- src/makeprompt.py | 26 ++++++++++++++++++-------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/cogs/chat.py b/src/cogs/chat.py index 6b69064..76a5fd1 100644 --- a/src/cogs/chat.py +++ b/src/cogs/chat.py @@ -107,7 +107,7 @@ class Chat(discord.Cog): if message.content.startswith("botator!unban"): user2ban = message.content.split(" ")[1] await banusr.unbanuser(user2ban) - await message.channel.send(f"User {user2ban} unbanned !") + await message.chafnnel.send(f"User {user2ban} unbanned !") return if str(message.author.id) in banusr.banend_users: await asyncio.sleep(2) @@ -115,11 +115,6 @@ class Chat(discord.Cog): return await mp.chat_process(self, message) - @discord.slash_command(name="say", description="Say a message") - async def say(self, ctx: discord.ApplicationContext, message: str): - await ctx.respond("Message sent !", ephemeral=True) - await ctx.send(message) - @discord.slash_command(name="redo", description="Redo a message") async def redo(self, ctx: discord.ApplicationContext): history = await ctx.channel.history(limit=2).flatten() diff --git a/src/functionscalls.py b/src/functionscalls.py index 2d94261..03ab465 100644 --- a/src/functionscalls.py +++ b/src/functionscalls.py @@ -5,6 +5,7 @@ import aiohttp import random import time +from utils.misc import moderate from simpleeval import simple_eval from bs4 import BeautifulSoup from src.config import tenor_api_key @@ -331,7 +332,7 @@ async def evaluate_math( return f"Result to math eval of {evaluable}: ```\n{str(result)}```" -async def call_function(message: discord.Message, function_call): +async def call_function(message: discord.Message, function_call, api_key): name = function_call.get("name", "") if name == "": raise FuntionCallError("No name provided") @@ -341,6 +342,14 @@ async def call_function(message: discord.Message, function_call): if name not in functions_matching: raise FuntionCallError("Invalid function name") function = functions_matching[name] + if arguments.get("message", "") != "" and moderate( + api_key=api_key, text=arguments["message"] + ): + return "Message blocked by the moderation system. Please try again." + if arguments.get("query", "") != "" and moderate( + api_key=api_key, text=arguments["query"] + ): + return "Query blocked by the moderation system. If the user asked for something edgy, please tell them in a funny way that you won't do it, but do not specify that it was blocked by the moderation system." returnable = await function(message, arguments) return returnable diff --git a/src/makeprompt.py b/src/makeprompt.py index fb39257..2444861 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -135,7 +135,7 @@ async def chatgpt_process( response = response["choices"][0]["message"] # type: ignore if response.get("function_call"): function_call = response.get("function_call") - returned = await call_function(message, function_call) + returned = await call_function(message, function_call, api_key) if returned != None: msgs.append( { @@ -153,13 +153,23 @@ async def chatgpt_process( await chatgpt_process(self, msgs, message, api_key, prompt, model, depth) else: content = response.get("content", "") - while len(content) != 0: - if len(content) > 2000: - await message.channel.send(content[:2000]) - content = content[2000:] - else: - await message.channel.send(content) - content = "" + if moderate(api_key, content, error_call): + depth += 1 + if depth > 2: + await message.channel.send( + "Oh uh, it seems like i am answering recursively. I will stop now." + ) + await chatgpt_process( + self, msgs, message, api_key, prompt, model, error_call, depth + ) + else: + while len(content) != 0: + if len(content) > 2000: + await message.channel.send(content[:2000]) + content = content[2000:] + else: + await message.channel.send(content) + content = "" async def chat_process(self, message):