# Path: embedmaker/serialize.py
# def deserialize_embed(conf: dict) -> discord.Embed:
# 
#     unpack = {k: v for k, v in conf["initable"].items() if v}
# 
#     if "timestamp" in unpack:
#         unpack["timestamp"] = dt.utcfromtimestamp(unpack["timestamp"])
# 
#     e = discord.Embed(**unpack)
# 
#     for k, v in conf["settable"].items():
#         if v is not None:
#             to_set = {_k: _v for _k, _v in v.items() if _v}
#             getattr(e, "set_" + k)(**to_set)
# 
#     for f in conf["fields"]:
#         e.add_field(**f)
# 
#     return e
# 
# def serialize_embed(embed: discord.Embed) -> dict:
# 
#     ret: dict = {"initable": {}, "settable": {}}
# 
#     for k in template["initable"].keys():
#         v = getattr(embed, k, None) or None
#         if v is None:
#             continue
#         if k == "timestamp" and v:
#             v = v.timestamp()
#         if k == "color" and v:
#             v = v.value
#         ret["initable"][k] = v
# 
#     for k, v in template["settable"].items():
#         proxy = getattr(embed, k, None) or None
#         if proxy is None:
#             continue
#         ret["settable"][k] = {}
#         for attr in v.keys():
#             to_set = getattr(proxy, attr, None)
#             if to_set or to_set is False:  # specifically not None or embed.Empty
#                 ret["settable"][k][attr] = to_set
# 
#     ret["fields"] = []
#     for field in embed.fields:
#         data = {}
#         for attr in ["name", "value", "inline"]:
#             to_set = getattr(field, attr, None)
#             if to_set or to_set is False:  # specifically not None or embed.Empty
#                 data[attr] = to_set
#         if data:
#             ret["fields"].append(data)
# 
#     return ret
# 
# Path: embedmaker/yaml_parse.py
# async def embed_from_userstr(ctx: commands.Context, string: str) -> discord.Embed:
#     ret: dict = {"initable": {}, "settable": {}, "fields": []}
#     string = string_preprocessor(string)
# 
#     parsed = yaml.safe_load(string)
#     ret["fields"] = [
#         field_data for _index, field_data in sorted(parsed.get("fields", {}).items())
#     ]
# 
#     for outer_key in ["initable", "settable"]:
#         for inner_key in template[outer_key].keys():
#             to_set = parsed.get(inner_key, {})
#             if to_set:
#                 if inner_key == "timestamp":
#                     to_set = handle_timestamp(to_set)
#                 elif inner_key in ("color", "colour"):
#                     to_set = await handle_color(ctx, to_set)
# 
#                 ret[outer_key][inner_key] = to_set
# 
#     return deserialize_embed(ret)


import io
import logging
import random
import discord
from typing import Generator, Literal, Optional, cast
from redbot.core import Config, checks, commands
from redbot.core.utils import AsyncIter
from redbot.core.utils.chat_formatting import pagify
from .serialize import deserialize_embed, serialize_embed
from .time_utils import parse_time
from .yaml_parse import embed_from_userstr
        )
        embed.add_field(name="RBG", value=hex(color.value))
        embed.add_field(name="HSV", value=f"({degree:.3g}\N{DEGREE SIGN}, 75%, 80%)")
        await ctx.send(embed=embed)

    @checks.mod_or_permissions(manage_messages=True)
    @_embed.command(name="editmsg")
    async def editmessage_embed(
        self,
        ctx: commands.GuildContext,
        message: discord.Message,
        embedname: str,
        use_global: bool = False,
    ):
        """
        Edits an existing message by channelID-messageID to have an embed (must be saved)
        """

        if message.guild != ctx.guild:
            return await ctx.send("Use this command in the same server as the message.")

        if message.author != ctx.guild.me:
            return await ctx.send("Not my message, can't edit")

        grp = self.config.custom(
            "EMBED", ("GLOBAL" if use_global else ctx.guild.id), embedname
        )

        if await grp.owner():
            data = await grp.embed()

# Path: embedmaker/serialize.py
# def deserialize_embed(conf: dict) -> discord.Embed:
# 
#     unpack = {k: v for k, v in conf["initable"].items() if v}
# 
#     if "timestamp" in unpack:
#         unpack["timestamp"] = dt.utcfromtimestamp(unpack["timestamp"])
# 
#     e = discord.Embed(**unpack)
# 
#     for k, v in conf["settable"].items():
#         if v is not None:
#             to_set = {_k: _v for _k, _v in v.items() if _v}
#             getattr(e, "set_" + k)(**to_set)
# 
#     for f in conf["fields"]:
#         e.add_field(**f)
# 
#     return e
# 
# def serialize_embed(embed: discord.Embed) -> dict:
# 
#     ret: dict = {"initable": {}, "settable": {}}
# 
#     for k in template["initable"].keys():
#         v = getattr(embed, k, None) or None
#         if v is None:
#             continue
#         if k == "timestamp" and v:
#             v = v.timestamp()
#         if k == "color" and v:
#             v = v.value
#         ret["initable"][k] = v
# 
#     for k, v in template["settable"].items():
#         proxy = getattr(embed, k, None) or None
#         if proxy is None:
#             continue
#         ret["settable"][k] = {}
#         for attr in v.keys():
#             to_set = getattr(proxy, attr, None)
#             if to_set or to_set is False:  # specifically not None or embed.Empty
#                 ret["settable"][k][attr] = to_set
# 
#     ret["fields"] = []
#     for field in embed.fields:
#         data = {}
#         for attr in ["name", "value", "inline"]:
#             to_set = getattr(field, attr, None)
#             if to_set or to_set is False:  # specifically not None or embed.Empty
#                 data[attr] = to_set
#         if data:
#             ret["fields"].append(data)
# 
#     return ret
# 
# Path: embedmaker/yaml_parse.py
# async def embed_from_userstr(ctx: commands.Context, string: str) -> discord.Embed:
#     ret: dict = {"initable": {}, "settable": {}, "fields": []}
#     string = string_preprocessor(string)
# 
#     parsed = yaml.safe_load(string)
#     ret["fields"] = [
#         field_data for _index, field_data in sorted(parsed.get("fields", {}).items())
#     ]
# 
#     for outer_key in ["initable", "settable"]:
#         for inner_key in template[outer_key].keys():
#             to_set = parsed.get(inner_key, {})
#             if to_set:
#                 if inner_key == "timestamp":
#                     to_set = handle_timestamp(to_set)
#                 elif inner_key in ("color", "colour"):
#                     to_set = await handle_color(ctx, to_set)
# 
#                 ret[outer_key][inner_key] = to_set
# 
#     return deserialize_embed(ret)


# Path: embedmaker/embedmaker.py
import io
import logging
import random
import discord
from typing import Generator, Literal, Optional, cast
from redbot.core import Config, checks, commands
from redbot.core.utils import AsyncIter
from redbot.core.utils.chat_formatting import pagify
from .serialize import deserialize_embed, serialize_embed
from .time_utils import parse_time
from .yaml_parse import embed_from_userstr

        )
        embed.add_field(name="RBG", value=hex(color.value))
        embed.add_field(name="HSV", value=f"({degree:.3g}\N{DEGREE SIGN}, 75%, 80%)")
        await ctx.send(embed=embed)

    @checks.mod_or_permissions(manage_messages=True)
    @_embed.command(name="editmsg")
    async def editmessage_embed(
        self,
        ctx: commands.GuildContext,
        message: discord.Message,
        embedname: str,
        use_global: bool = False,
    ):
        """
        Edits an existing message by channelID-messageID to have an embed (must be saved)
        """

        if message.guild != ctx.guild:
            return await ctx.send("Use this command in the same server as the message.")

        if message.author != ctx.guild.me:
            return await ctx.send("Not my message, can't edit")

        grp = self.config.custom(
            "EMBED", ("GLOBAL" if use_global else ctx.guild.id), embedname
        )

        if await grp.owner():
            data = await grp.embed()

            embed = deserialize_embed(data)