diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/jarvis.py | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/cogs/jarvis.py b/cogs/jarvis.py new file mode 100644 index 0000000..0f4606b --- /dev/null +++ b/cogs/jarvis.py | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | import re | ||
| 2 | import discord | ||
| 3 | from discord.ext import commands | ||
| 4 | import requests | ||
| 5 | import json | ||
| 6 | import logging | ||
| 7 | |||
| 8 | from cogs.compression import Compression | ||
| 9 | from cogs.downloader import Downloader | ||
| 10 | |||
| 11 | logger = logging.getLogger(__name__) | ||
| 12 | |||
| 13 | |||
| 14 | class Jarvis(commands.Cog): | ||
| 15 | def __init__(self, bot): | ||
| 16 | self.bot = bot | ||
| 17 | |||
| 18 | @commands.Cog.listener() | ||
| 19 | async def on_message(self, message): | ||
| 20 | print(message.content) | ||
| 21 | if message.author == self.bot.user: | ||
| 22 | return | ||
| 23 | |||
| 24 | if message.mentions: | ||
| 25 | if message.mentions[0].id == self.bot.user.id: | ||
| 26 | await self.jarvis(message) | ||
| 27 | |||
| 28 | if "jarvis" in message.content.lower(): | ||
| 29 | await self.jarvis(message) | ||
| 30 | |||
| 31 | @staticmethod | ||
| 32 | async def jarvis(message): | ||
| 33 | url = "https://openrouter.ai/api/v1/chat/completions" | ||
| 34 | headers = { | ||
| 35 | "Authorization": f"Bearer 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", | ||
| 36 | "Content-Type": "application/json" | ||
| 37 | } | ||
| 38 | payload = { | ||
| 39 | "model": "mistralai/devstral-2512:free", | ||
| 40 | "messages": [ | ||
| 41 | { | ||
| 42 | "role": "system", | ||
| 43 | "content": "respond in JSON format ONLY.\n" | ||
| 44 | "your task is to tell the program what to do and how to respond after receiving a message.\n" | ||
| 45 | "in the json, only fields need to be filled out, message and action\n" | ||
| 46 | "message is the response that the program will send the user, " | ||
| 47 | "and action is the action the program will do\n" | ||
| 48 | "action can either be: compress or download.\n" | ||
| 49 | "message field has to be filled out in polish.\n" | ||
| 50 | "act like jarvis, be a personal assistant to user, do not use emojis" | ||
| 51 | }, | ||
| 52 | { | ||
| 53 | "role": "user", | ||
| 54 | "content": f"{message.content}" | ||
| 55 | } | ||
| 56 | ] | ||
| 57 | } | ||
| 58 | |||
| 59 | response = requests.post(url, headers=headers, json=payload) | ||
| 60 | # Parse the JSON response | ||
| 61 | response_data = response.json() | ||
| 62 | logger.info(response_data) | ||
| 63 | # Extract the content string | ||
| 64 | content = response_data['choices'][0]['message']['content'] | ||
| 65 | |||
| 66 | # Remove the code block markers and parse the inner JSON | ||
| 67 | inner_json_str = content.strip('```json\n').strip('```').strip() | ||
| 68 | inner_data = json.loads(inner_json_str) | ||
| 69 | |||
| 70 | # Now you can access the message and action | ||
| 71 | response_message = inner_data['message'] # "Proszę podać plik do kompresji lub link do pobrania." | ||
| 72 | action = inner_data['action'] # "none OR download OR compress" | ||
| 73 | channel = message.channel | ||
| 74 | logger.info(response_message) | ||
| 75 | logger.debug(action) | ||
| 76 | |||
| 77 | await channel.send(response_message) | ||
| 78 | |||
| 79 | if action == "download": | ||
| 80 | # Extract the URL from the message using regex | ||
| 81 | url_match = re.search(r'(https?://\S+)', message.content).group(0) | ||
| 82 | if not url_match: | ||
| 83 | await channel.send("Nie znaleziono poprawnego linku w odpowiedzi.") | ||
| 84 | return | ||
| 85 | |||
| 86 | response = await Downloader.download_standalone(url=url_match) | ||
| 87 | if isinstance(response, dict): | ||
| 88 | if response["status"] == "success": | ||
| 89 | if response["type"] == "url": | ||
| 90 | await channel.send(f"pobioron i wysyłon: {response['content']}") | ||
| 91 | elif response["type"] == "file": | ||
| 92 | file_data = response["content"] | ||
| 93 | await channel.send("wysyłon:", file=discord.File(file_data)) | ||
| 94 | else: | ||
| 95 | logger.error(f"Error: {response['content']}") | ||
| 96 | if 'details' in response: | ||
| 97 | logger.error(f"Details: {response['details']}") | ||
| 98 | |||
| 99 | elif action == "compress": | ||
| 100 | pass | ||
| 101 | |||
| 102 | def setup(bot): | ||
| 103 | bot.add_cog(Jarvis(bot)) \ No newline at end of file | ||
