File size: 6,464 Bytes
1fefee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import hashlib
import html
import json
import os
import urllib.parse
import uuid


def safe_eval_math(expr: str):
    expr = expr.replace(",", ".").replace("^", "**")
    allowed = set("0123456789+-*/%(). **")
    if not set(expr) <= allowed:
        raise ValueError("Solo se permiten números y operadores básicos.")
    code = compile(expr, "<calc>", "eval")
    if code.co_names:
        raise ValueError("La expresión contiene nombres no permitidos.")
    return eval(code, {"__builtins__": {}}, {})


def setup(registry):
    async def calc_cmd(ctx):
        text = await ctx.require_text("Escribe una operación. Ejemplo: <code>.calc (10+5)*2</code>")
        if not text:
            return
        try:
            result = safe_eval_math(text)
        except Exception as exc:
            await ctx.respond(f"❌ No pude calcular eso:\n<code>{html.escape(str(exc))}</code>")
            return
        await ctx.respond(
            "🧮 <b>Calculadora</b>\n\n"
            f"📥 <b>Expresión:</b> <code>{html.escape(text)}</code>\n"
            f"📤 <b>Resultado:</b> <code>{result}</code>"
        )

    async def hash_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto o responde a un mensaje para generar hashes.")
        if not raw:
            return
        data = raw.encode("utf-8")
        await ctx.respond(
            "🔐 <b>Hashes</b>\n\n"
            f"MD5: <code>{hashlib.md5(data).hexdigest()}</code>\n"
            f"SHA1: <code>{hashlib.sha1(data).hexdigest()}</code>\n"
            f"SHA256: <code>{hashlib.sha256(data).hexdigest()}</code>"
        )

    async def b64e_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto para codificar en Base64.")
        if not raw:
            return
        encoded = base64.b64encode(raw.encode()).decode()
        await ctx.respond(f"🧾 <b>Base64 encode</b>\n\n<code>{encoded}</code>")

    async def b64d_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto en Base64 para decodificar.")
        if not raw:
            return
        try:
            decoded = base64.b64decode(raw.encode()).decode(errors="ignore")
        except Exception as exc:
            await ctx.respond(f"❌ Base64 inválido: <code>{html.escape(str(exc))}</code>")
            return
        await ctx.respond(f"📤 <b>Base64 decode</b>\n\n<code>{html.escape(decoded)}</code>")

    async def urlencode_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto para URL-encode.")
        if not raw:
            return
        await ctx.respond(f"🌐 <b>URL encode</b>\n\n<code>{urllib.parse.quote(raw)}</code>")

    async def urldecode_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto para URL-decode.")
        if not raw:
            return
        await ctx.respond(f"🌐 <b>URL decode</b>\n\n<code>{html.escape(urllib.parse.unquote(raw))}</code>")

    async def reverse_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto para darle la vuelta.")
        if not raw:
            return
        await ctx.respond(f"🔁 <b>Texto invertido</b>\n\n<code>{html.escape(raw[::-1])}</code>")

    async def upper_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto para ponerlo en mayúsculas.")
        if not raw:
            return
        await ctx.respond(f"🔠 <b>Mayúsculas</b>\n\n<code>{html.escape(raw.upper())}</code>")

    async def lower_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto para ponerlo en minúsculas.")
        if not raw:
            return
        await ctx.respond(f"🔡 <b>Minúsculas</b>\n\n<code>{html.escape(raw.lower())}</code>")

    async def title_cmd(ctx):
        raw = await ctx.require_text("Pásame un texto para capitalizarlo.")
        if not raw:
            return
        await ctx.respond(f"📝 <b>Capitalizado</b>\n\n<code>{html.escape(raw.title())}</code>")

    async def uuid_cmd(ctx):
        await ctx.respond(f"🆔 <b>UUID</b>\n\n<code>{uuid.uuid4()}</code>")

    async def json_cmd(ctx):
        raw = await ctx.require_text("Pásame JSON crudo o responde a un mensaje que lo tenga.")
        if not raw:
            return
        try:
            parsed = json.loads(raw)
        except Exception as exc:
            await ctx.respond(f"❌ JSON inválido: <code>{html.escape(str(exc))}</code>")
            return
        pretty = json.dumps(parsed, ensure_ascii=False, indent=2)
        await ctx.respond(f"🧩 <b>JSON bonito</b>\n\n<code>{html.escape(pretty[:3500])}</code>")

    async def pwd_cmd(ctx):
        size = 16
        if ctx.args.strip().isdigit():
            size = max(8, min(64, int(ctx.args.strip())))
        alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*_-"
        token = "".join(alphabet[ord(os.urandom(1)) % len(alphabet)] for _ in range(size))
        await ctx.respond(f"🔑 <b>Contraseña aleatoria</b>\n\n<code>{token}</code>")

    registry.add("calc", calc_cmd, "Calculadora simple segura.", usage=".calc (10+5)*2", category="Herramientas")
    registry.add("hash", hash_cmd, "Genera MD5, SHA1 y SHA256.", usage=".hash hola", category="Herramientas")
    registry.add("b64e", b64e_cmd, "Codifica texto en Base64.", usage=".b64e hola", category="Herramientas")
    registry.add("b64d", b64d_cmd, "Decodifica texto Base64.", usage=".b64d aG9sYQ==", category="Herramientas")
    registry.add("urlencode", urlencode_cmd, "Codifica texto para URL.", usage=".urlencode hola mundo", category="Herramientas")
    registry.add("urldecode", urldecode_cmd, "Decodifica texto URL.", usage=".urldecode hola%20mundo", aliases=["udecode"], category="Herramientas")
    registry.add("reverse", reverse_cmd, "Invierte un texto.", usage=".reverse hola", category="Herramientas")
    registry.add("upper", upper_cmd, "Convierte a mayúsculas.", usage=".upper hola", category="Herramientas")
    registry.add("lower", lower_cmd, "Convierte a minúsculas.", usage=".lower HOLA", category="Herramientas")
    registry.add("title", title_cmd, "Capitaliza un texto.", usage=".title hola mundo", category="Herramientas")
    registry.add("uuid", uuid_cmd, "Genera un UUID aleatorio.", usage=".uuid", category="Herramientas")
    registry.add("json", json_cmd, "Formatea JSON bonito.", usage=".json {\"ok\":true}", category="Herramientas")
    registry.add("pwd", pwd_cmd, "Genera una contraseña aleatoria.", usage=".pwd 20", aliases=["pass"], category="Herramientas")