<commit_before>from datetime import datetime
from discord import utils, TextChannel
from . import models


def generate_id():
    return utils.time_snowflake(datetime.now())


async def add_permanent_role(user_id: int, role_id: int):
    await add_dbmember_if_not_exist(user_id)
    if not await models.PermanentRole.query.where((models.PermanentRole.user_id == user_id) & (
            models.PermanentRole.role_id == role_id)).gino.first():
        return await models.PermanentRole.create(user_id=user_id, role_id=role_id)


async def remove_permanent_role(user_id: int, role_id: int):
    permanent_role = await models.PermanentRole.query.where((models.PermanentRole.user_id == user_id) & (
                models.PermanentRole.role_id == role_id)).gino.first()
    if permanent_role:
        await permanent_role.delete()
        return permanent_role


async def get_permanent_roles(user_id: int):
    db_member = await get_dbmember(user_id)
    if db_member:
        return await models.PermanentRole.load(parent=db_member).query.gino.all()


async def add_staff(user_id: int, position: str):
    await add_dbmember_if_not_exist(user_id)
    staff = await get_staff(user_id) or await get_helper(user_id)
    if staff:
        await staff.update(position=position).apply()
    else:
        await models.Staff.create(id=user_id, position=position)


async def add_helper(user_id: int, position: str, console: str = None):
    await add_dbmember_if_not_exist(user_id)
    if staff := await get_staff(user_id):
        await staff.update(console=console).apply()
    else:
        await models.Staff.create(id=user_id, position=position, console=console)


async def remove_staff(user_id: int):
    staff = await get_staff(user_id)
    if staff:
        if staff.console:
            await staff.update(position="Helper").apply()
        else:
            await staff.delete()


async def remove_helper(user_id: int):
    helper = await get_helper(user_id)
    if helper:
        if helper.position != "Helper":
            await helper.update(console=None).apply()
        else:
            await helper.delete()


async def get_staff_all():
    return await models.Staff.query.where(models.Staff.position != 'Helper').gino.all()


async def get_staff(user_id: int):
    return await models.Staff.query.where(
        (models.Staff.position != 'Helper') & (models.Staff.id == user_id)).gino.first()


async def get_helpers():
    return await models.Staff.query.where(models.Staff.console.isnot(None)).gino.all()


async def get_helper(user_id: int):
    return await models.Staff.query.where(models.Staff.id == user_id).gino.first()


async def add_warn(user_id: int, issuer_id: int, reason: str):
    await add_dbmember_if_not_exist(user_id)
    await add_dbmember_if_not_exist(issuer_id)
    await models.Warn.create(id=generate_id(), user=user_id, issuer=issuer_id, reason=reason)


async def get_warns(user_id: int):
    return await models.Warn.query.where(models.Warn.user == user_id).gino.all()


async def remove_warn_id(user_id: int, index: int):
    warn = await models.Warn.query.where(models.Warn.user == user_id).offset(index - 1).gino.first()
    await warn.delete()


async def remove_warns(user_id: int):
    n_warns = await (models.db.select([models.db.func.count()]).where(models.Warn.user == user_id).gino.scalar())
    if n_warns:
        await models.Warn.delete.where(models.Warn.user == user_id).gino.status()
    return n_warns


async def add_timed_restriction(user_id: int, end_date: datetime, type: str):
    await add_dbmember_if_not_exist(user_id)
    await models.TimedRestriction.create(id=utils.time_snowflake(datetime.now()), user=user_id, type=type,
                                         end_date=end_date)


async def get_time_restrictions_by_user_type(user_id: int, type: str):
    return await models.TimedRestriction.query.where((models.TimedRestriction.user == user_id) & (
            models.TimedRestriction.type == type)).gino.first()


async def get_time_restrictions_by_type(type: str):
    return await models.TimedRestriction.query.where(models.TimedRestriction.type == type).gino.all()


async def remove_timed_restriction(user_id: int, type: str):
    time_restriction = await get_time_restrictions_by_user_type(user_id, type)
    if time_restriction:
        await time_restriction.delete()


async def set_time_restriction_alert(user_id: int, type: str):
    time_restriction = await get_time_restrictions_by_user_type(user_id, type)
    if time_restriction:
        await time_restriction.update(alerted=True).apply()


async def add_flag(name: str):
    await models.Flag.create(name=name)


async def get_flag(name: str):
    if flag := await models.Flag.get(name):
        return flag.value
    return None


async def remove_flag(name: str):
    flag = await get_flag(name)
    if flag:
        await flag.delete()


async def set_flag(name: str, value: bool):
    flag = await get_flag(name)
    if flag:
        await flag.update(value=value).apply()


async def add_softban(user_id: int, issuer_id: int, reason: str):
    await add_dbmember_if_not_exist(user_id)
    await models.Softban.create(id=generate_id(), user=user_id, issuer=issuer_id, reason=reason)


async def remove_softban(user_id: int):
    softban = await get_softban(user_id)
    if softban:
        await softban.delete()


async def add_dbmember(user_id: int):
    return await models.Member.create(id=user_id)


async def add_dbmember_if_not_exist(user_id: int):
    db_member = await get_dbmember(user_id)
    if not db_member:
        db_member = await add_dbmember(user_id)
    return db_member


async def get_dbmember(user_id: int):
    return await models.Member.get(user_id)


async def add_dbchannel(channel_id: int, name: str):
    return await models.Channel.create(id=channel_id, name=name)


async def get_dbchannel(channel_id: int):
    return await models.Member.get(channel_id)


async def get_dbrole(role_id: int):
    return await models.Role.get(role_id)


async def get_softban(user_id: int):
    return await models.Softban.query.where(models.Softban.user == user_id).gino.first()


async def add_watch(user_id: int):
    db_member = await add_dbmember_if_not_exist(user_id)
    await db_member.update(watched=True).apply()
<commit_msg>crud.py: Fix using wrong model for get_dbchannel<commit_after>from datetime import datetime
from discord import utils, TextChannel
from . import models


def generate_id():
    return utils.time_snowflake(datetime.now())


async def add_permanent_role(user_id: int, role_id: int):
    await add_dbmember_if_not_exist(user_id)
    if not await models.PermanentRole.query.where((models.PermanentRole.user_id == user_id) & (
            models.PermanentRole.role_id == role_id)).gino.first():
        return await models.PermanentRole.create(user_id=user_id, role_id=role_id)


async def remove_permanent_role(user_id: int, role_id: int):
    permanent_role = await models.PermanentRole.query.where((models.PermanentRole.user_id == user_id) & (
                models.PermanentRole.role_id == role_id)).gino.first()
    if permanent_role:
        await permanent_role.delete()
        return permanent_role


async def get_permanent_roles(user_id: int):
    db_member = await get_dbmember(user_id)
    if db_member:
        return await models.PermanentRole.load(parent=db_member).query.gino.all()


async def add_staff(user_id: int, position: str):
    await add_dbmember_if_not_exist(user_id)
    staff = await get_staff(user_id) or await get_helper(user_id)
    if staff:
        await staff.update(position=position).apply()
    else:
        await models.Staff.create(id=user_id, position=position)


async def add_helper(user_id: int, position: str, console: str = None):
    await add_dbmember_if_not_exist(user_id)
    if staff := await get_staff(user_id):
        await staff.update(console=console).apply()
    else:
        await models.Staff.create(id=user_id, position=position, console=console)


async def remove_staff(user_id: int):
    staff = await get_staff(user_id)
    if staff:
        if staff.console:
            await staff.update(position="Helper").apply()
        else:
            await staff.delete()


async def remove_helper(user_id: int):
    helper = await get_helper(user_id)
    if helper:
        if helper.position != "Helper":
            await helper.update(console=None).apply()
        else:
            await helper.delete()


async def get_staff_all():
    return await models.Staff.query.where(models.Staff.position != 'Helper').gino.all()


async def get_staff(user_id: int):
    return await models.Staff.query.where(
        (models.Staff.position != 'Helper') & (models.Staff.id == user_id)).gino.first()


async def get_helpers():
    return await models.Staff.query.where(models.Staff.console.isnot(None)).gino.all()


async def get_helper(user_id: int):
    return await models.Staff.query.where(models.Staff.id == user_id).gino.first()


async def add_warn(user_id: int, issuer_id: int, reason: str):
    await add_dbmember_if_not_exist(user_id)
    await add_dbmember_if_not_exist(issuer_id)
    await models.Warn.create(id=generate_id(), user=user_id, issuer=issuer_id, reason=reason)


async def get_warns(user_id: int):
    return await models.Warn.query.where(models.Warn.user == user_id).gino.all()


async def remove_warn_id(user_id: int, index: int):
    warn = await models.Warn.query.where(models.Warn.user == user_id).offset(index - 1).gino.first()
    await warn.delete()


async def remove_warns(user_id: int):
    n_warns = await (models.db.select([models.db.func.count()]).where(models.Warn.user == user_id).gino.scalar())
    if n_warns:
        await models.Warn.delete.where(models.Warn.user == user_id).gino.status()
    return n_warns


async def add_timed_restriction(user_id: int, end_date: datetime, type: str):
    await add_dbmember_if_not_exist(user_id)
    await models.TimedRestriction.create(id=utils.time_snowflake(datetime.now()), user=user_id, type=type,
                                         end_date=end_date)


async def get_time_restrictions_by_user_type(user_id: int, type: str):
    return await models.TimedRestriction.query.where((models.TimedRestriction.user == user_id) & (
            models.TimedRestriction.type == type)).gino.first()


async def get_time_restrictions_by_type(type: str):
    return await models.TimedRestriction.query.where(models.TimedRestriction.type == type).gino.all()


async def remove_timed_restriction(user_id: int, type: str):
    time_restriction = await get_time_restrictions_by_user_type(user_id, type)
    if time_restriction:
        await time_restriction.delete()


async def set_time_restriction_alert(user_id: int, type: str):
    time_restriction = await get_time_restrictions_by_user_type(user_id, type)
    if time_restriction:
        await time_restriction.update(alerted=True).apply()


async def add_flag(name: str):
    await models.Flag.create(name=name)


async def get_flag(name: str):
    if flag := await models.Flag.get(name):
        return flag.value
    return None


async def remove_flag(name: str):
    flag = await get_flag(name)
    if flag:
        await flag.delete()


async def set_flag(name: str, value: bool):
    flag = await get_flag(name)
    if flag:
        await flag.update(value=value).apply()


async def add_softban(user_id: int, issuer_id: int, reason: str):
    await add_dbmember_if_not_exist(user_id)
    await models.Softban.create(id=generate_id(), user=user_id, issuer=issuer_id, reason=reason)


async def remove_softban(user_id: int):
    softban = await get_softban(user_id)
    if softban:
        await softban.delete()


async def add_dbmember(user_id: int):
    return await models.Member.create(id=user_id)


async def add_dbmember_if_not_exist(user_id: int):
    db_member = await get_dbmember(user_id)
    if not db_member:
        db_member = await add_dbmember(user_id)
    return db_member


async def get_dbmember(user_id: int):
    return await models.Member.get(user_id)


async def add_dbchannel(channel_id: int, name: str):
    return await models.Channel.create(id=channel_id, name=name)


async def get_dbchannel(channel_id: int):
    return await models.Channel.get(channel_id)


async def get_dbrole(role_id: int):
    return await models.Role.get(role_id)


async def get_softban(user_id: int):
    return await models.Softban.query.where(models.Softban.user == user_id).gino.first()


async def add_watch(user_id: int):
    db_member = await add_dbmember_if_not_exist(user_id)
    await db_member.update(watched=True).apply()

{'fraction_non_alphanumeric': 0.10790682934558707, 'fraction_numerical': 0.00015845349389954048, 'mean_word_length': 4.041134185303514, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6365578', 'n_tokens_mistral': 4302, 'n_tokens_neox': 4239, 'n_words': 862}