diff options
| author | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-08-21 17:40:39 +0200 |
|---|---|---|
| committer | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-08-21 17:40:39 +0200 |
| commit | 49f6175b6a7b62001d11fd812d63d2b86d93e8c3 (patch) | |
| tree | b0efe23e39f038f1e7039cbb8f0d645d5893fa0f /cogs | |
| parent | small changes (diff) | |
| download | senator-sperma-49f6175b6a7b62001d11fd812d63d2b86d93e8c3.tar.gz senator-sperma-49f6175b6a7b62001d11fd812d63d2b86d93e8c3.zip | |
added conversion.py
Diffstat (limited to 'cogs')
| -rw-r--r-- | cogs/compression.py | 2 | ||||
| -rw-r--r-- | cogs/conversion.py | 80 |
2 files changed, 81 insertions, 1 deletions
diff --git a/cogs/compression.py b/cogs/compression.py index dc8e632..2ee36bb 100644 --- a/cogs/compression.py +++ b/cogs/compression.py | |||
| @@ -29,7 +29,7 @@ class Compression(commands.Cog): | |||
| 29 | description="skompresuj wideło albo dźwienk :)", | 29 | description="skompresuj wideło albo dźwienk :)", |
| 30 | ) | 30 | ) |
| 31 | @discord.option( | 31 | @discord.option( |
| 32 | "plik", | 32 | name="plik", |
| 33 | description="plik do skompresowania", | 33 | description="plik do skompresowania", |
| 34 | required=True, | 34 | required=True, |
| 35 | type=discord.Attachment | 35 | type=discord.Attachment |
diff --git a/cogs/conversion.py b/cogs/conversion.py new file mode 100644 index 0000000..4d5d4d9 --- /dev/null +++ b/cogs/conversion.py | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | import discord | ||
| 2 | from discord.ext import commands | ||
| 3 | import logging | ||
| 4 | import os | ||
| 5 | import subprocess | ||
| 6 | import mimetypes | ||
| 7 | |||
| 8 | logger = logging.getLogger(__name__) | ||
| 9 | |||
| 10 | def filetype(filename): | ||
| 11 | mime_type, _ = mimetypes.guess_type(filename) | ||
| 12 | if mime_type: | ||
| 13 | if mime_type.startswith('video/'): | ||
| 14 | return "Video" | ||
| 15 | elif mime_type.startswith('audio/'): | ||
| 16 | return "Audio" | ||
| 17 | elif mime_type.startswith('image/'): | ||
| 18 | return "Image" | ||
| 19 | return "Unknown" | ||
| 20 | |||
| 21 | class Conversion(commands.Cog): | ||
| 22 | def __init__(self, bot): | ||
| 23 | self.bot = bot | ||
| 24 | |||
| 25 | @commands.slash_command(name="konwersja", description="konwertuj plik na inny format") | ||
| 26 | @discord.option( | ||
| 27 | "plik", | ||
| 28 | description="plik do konwersji", | ||
| 29 | required=True, | ||
| 30 | type=discord.Attachment | ||
| 31 | ) | ||
| 32 | @discord.option( | ||
| 33 | "format", | ||
| 34 | description="format konwersji", | ||
| 35 | required=True, | ||
| 36 | type=str | ||
| 37 | ) | ||
| 38 | async def convert(self, ctx, plik, output_format): | ||
| 39 | await ctx.defer() | ||
| 40 | # Save files to the temp directory inside the bot's root directory | ||
| 41 | bot_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 42 | temp_dir = os.path.join(bot_dir, "temp") | ||
| 43 | if not os.path.exists(temp_dir): | ||
| 44 | os.makedirs(temp_dir) | ||
| 45 | file_path = os.path.join(temp_dir, plik.filename) | ||
| 46 | filename_noext = os.path.splitext(plik.filename)[0] | ||
| 47 | await plik.save(file_path) | ||
| 48 | |||
| 49 | input_path = file_path | ||
| 50 | output_path = os.path.join(temp_dir, f"{filename_noext}.{output_format}") | ||
| 51 | file_type = filetype(file_path) | ||
| 52 | logger.info(f"Conversion started for: {file_type}") | ||
| 53 | if file_type == "Video" or file_type == "Audio": | ||
| 54 | # Execute ffmpeg command to compress video | ||
| 55 | ffmpeg_command = f"ffmpeg -i {input_path} {output_path}" | ||
| 56 | elif file_type == "Image": | ||
| 57 | img_command = f"magick {input_path} {output_path}" | ||
| 58 | ffmpeg_command = img_command | ||
| 59 | else: | ||
| 60 | await ctx.respond("Plik jest nieobsługiwany", ephemeral=True) | ||
| 61 | logger.info("Unsupported file type") | ||
| 62 | return | ||
| 63 | |||
| 64 | try: | ||
| 65 | subprocess.run(ffmpeg_command, check=True, shell=True) | ||
| 66 | # Upload the compressed file | ||
| 67 | await ctx.respond(file=discord.File(output_path)) | ||
| 68 | logger.info(f"Conversion completed for: {file_type}") | ||
| 69 | # Delete compressed file | ||
| 70 | os.remove(input_path) | ||
| 71 | os.remove(output_path) | ||
| 72 | except subprocess.CalledProcessError as e: | ||
| 73 | await ctx.respond(f"Conversion failed: {str(e)}", ephemeral=True) | ||
| 74 | if os.path.exists(input_path): | ||
| 75 | os.remove(input_path) | ||
| 76 | if os.path.exists(output_path): | ||
| 77 | os.remove(output_path) | ||
| 78 | |||
| 79 | def setup(bot): | ||
| 80 | bot.add_cog(Conversion(bot)) \ No newline at end of file | ||
