<filename>cogs/utils/utils.py
import time
import discord
from discord import Member
from discord.ext import commands
from discord.ext.commands import Context

from cogs.utils.config import get_settings
from cogs.utils.values import DEFAULT_COLOR


def log(message):
    print('\n', time.asctime(time.localtime()), ':', message)


async def dm(user: Member, ctx: Context, message: str, confirmation_msg: str = None, display_author=True,
             title=None, color=DEFAULT_COLOR, description=None, name='Direct Message!'):
    """
    Send a direct message to a user
    :param user: A Member object of the user who will receive the DM
    :param ctx: The Context of the request to DM
    :param message: The message to be sent
    :param confirmation_msg: Should there be a confirmation message? If so, what should it say?
    :param display_author: Whether the author should be displayed in the DM
    :param title: Embed title
    :param color: Embed color
    :param description: Embed description (defaults to ctx.author.mention)
    :param name: Name of the message (defaults to 'Direct Message!')
    :return: Returns the DM sent
    """
    embed_description = (ctx.author.mention if description is None else description)

    e = discord.Embed(title=title, color=color, description=embed_description)
    if display_author:
        e.set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
    e.add_field(name=name, value=message)

    if confirmation_msg is not None:
        e2 = discord.Embed(title=confirmation_msg, color=DEFAULT_COLOR, description=ctx.author.mention)
        await ctx.send(embed=e2)

    log('Sent a DM to {.name}!'.format(user))
    return await user.send(embed=e)


async def send_basic(ctx: Context, title: str = None, name: str = None, value: str = None,
                     description=None, display_author=True, color=DEFAULT_COLOR):
    """
    Send a basic message with one text field
    :param ctx: The Context of the command call
    :param title: Embed title
    :param name: Name of the text field
    :param value: Value of the text field
    :param description: Embed description (defaults to ctx.author.mention)
    :param display_author: Whether the author should be displayed in the DM
    :param color: Embed color
    :return: Returns the message sent
    """
    embed_description = (ctx.author.mention if description is None else description)

    e = discord.Embed(title=title, color=color, description=embed_description)
    if display_author:
        e.set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
    if name is not None and value is not None:
        e.add_field(name=name, value=value)

    return await ctx.send(embed=e)


async def fetch_owner(bot: commands.Bot):
    owner_id = get_settings('config.json', 'owner_id')
    return await bot.fetch_user(owner_id)

# def invalid_subcommand(ctx: Context):
#     return await send_basic(ctx,
#                             name=values.Errors.INVALID_SUBCOMMAND['name'],
#                             value=values.Errors.INVALID_SUBCOMMAND['value'].format(ctx))
