You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fox-V3/unicode/unicode.py

51 lines
1.6 KiB

import codecs as c
import discord
from redbot.core import commands
from redbot.core.commands import Cog
class Unicode(Cog):
"""Encode/Decode Unicode characters!"""
def __init__(self, bot):
self.bot = bot
6 years ago
@commands.group(name="unicode", pass_context=True)
async def unicode(self, ctx):
"""Encode/Decode a Unicode character."""
if ctx.invoked_subcommand is None:
pass
@unicode.command()
async def decode(self, ctx: commands.Context, character):
"""Decode a Unicode character."""
try:
6 years ago
data = "U+{:04X}".format(ord(character[0]))
color = discord.Color.green()
except ValueError:
6 years ago
data = "<unknown>"
color = discord.Color.red()
em = discord.Embed(title=character, description=data, color=color)
await ctx.send(embed=em)
@unicode.command()
6 years ago
async def encode(self, ctx: commands.Context, character):
"""Encode an Unicode character."""
try:
6 years ago
if character[:2] == "\\u":
data = repr(c.decode(character, "unicode-escape"))
data = data.strip("'")
color = discord.Color.green()
6 years ago
elif character[:2] == "U+":
data = chr(int(character.lstrip("U+"), 16))
color = discord.Color.green()
else:
6 years ago
data = "<unknown>"
color = discord.Color.red()
except ValueError:
6 years ago
data = "<unknown>"
color = discord.Color.red()
em = discord.Embed(title=character, description=data, color=color)
await ctx.send(embed=em)