diff options
Diffstat (limited to 'cogs/assistant.py')
| -rw-r--r-- | cogs/assistant.py | 130 |
1 files changed, 0 insertions, 130 deletions
diff --git a/cogs/assistant.py b/cogs/assistant.py deleted file mode 100644 index fed7825..0000000 --- a/cogs/assistant.py +++ /dev/null | |||
| @@ -1,130 +0,0 @@ | |||
| 1 | # This file is part of Senator Sperma | ||
| 2 | # Licensed under the GPL-3.0 License | ||
| 3 | # Copyright (C) 2026 Salmonik @ salmonik@tuta.io | ||
| 4 | # https://codeberg.org/salmonik/senator-sperma | ||
| 5 | |||
| 6 | import discord | ||
| 7 | from discord.ext import commands | ||
| 8 | import httpx | ||
| 9 | import logging | ||
| 10 | import os | ||
| 11 | |||
| 12 | logger = logging.getLogger(__name__) | ||
| 13 | |||
| 14 | class Jarvis(commands.Cog): | ||
| 15 | def __init__(self, bot): | ||
| 16 | self.bot = bot | ||
| 17 | self.keyword = os.getenv("ASSISTANT_KEYWORD", "jarvis").lower() | ||
| 18 | |||
| 19 | @commands.Cog.listener() | ||
| 20 | async def on_message(self, message): | ||
| 21 | if message.author == self.bot.user: | ||
| 22 | return | ||
| 23 | |||
| 24 | logger.info(message.content) | ||
| 25 | |||
| 26 | if message.mentions: | ||
| 27 | if message.mentions[0].id == self.bot.user.id: | ||
| 28 | await self.assistant(message) | ||
| 29 | |||
| 30 | elif self.keyword in message.content.lower(): | ||
| 31 | await self.assistant(message) | ||
| 32 | |||
| 33 | @staticmethod | ||
| 34 | async def assistant(message): | ||
| 35 | headers = { | ||
| 36 | "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", | ||
| 37 | "Content-Type": "application/json" | ||
| 38 | } | ||
| 39 | |||
| 40 | chain = await Jarvis._build_message_chain(message) | ||
| 41 | messages = Jarvis._build_messages(message, chain) | ||
| 42 | payload = Jarvis._build_payload(messages) | ||
| 43 | |||
| 44 | response_data = await Jarvis._safe_api_call(payload, headers) | ||
| 45 | if response_data is None: | ||
| 46 | await message.reply("Przepraszam, nie mogę teraz odpowiedzieć. Spróbuj ponownie później.") | ||
| 47 | return | ||
| 48 | |||
| 49 | response_message = Jarvis._parse_response(response_data) | ||
| 50 | |||
| 51 | channel = message.channel | ||
| 52 | await message.reply(response_message) | ||
| 53 | |||
| 54 | @staticmethod | ||
| 55 | async def _build_message_chain(message): | ||
| 56 | chain = [] | ||
| 57 | current = message | ||
| 58 | max_depth = 5 | ||
| 59 | |||
| 60 | while current and len(chain) < max_depth: | ||
| 61 | chain.append(current) | ||
| 62 | if not current.reference: | ||
| 63 | break | ||
| 64 | try: | ||
| 65 | if current.reference.resolved: | ||
| 66 | current = current.reference.resolved | ||
| 67 | else: | ||
| 68 | current = await current.channel.fetch_message(current.reference.message_id) | ||
| 69 | except discord.NotFound: | ||
| 70 | break | ||
| 71 | |||
| 72 | return list(reversed(chain)) | ||
| 73 | |||
| 74 | @staticmethod | ||
| 75 | def _build_messages(message, chain=None): | ||
| 76 | messages = [{ | ||
| 77 | "role": "system", | ||
| 78 | "content": "Jesteś Senator Sperma Armostrong, także znany jako Jarvis." | ||
| 79 | "Twoim zadaniem jest asystowanie użytkownika" | ||
| 80 | "zachowuj sie jak Jarvis, bądź osobistym asystentem użytkownika, nie używaj emotikon" | ||
| 81 | "nie pytaj sie użytkownika czy chce abyś coś zrobił, użytkownik ci sam powie abyś coś wykonał" | ||
| 82 | "Nie pozostawiaj wiadomości pustej oraz nie dawaj swojej wiadomości w pole myślenia" | ||
| 83 | }] | ||
| 84 | |||
| 85 | if chain is None: | ||
| 86 | chain = [message] | ||
| 87 | |||
| 88 | for msg in chain: | ||
| 89 | messages.append({ | ||
| 90 | "role": "user", | ||
| 91 | "content": f"USER: {msg.author.display_name}\nMESSAGE: {msg.content}" | ||
| 92 | }) | ||
| 93 | |||
| 94 | return messages | ||
| 95 | |||
| 96 | @staticmethod | ||
| 97 | def _build_payload(messages): | ||
| 98 | return { | ||
| 99 | "model": os.getenv("OPENROUTER_MODEL"), | ||
| 100 | "messages": messages, | ||
| 101 | "plugins": [{"id": "response-healing"}] | ||
| 102 | } | ||
| 103 | |||
| 104 | @staticmethod | ||
| 105 | async def _make_api_request(payload, headers): | ||
| 106 | async with httpx.AsyncClient(timeout=30.0) as client: | ||
| 107 | url = "https://openrouter.ai/api/v1/chat/completions" | ||
| 108 | response = await client.post(url=url, headers=headers, json=payload) | ||
| 109 | response_data = response.json() | ||
| 110 | logger.info(response_data) | ||
| 111 | return response_data | ||
| 112 | |||
| 113 | @staticmethod | ||
| 114 | async def _safe_api_call(payload, headers): | ||
| 115 | try: | ||
| 116 | return await Jarvis._make_api_request(payload, headers) | ||
| 117 | except httpx.HTTPError as e: | ||
| 118 | logger.error(f"API request failed: {e}") | ||
| 119 | return None | ||
| 120 | |||
| 121 | @staticmethod | ||
| 122 | def _parse_response(response_data): | ||
| 123 | content = response_data['choices'][0]['message']['content'] | ||
| 124 | |||
| 125 | logger.info(content) | ||
| 126 | |||
| 127 | return content | ||
| 128 | |||
| 129 | def setup(bot): | ||
| 130 | bot.add_cog(Jarvis(bot)) | ||
