aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/qr.py
diff options
context:
space:
mode:
authorsalmonik <raisersoft@proton.me>2026-04-18 00:24:50 +0200
committersalmonik <raisersoft@proton.me>2026-04-18 00:24:50 +0200
commita2b5b10625126b7e98643af9fe40569c9e004047 (patch)
tree2b59a929d068bc28bbc21e8c8d8836e4d29270c0 /cogs/qr.py
parentadd mathematical plotting (diff)
downloadsenator-sperma-a2b5b10625126b7e98643af9fe40569c9e004047.tar.gz
senator-sperma-a2b5b10625126b7e98643af9fe40569c9e004047.zip
refactor + preparing bot for public use
Signed-off-by: salmonik <raisersoft@proton.me>
Diffstat (limited to 'cogs/qr.py')
-rw-r--r--cogs/qr.py46
1 files changed, 29 insertions, 17 deletions
diff --git a/cogs/qr.py b/cogs/qr.py
index 058dac8..c1fd499 100644
--- a/cogs/qr.py
+++ b/cogs/qr.py
@@ -15,14 +15,26 @@ from discord.ext import commands
15 15
16logger = logging.getLogger(__name__) 16logger = logging.getLogger(__name__)
17 17
18try: 18_pil_image = None
19 from PIL import Image 19_decode_func = None
20 from pyzbar.pyzbar import decode as decode_qr 20_decode_available = False
21 21
22 DECODE_AVAILABLE = True 22
23except ImportError: 23def _ensure_qr():
24 DECODE_AVAILABLE = False 24 global _pil_image, _decode_func, _decode_available
25 logger.warning("pyzbar or PIL not available, decode command will be disabled") 25
26 if _pil_image is not None:
27 return _decode_available
28
29 try:
30 from PIL import Image
31 from pyzbar.pyzbar import decode as _decode_func
32 _pil_image = Image
33 _decode_available = True
34 except ImportError:
35 _decode_available = False
36 logger.warning("pyzbar or PIL not available, decode command will be disabled")
37 return _decode_available
26 38
27 39
28class QR(commands.Cog): 40class QR(commands.Cog):
@@ -50,10 +62,9 @@ class QR(commands.Cog):
50 ): 62 ):
51 await ctx.defer() 63 await ctx.defer()
52 64
53 temp_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "temp") 65 from cogs.utils.temp import get_temp_dir
54 os.makedirs(temp_dir, exist_ok=True)
55 temp_path = os.path.join( 66 temp_path = os.path.join(
56 temp_dir, f"qr_{os.getpid()}_{asyncio.get_event_loop().time()}.png" 67 get_temp_dir(), f"qr_{os.getpid()}_{asyncio.get_event_loop().time()}.png"
57 ) 68 )
58 69
59 try: 70 try:
@@ -75,7 +86,7 @@ class QR(commands.Cog):
75 """Generate QR code (runs in executor)""" 86 """Generate QR code (runs in executor)"""
76 qr = qrcode.QRCode( 87 qr = qrcode.QRCode(
77 version=1, 88 version=1,
78 error_correction=qrcode.constants.ERROR_CORRECT_L, 89 error_correction=qrcode.constants.ERROR_CORRECT_L, # type: ignore[attr-defined]
79 box_size=10, 90 box_size=10,
80 border=4, 91 border=4,
81 ) 92 )
@@ -92,7 +103,7 @@ class QR(commands.Cog):
92 discord.Attachment, "Obrazek z kodem QR", required=True 103 discord.Attachment, "Obrazek z kodem QR", required=True
93 ), 104 ),
94 ): 105 ):
95 if not DECODE_AVAILABLE: 106 if not _ensure_qr():
96 await ctx.respond( 107 await ctx.respond(
97 "Dekodowanie nie jest dostępne (brak biblioteki pyzbar)", ephemeral=True 108 "Dekodowanie nie jest dostępne (brak biblioteki pyzbar)", ephemeral=True
98 ) 109 )
@@ -126,11 +137,12 @@ class QR(commands.Cog):
126 137
127 def _decode_qr(self, image_path): 138 def _decode_qr(self, image_path):
128 """Decode QR code from image (runs in executor)""" 139 """Decode QR code from image (runs in executor)"""
129 try: 140 if not _ensure_qr():
130 from PIL import Image 141 return None
131 142
132 img = Image.open(image_path) 143 try:
133 decoded_list = decode_qr(img) 144 img = _pil_image.open(image_path)
145 decoded_list = _decode_func(img)
134 if decoded_list: 146 if decoded_list:
135 return decoded_list[0].data.decode("utf-8") 147 return decoded_list[0].data.decode("utf-8")
136 except Exception as e: 148 except Exception as e: