Add OPENAI Command Maker
authorGeorgios Atheridis <georgios@atheridis.org>
Mon, 22 May 2023 22:59:56 +0000 (23:59 +0100)
committerGeorgios Atheridis <georgios@atheridis.org>
Mon, 22 May 2023 22:59:56 +0000 (23:59 +0100)
Automatically generates an output for nonexistent commands

ihaspeks/analyze_command.py
ihaspeks/main.py
ihaspeks/scripts/command_generator.py [new file with mode: 0644]

index 76ba314b3e74f1e9ac2f97d250d64b124110a6d3..a2ec24bdc6ae195f0a7ff03977ffc7da74573542 100644 (file)
@@ -12,7 +12,7 @@ PATH = os.path.dirname(os.path.realpath(__file__))
 logger.debug(f"PATH set to: {PATH}")
 
 
-def do_command(bot: Bot, message: Message, command_modules: dict):
+def do_command(bot: Bot, message: Message, command_modules: dict) -> bool:
     db_name_database = "database.db"
     conn = sqlite3.connect(os.path.join(PATH, db_name_database))
     c = conn.cursor()
@@ -73,7 +73,7 @@ def do_command(bot: Bot, message: Message, command_modules: dict):
     fetched = c.fetchall()
     if not fetched:
         conn.close()
-        return
+        return False
 
     (
         _,
@@ -90,7 +90,7 @@ def do_command(bot: Bot, message: Message, command_modules: dict):
             Please wait {int(avail_time - message_timestamp) + 1} seconds.",
         )
         conn.close()
-        return
+        return False
 
     c.execute(
         "REPLACE INTO cooldowns VALUES (?, ?, ?)",
@@ -113,3 +113,5 @@ def do_command(bot: Bot, message: Message, command_modules: dict):
         command_modules[command].main(bot, message)
     else:
         tools.smart_privmsg.send(bot, message, value)
+
+    return True
index 95510349b4c75052da617273a2f0c8c7ffaec35e..277be70b9623ee7bcfe9484873ef6bb160dcffc1 100644 (file)
@@ -16,6 +16,7 @@ import scripts.crylaugh
 import scripts.clean_queue
 import scripts.discord_start_stream
 import scripts.will_be_missed
+import scripts.command_generator
 import database_manager
 import analyze_auto_message
 import time
@@ -36,6 +37,7 @@ reload(scripts.crylaugh)
 reload(scripts.clean_queue)
 reload(scripts.discord_start_stream)
 reload(scripts.will_be_missed)
+reload(scripts.command_generator)
 reload(database_manager)
 reload(analyze_auto_message)
 
@@ -140,7 +142,8 @@ def main(bot: Bot, message: Message):
     if message.command == Commands.PRIVMSG:
         database_manager.add_message_to_chat_history(message)
         if message.value[0] in {"?", "\\"}:
-            analyze_command.do_command(bot, message, commands_modules)
+            if not analyze_command.do_command(bot, message, commands_modules):
+                scripts.command_generator.generate(bot, message)
         scripts.unit_converter.send_metric(bot, message)
         scripts.alwase.alwase(bot, message)
         scripts.alwase.no_horny(bot, message)
diff --git a/ihaspeks/scripts/command_generator.py b/ihaspeks/scripts/command_generator.py
new file mode 100644 (file)
index 0000000..3be736a
--- /dev/null
@@ -0,0 +1,54 @@
+from aptbot.bot import Bot, Message, Commands
+import os
+import tools.smart_privmsg
+import requests
+
+openai_api_key = os.getenv("OPENAI_API")
+
+SYSTEM_CONTENT = """You act like a twitch.tv command. The user gives you a word and you say something that would be expected to be said as that word being a command. All words are valid commands.
+
+Do not comment about what's going to happen on stream. Do not add <insert X> type of text.
+
+All commands start with a question mark. Every word is a valid command. Come up with something funny.
+
+Some information: The name of the streamer is Peks. His avatar is a shiba inu doge. He likes playing League of Legends and Overwatch 2. He mains Twitch in League of Legends and Genji in Overwatch 2. He's born in England, but has lived in France since he was 4, so Peks is French.
+
+Try to come up with funny and troll answers. Stuff that would annoy viewers."""
+
+
+def generate(bot: Bot, message: Message):
+    replied_message = message.tags.get("reply-parent-msg-body", None)
+    if replied_message:
+        msg = replied_message
+        replied_msg_id = message.tags["reply-parent-msg-id"]
+    else:
+        msg = " ".join(message.value.split(" ")[1:])
+        replied_msg_id = message.tags["id"]
+
+    response = requests.post(
+        "https://api.openai.com/v1/chat/completions",
+        headers={
+            "Content-Type": "application/json",
+            "Authorization": f"Bearer {openai_api_key}",
+        },
+        data={
+            "model": "gpt-3.5-turbo",
+            "messages": [
+                {
+                    "role": "system",
+                    "content": SYSTEM_CONTENT,
+                },
+                {
+                    "role": "user",
+                    "content": msg,
+                }
+            ],
+        },
+    )
+    print(response)
+    if response.status_code != 200:
+        msg = "Sowwy, theres a minor issue right now :("
+    else:
+        msg = response.json()["choices"][0]["message"]["content"]
+
+    tools.smart_privmsg.send_safe(bot, message.channel, msg, reply=replied_msg_id)