diff options
| author | salmonik <raisersoft@proton.me> | 2026-04-17 19:40:48 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-04-17 19:40:48 +0200 |
| commit | 0d1163d5e065c20467351d1e0e332eb40dae1133 (patch) | |
| tree | c2129da1f6ce0641bb2a7a983a5434f2bf3bab3d | |
| parent | add cron functionality (diff) | |
| download | senator-sperma-0d1163d5e065c20467351d1e0e332eb40dae1133.tar.gz senator-sperma-0d1163d5e065c20467351d1e0e332eb40dae1133.zip | |
add qr generation and decoding
Signed-off-by: salmonik <raisersoft@proton.me>
| -rw-r--r-- | cogs/qr.py | 142 | ||||
| -rw-r--r-- | requirements.txt | 9 |
2 files changed, 150 insertions, 1 deletions
diff --git a/cogs/qr.py b/cogs/qr.py new file mode 100644 index 0000000..058dac8 --- /dev/null +++ b/cogs/qr.py | |||
| @@ -0,0 +1,142 @@ | |||
| 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 asyncio | ||
| 7 | import logging | ||
| 8 | import os | ||
| 9 | import tempfile | ||
| 10 | |||
| 11 | import aiohttp | ||
| 12 | import discord | ||
| 13 | import qrcode | ||
| 14 | from discord.ext import commands | ||
| 15 | |||
| 16 | logger = logging.getLogger(__name__) | ||
| 17 | |||
| 18 | try: | ||
| 19 | from PIL import Image | ||
| 20 | from pyzbar.pyzbar import decode as decode_qr | ||
| 21 | |||
| 22 | DECODE_AVAILABLE = True | ||
| 23 | except ImportError: | ||
| 24 | DECODE_AVAILABLE = False | ||
| 25 | logger.warning("pyzbar or PIL not available, decode command will be disabled") | ||
| 26 | |||
| 27 | |||
| 28 | class QR(commands.Cog): | ||
| 29 | def __init__(self, bot): | ||
| 30 | self.bot = bot | ||
| 31 | |||
| 32 | async def _download_image(self, url): | ||
| 33 | """Download image from URL to temporary file""" | ||
| 34 | async with aiohttp.ClientSession() as session: | ||
| 35 | async with session.get(url) as response: | ||
| 36 | if response.status == 200: | ||
| 37 | content = await response.read() | ||
| 38 | with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as f: | ||
| 39 | f.write(content) | ||
| 40 | return f.name | ||
| 41 | return None | ||
| 42 | |||
| 43 | @commands.slash_command( | ||
| 44 | name="qr_generuj", description="Generuje kod QR z podanego tekstu" | ||
| 45 | ) | ||
| 46 | async def qr( | ||
| 47 | self, | ||
| 48 | ctx, | ||
| 49 | text: str = discord.Option(str, "Tekst do zakodowania", required=True), | ||
| 50 | ): | ||
| 51 | await ctx.defer() | ||
| 52 | |||
| 53 | temp_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "temp") | ||
| 54 | os.makedirs(temp_dir, exist_ok=True) | ||
| 55 | temp_path = os.path.join( | ||
| 56 | temp_dir, f"qr_{os.getpid()}_{asyncio.get_event_loop().time()}.png" | ||
| 57 | ) | ||
| 58 | |||
| 59 | try: | ||
| 60 | loop = asyncio.get_event_loop() | ||
| 61 | await loop.run_in_executor(None, self._generate_qr, text, temp_path) | ||
| 62 | await ctx.respond(file=discord.File(temp_path, filename="qr.png")) | ||
| 63 | logger.info(f"Generated QR for text: {text}") | ||
| 64 | except Exception as e: | ||
| 65 | logger.error(f"QR generation error: {e}") | ||
| 66 | await ctx.respond("Error generating QR code", ephemeral=True) | ||
| 67 | finally: | ||
| 68 | try: | ||
| 69 | if os.path.exists(temp_path): | ||
| 70 | os.unlink(temp_path) | ||
| 71 | except Exception as e: | ||
| 72 | logger.error(f"Error cleaning up temp file: {e}") | ||
| 73 | |||
| 74 | def _generate_qr(self, text, output_path): | ||
| 75 | """Generate QR code (runs in executor)""" | ||
| 76 | qr = qrcode.QRCode( | ||
| 77 | version=1, | ||
| 78 | error_correction=qrcode.constants.ERROR_CORRECT_L, | ||
| 79 | box_size=10, | ||
| 80 | border=4, | ||
| 81 | ) | ||
| 82 | qr.add_data(text) | ||
| 83 | qr.make(fit=True) | ||
| 84 | img = qr.make_image(fill_color="black", back_color="white") | ||
| 85 | img.save(output_path) | ||
| 86 | |||
| 87 | @commands.slash_command(name="qr_dekoduj", description="Dekoduje kod QR z obrazka") | ||
| 88 | async def decode( | ||
| 89 | self, | ||
| 90 | ctx, | ||
| 91 | image: discord.Attachment = discord.Option( | ||
| 92 | discord.Attachment, "Obrazek z kodem QR", required=True | ||
| 93 | ), | ||
| 94 | ): | ||
| 95 | if not DECODE_AVAILABLE: | ||
| 96 | await ctx.respond( | ||
| 97 | "Dekodowanie nie jest dostępne (brak biblioteki pyzbar)", ephemeral=True | ||
| 98 | ) | ||
| 99 | return | ||
| 100 | |||
| 101 | await ctx.defer() | ||
| 102 | |||
| 103 | try: | ||
| 104 | temp_path = await self._download_image(image.url) | ||
| 105 | if not temp_path: | ||
| 106 | await ctx.respond("Nie udało się pobrać obrazka", ephemeral=True) | ||
| 107 | return | ||
| 108 | |||
| 109 | try: | ||
| 110 | loop = asyncio.get_event_loop() | ||
| 111 | decoded = await loop.run_in_executor(None, self._decode_qr, temp_path) | ||
| 112 | |||
| 113 | if decoded: | ||
| 114 | await ctx.respond(f"```\n{decoded}\n```") | ||
| 115 | logger.info(f"Decoded QR: {decoded}") | ||
| 116 | else: | ||
| 117 | await ctx.respond( | ||
| 118 | "Nie znaleziono kodu QR na obrazku", ephemeral=True | ||
| 119 | ) | ||
| 120 | finally: | ||
| 121 | if os.path.exists(temp_path): | ||
| 122 | os.unlink(temp_path) | ||
| 123 | except Exception as e: | ||
| 124 | logger.error(f"QR decode error: {e}") | ||
| 125 | await ctx.respond("Error dekodowania QR", ephemeral=True) | ||
| 126 | |||
| 127 | def _decode_qr(self, image_path): | ||
| 128 | """Decode QR code from image (runs in executor)""" | ||
| 129 | try: | ||
| 130 | from PIL import Image | ||
| 131 | |||
| 132 | img = Image.open(image_path) | ||
| 133 | decoded_list = decode_qr(img) | ||
| 134 | if decoded_list: | ||
| 135 | return decoded_list[0].data.decode("utf-8") | ||
| 136 | except Exception as e: | ||
| 137 | logger.error(f"Error decoding QR: {e}") | ||
| 138 | return None | ||
| 139 | |||
| 140 | |||
| 141 | def setup(bot): | ||
| 142 | bot.add_cog(QR(bot)) | ||
diff --git a/requirements.txt b/requirements.txt index e3883e0..e5dc093 100644 --- a/requirements.txt +++ b/requirements.txt | |||
| @@ -10,6 +10,8 @@ certifi==2026.2.25 | |||
| 10 | cffi==2.0.0 | 10 | cffi==2.0.0 |
| 11 | charset-normalizer==3.4.6 | 11 | charset-normalizer==3.4.6 |
| 12 | click==8.1.8 | 12 | click==8.1.8 |
| 13 | croniter==6.2.2 | ||
| 14 | davey==0.1.4 | ||
| 13 | dotenv==0.9.9 | 15 | dotenv==0.9.9 |
| 14 | frozenlist==1.8.0 | 16 | frozenlist==1.8.0 |
| 15 | gTTS==2.5.4 | 17 | gTTS==2.5.4 |
| @@ -19,14 +21,19 @@ httpx==0.28.1 | |||
| 19 | idna==3.11 | 21 | idna==3.11 |
| 20 | multidict==6.7.1 | 22 | multidict==6.7.1 |
| 21 | mutagen==1.47.0 | 23 | mutagen==1.47.0 |
| 24 | pillow==12.2.0 | ||
| 22 | propcache==0.4.1 | 25 | propcache==0.4.1 |
| 23 | py-cord==2.7.1 | 26 | py-cord @ file:///home/salmon/Documents/botcode/senator-sperma/pycord |
| 24 | pycparser==3.0 | 27 | pycparser==3.0 |
| 25 | PyNaCl==1.6.2 | 28 | PyNaCl==1.6.2 |
| 29 | python-dateutil==2.9.0.post0 | ||
| 26 | python-dotenv==1.2.2 | 30 | python-dotenv==1.2.2 |
| 27 | pytube==15.0.0 | 31 | pytube==15.0.0 |
| 32 | pyzbar==0.1.9 | ||
| 33 | qrcode==8.2 | ||
| 28 | RapidFuzz==3.14.3 | 34 | RapidFuzz==3.14.3 |
| 29 | requests==2.32.5 | 35 | requests==2.32.5 |
| 36 | six==1.17.0 | ||
| 30 | soupsieve==2.8.3 | 37 | soupsieve==2.8.3 |
| 31 | typing_extensions==4.15.0 | 38 | typing_extensions==4.15.0 |
| 32 | urllib3==2.6.3 | 39 | urllib3==2.6.3 |
