import functools


def create_dm(cog_function):
    """
        Decorator that creates a Direct Message (dm) and appends it to the current context. 
        Must receive a class function with a Context argument.
        Useful in discord.Cog calls
    """

    # Preserve function name so discord can still call the command
    @functools.wraps(cog_function)
    async def wrapper(cls, ctx, *args, **kwargs):
        ctx.dm = None
        try:
            ctx.dm = await ctx.author.create_dm()
        except:
            print("Could not create dm")
        await cog_function(cls, ctx, *args, **kwargs)
    return wrapper