import base64
import hashlib
import urllib.parse
import binascii
import json
from html import escape, unescape
from app import BOT, Message
@BOT.add_cmd(cmd="encode")
async def encode_text(bot: BOT, message: Message):
"""
CMD: ENCODE
INFO: Encode text using various encoding methods.
FLAGS: -b64 (base64), -url (URL encode), -hex (hexadecimal), -html (HTML entities)
USAGE: .encode -b64 Hello World
.encode -url Hello World!
.encode -hex Secret Text
"""
text = message.filtered_input
if not text:
await message.reply("❌ No text provided!\n"
"Usage: .encode -[method] [text]\n\n"
"Available methods:\n"
"• -b64 - Base64 encoding\n"
"• -url - URL encoding\n"
"• -hex - Hexadecimal encoding\n"
"• -html - HTML entities encoding")
return
response = await message.reply("🔄 Encoding text...")
try:
results = []
# Base64 encoding
if "-b64" in message.flags:
encoded = base64.b64encode(text.encode('utf-8')).decode('utf-8')
results.append(f"📦 Base64:\n{encoded}")
# URL encoding
if "-url" in message.flags:
encoded = urllib.parse.quote(text, safe='')
results.append(f"🌐 URL Encoded:\n{encoded}")
# Hexadecimal encoding
if "-hex" in message.flags:
encoded = text.encode('utf-8').hex()
results.append(f"🔢 Hexadecimal:\n{encoded}")
# HTML entities encoding
if "-html" in message.flags:
encoded = escape(text)
results.append(f"🌐 HTML Entities:\n{encoded}")
if not results:
# Default to base64 if no method specified
encoded = base64.b64encode(text.encode('utf-8')).decode('utf-8')
results.append(f"📦 Base64 (default):\n{encoded}")
encode_text = f"🔐 Text Encoding Results\n\n"
encode_text += f"Original Text:\n{text}\n\n"
encode_text += "\n\n".join(results)
encode_text += "\n\n✅ Encoding completed!"
await response.edit(encode_text)
except Exception as e:
await response.edit(f"❌ Encoding error:\n{str(e)}")
@BOT.add_cmd(cmd="decode")
async def decode_text(bot: BOT, message: Message):
"""
CMD: DECODE
INFO: Decode text using various decoding methods.
FLAGS: -b64 (base64), -url (URL decode), -hex (hexadecimal), -html (HTML entities)
USAGE: .decode -b64 SGVsbG8gV29ybGQ=
.decode -url Hello%20World%21
.decode -hex 48656c6c6f20576f726c64
"""
text = message.filtered_input
if not text:
await message.reply("❌ No text provided!\n"
"Usage: .decode -[method] [encoded_text]\n\n"
"Available methods:\n"
"• -b64 - Base64 decoding\n"
"• -url - URL decoding\n"
"• -hex - Hexadecimal decoding\n"
"• -html - HTML entities decoding")
return
response = await message.reply("🔄 Decoding text...")
try:
results = []
# Base64 decoding
if "-b64" in message.flags:
try:
decoded = base64.b64decode(text).decode('utf-8')
results.append(f"📦 Base64 Decoded:\n{decoded}")
except Exception as e:
results.append(f"📦 Base64: ❌ {str(e)}")
# URL decoding
if "-url" in message.flags:
try:
decoded = urllib.parse.unquote(text)
results.append(f"🌐 URL Decoded:\n{decoded}")
except Exception as e:
results.append(f"🌐 URL: ❌ {str(e)}")
# Hexadecimal decoding
if "-hex" in message.flags:
try:
decoded = bytes.fromhex(text).decode('utf-8')
results.append(f"🔢 Hexadecimal Decoded:\n{decoded}")
except Exception as e:
results.append(f"🔢 Hexadecimal: ❌ {str(e)}")
# HTML entities decoding
if "-html" in message.flags:
try:
decoded = unescape(text)
results.append(f"🌐 HTML Entities Decoded:\n{decoded}")
except Exception as e:
results.append(f"🌐 HTML: ❌ {str(e)}")
if not results:
# Try to auto-detect and decode
# Try base64 first
try:
decoded = base64.b64decode(text).decode('utf-8')
results.append(f"📦 Auto-detected Base64:\n{decoded}")
except:
# Try hex
try:
decoded = bytes.fromhex(text).decode('utf-8')
results.append(f"🔢 Auto-detected Hexadecimal:\n{decoded}")
except:
# Try URL decode
try:
decoded = urllib.parse.unquote(text)
if decoded != text: # Only show if actually decoded something
results.append(f"🌐 Auto-detected URL:\n{decoded}")
else:
results.append("❌ Could not auto-detect encoding method.")
except:
results.append("❌ Could not auto-detect encoding method.")
decode_text = f"🔓 Text Decoding Results\n\n"
decode_text += f"Encoded Text:\n{text}\n\n"
decode_text += "\n\n".join(results)
decode_text += "\n\n✅ Decoding completed!"
await response.edit(decode_text)
except Exception as e:
await response.edit(f"❌ Decoding error:\n{str(e)}")
@BOT.add_cmd(cmd="hash")
async def hash_text(bot: BOT, message: Message):
"""
CMD: HASH
INFO: Generate various hash values for text.
FLAGS: -md5, -sha1, -sha256, -sha512, -all (for all hashes)
USAGE: .hash -sha256 Hello World
.hash -all Secret Text
"""
text = message.filtered_input
if not text:
await message.reply("❌ No text provided!\n"
"Usage: .hash -[method] [text]\n\n"
"Available methods:\n"
"• -md5 - MD5 hash\n"
"• -sha1 - SHA1 hash\n"
"• -sha256 - SHA256 hash\n"
"• -sha512 - SHA512 hash\n"
"• -all - All hash methods")
return
response = await message.reply("🔄 Generating hashes...")
try:
text_bytes = text.encode('utf-8')
results = []
# Generate specific hashes based on flags
if "-md5" in message.flags or "-all" in message.flags:
md5_hash = hashlib.md5(text_bytes).hexdigest()
results.append(f"🔐 MD5:\n{md5_hash}")
if "-sha1" in message.flags or "-all" in message.flags:
sha1_hash = hashlib.sha1(text_bytes).hexdigest()
results.append(f"🔐 SHA1:\n{sha1_hash}")
if "-sha256" in message.flags or "-all" in message.flags:
sha256_hash = hashlib.sha256(text_bytes).hexdigest()
results.append(f"🔐 SHA256:\n{sha256_hash}")
if "-sha512" in message.flags or "-all" in message.flags:
sha512_hash = hashlib.sha512(text_bytes).hexdigest()
results.append(f"🔐 SHA512:\n{sha512_hash}")
if not results:
# Default to SHA256 if no method specified
sha256_hash = hashlib.sha256(text_bytes).hexdigest()
results.append(f"🔐 SHA256 (default):\n{sha256_hash}")
hash_text = f"🔐 Hash Generation Results\n\n"
hash_text += f"Original Text:\n{text}\n\n"
hash_text += "\n\n".join(results)
hash_text += "\n\n✅ Hash generation completed!"
await response.edit(hash_text)
except Exception as e:
await response.edit(f"❌ Hash generation error:\n{str(e)}")
@BOT.add_cmd(cmd="json")
async def json_formatter(bot: BOT, message: Message):
"""
CMD: JSON
INFO: Format, validate and minify JSON data.
FLAGS: -pretty (format), -minify (minify), -validate (validate only)
USAGE: .json -pretty {"name":"John","age":30}
.json -minify { "formatted": "json" }
"""
json_text = message.filtered_input
if not json_text:
await message.reply("❌ No JSON provided!\n"
"Usage: .json -[method] [json_data]\n\n"
"Available methods:\n"
"• -pretty - Format JSON with indentation\n"
"• -minify - Minify JSON (remove whitespace)\n"
"• -validate - Validate JSON syntax only")
return
response = await message.reply("🔄 Processing JSON...")
try:
# Parse JSON to validate
parsed_json = json.loads(json_text)
results = []
if "-pretty" in message.flags:
pretty_json = json.dumps(parsed_json, indent=2, ensure_ascii=False)
results.append(f"📋 Pretty Formatted:\n
{pretty_json}")
if "-minify" in message.flags:
minified_json = json.dumps(parsed_json, separators=(',', ':'), ensure_ascii=False)
results.append(f"🗜️ Minified:\n{minified_json}")
if "-validate" in message.flags:
results.append("✅ JSON is valid!")
if not results:
# Default to pretty format
pretty_json = json.dumps(parsed_json, indent=2, ensure_ascii=False)
results.append(f"📋 Pretty Formatted (default):\n{pretty_json}")
json_result = f"📋 JSON Processing Results\n\n"
json_result += "\n\n".join(results)
json_result += "\n\n✅ JSON processing completed!"
await response.edit(json_result)
except json.JSONDecodeError as e:
await response.edit(f"❌ Invalid JSON!\n"
f"Error: {str(e)}\n"
f"Input: {json_text}")
except Exception as e:
await response.edit(f"❌ JSON processing error:\n{str(e)}")