aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/qr.py
blob: 32c574ed6464685d6f188a850049167cbec4e3c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# This file is part of Senator Sperma
# Licensed under the GPL-3.0 License
# Copyright (C) 2026 Salmonik @ salmonik@tuta.io
# https://codeberg.org/salmonik/senator-sperma

import asyncio
import logging
import os
import tempfile

import aiohttp
import discord
import qrcode
from discord.ext import commands

logger = logging.getLogger(__name__)

_pil_image = None
_decode_func = None
_decode_available = False


def _ensure_qr():
    global _pil_image, _decode_func, _decode_available

    if _pil_image is not None:
        return _decode_available

    try:
        from PIL import Image
        from pyzbar.pyzbar import decode as _decode_func
        _pil_image = Image
        _decode_available = True
    except ImportError:
        _decode_available = False
        logger.warning("pyzbar or PIL not available, decode command will be disabled")
    return _decode_available


class QR(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    async def _download_image(self, url):
        """Download image from URL to temporary file"""
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                if response.status == 200:
                    content = await response.read()
                    with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as f:
                        await asyncio.get_event_loop().run_in_executor(None, f.write, content)
                        return f.name
        return None

    @commands.slash_command(
        name="qr_generuj", description="Generuje kod QR z podanego tekstu"
    )
    async def qr(
        self,
        ctx,
        text: str = discord.Option(str, "Tekst do zakodowania", required=True),
    ):
        await ctx.defer()

        from cogs.utils.temp import get_temp_dir
        temp_path = os.path.join(
            get_temp_dir(), f"qr_{os.getpid()}_{asyncio.get_event_loop().time()}.png"
        )

        try:
            loop = asyncio.get_event_loop()
            await loop.run_in_executor(None, self._generate_qr, text, temp_path)
            await ctx.respond(file=discord.File(temp_path, filename="qr.png"))
            logger.info(f"Generated QR for text: {text}")
        except Exception as e:
            logger.error(f"QR generation error: {e}")
            await ctx.respond("Error generating QR code", ephemeral=True)
        finally:
            try:
                if os.path.exists(temp_path):
                    os.unlink(temp_path)
            except Exception as e:
                logger.error(f"Error cleaning up temp file: {e}")

    def _generate_qr(self, text, output_path):
        """Generate QR code (runs in executor)"""
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,  # type: ignore[attr-defined]
            box_size=10,
            border=4,
        )
        qr.add_data(text)
        qr.make(fit=True)
        img = qr.make_image(fill_color="black", back_color="white")
        img.save(output_path)

    @commands.slash_command(name="qr_dekoduj", description="Dekoduje kod QR z obrazka")
    async def decode(
        self,
        ctx,
        image: discord.Attachment = discord.Option(
            discord.Attachment, "Obrazek z kodem QR", required=True
        ),
    ):
        if not _ensure_qr():
            await ctx.respond(
                "Dekodowanie nie jest dostępne (brak biblioteki pyzbar)", ephemeral=True
            )
            return

        await ctx.defer()

        try:
            temp_path = await self._download_image(image.url)
            if not temp_path:
                await ctx.respond("Nie udało się pobrać obrazka", ephemeral=True)
                return

            try:
                loop = asyncio.get_event_loop()
                decoded = await loop.run_in_executor(None, self._decode_qr, temp_path)

                if decoded:
                    await ctx.respond(f"```\n{decoded}\n```")
                    logger.info(f"Decoded QR: {decoded}")
                else:
                    await ctx.respond(
                        "Nie znaleziono kodu QR na obrazku", ephemeral=True
                    )
            finally:
                if os.path.exists(temp_path):
                    os.unlink(temp_path)
        except Exception as e:
            logger.error(f"QR decode error: {e}")
            await ctx.respond("Error dekodowania QR", ephemeral=True)

    def _decode_qr(self, image_path):
        """Decode QR code from image (runs in executor)"""
        if not _ensure_qr() or _decode_func is None:
            return None

        try:
            img = _pil_image.open(image_path)
            decoded_list = _decode_func(img)
            if decoded_list:
                return decoded_list[0].data.decode("utf-8")
        except Exception as e:
            logger.error(f"Error decoding QR: {e}")
        return None


def setup(bot):
    bot.add_cog(QR(bot))