make the bot.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import random
|
||||
import string
|
||||
from io import BytesIO
|
||||
from captcha.image import ImageCaptcha
|
||||
|
||||
def generate_captcha():
|
||||
"""Generates a random captcha and returns (text, image_bytes)"""
|
||||
captcha_text = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
|
||||
image = ImageCaptcha(width=280, height=90)
|
||||
data = image.generate(captcha_text)
|
||||
return captcha_text, data.getvalue()
|
||||
@@ -0,0 +1,31 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
class I18n:
|
||||
def __init__(self, default_locale="en"):
|
||||
self.default_locale = default_locale
|
||||
self.translations = {}
|
||||
self.load_translations()
|
||||
|
||||
def load_translations(self):
|
||||
locales_dir = "locales"
|
||||
if not os.path.exists(locales_dir):
|
||||
return
|
||||
|
||||
for filename in os.listdir(locales_dir):
|
||||
if filename.endswith(".json"):
|
||||
locale = filename[:-5]
|
||||
with open(os.path.join(locales_dir, filename), "r", encoding="utf-8") as f:
|
||||
self.translations[locale] = json.load(f)
|
||||
|
||||
def get(self, key, locale=None, **kwargs):
|
||||
if locale is None:
|
||||
locale = self.default_locale
|
||||
text = self.translations.get(locale, {}).get(key, self.translations.get(self.default_locale, {}).get(key, key))
|
||||
try:
|
||||
return text.format(**kwargs)
|
||||
except (KeyError, IndexError):
|
||||
return text
|
||||
|
||||
i18n = I18n()
|
||||
_ = i18n.get
|
||||
Reference in New Issue
Block a user