# Path: scripts/discord_utils.py
# def try_get_member(ctx: Context, ex=None) -> Member:
#     """
#     Try to get a member from the context.
#     :param ctx: the context.
#     :param ex: the exception to be raised if member cannot be found.
#     :return: the member.
#     :raises: ex, see :param ex
#     """
#     try:
#         return get(ctx.guild.members, id=ctx.author.id)
#     except Exception as e:
#         raise ex or e


from discord import DMChannel, TextChannel
from discord.ext.commands import CommandError, Context, NoPrivateMessage, \
    NotOwner
from scripts.discord_utils import try_get_member
class AdminError(CommandError):
    pass


class ManageMessageError(CommandError):
    pass


def is_nsfw(ctx: Context):
    """
    Detiremine if nsfw is enabled for this channel
    :param ctx: the context
    :return: if nsfw is enabled in this channel
    """
    channel = ctx.channel
    if isinstance(channel, DMChannel):
        return True
    if isinstance(channel, TextChannel) and channel.is_nsfw():
        return True
    raise NsfwError('NSFW commands must be used in DM '
                    'or a channel with NSFW enabled.')


def has_manage_role(ctx: Context):
    """
    Check if an user has the manage_roles permissions
    :param ctx: the context
    :return: True if the user has the manage_roles permissions
    :rtype: bool
    """

# Path: scripts/discord_utils.py
# def try_get_member(ctx: Context, ex=None) -> Member:
#     """
#     Try to get a member from the context.
#     :param ctx: the context.
#     :param ex: the exception to be raised if member cannot be found.
#     :return: the member.
#     :raises: ex, see :param ex
#     """
#     try:
#         return get(ctx.guild.members, id=ctx.author.id)
#     except Exception as e:
#         raise ex or e


# Path: scripts/checks.py
from discord import DMChannel, TextChannel
from discord.ext.commands import CommandError, Context, NoPrivateMessage, \
    NotOwner
from scripts.discord_utils import try_get_member

class AdminError(CommandError):
    pass


class ManageMessageError(CommandError):
    pass


def is_nsfw(ctx: Context):
    """
    Detiremine if nsfw is enabled for this channel
    :param ctx: the context
    :return: if nsfw is enabled in this channel
    """
    channel = ctx.channel
    if isinstance(channel, DMChannel):
        return True
    if isinstance(channel, TextChannel) and channel.is_nsfw():
        return True
    raise NsfwError('NSFW commands must be used in DM '
                    'or a channel with NSFW enabled.')


def has_manage_role(ctx: Context):
    """
    Check if an user has the manage_roles permissions
    :param ctx: the context
    :return: True if the user has the manage_roles permissions
    :rtype: bool
    """

    member = try_get_member(ctx, ManageRoleError)