async def get_stats(cls, session: AsyncSession) -> t.List:
        try:
            rows = await session.run_sync(lambda session: session.query(cls).all())
        except NoResultFound:
            return []

        return [row.dict() for row in rows]
Exemplo n.º 2
0
    async def get_prefixes(cls, session: AsyncSession) -> t.Optional[list]:
        try:
            rows = await session.run_sync(
                lambda session: session.query(cls).all())
        except NoResultFound:
            return []

        return [row.dict() for row in rows]
    async def remove_feed_channel(
            cls, session: AsyncSession,
            guild_id: t.Union[str, int, discord.Guild]) -> None:
        guild_id = get_datatype_int(guild_id)

        row = await session.run_sync(lambda session: session.query(cls).
                                     filter_by(guild_id=guild_id).first())
        await session.run_sync(lambda session: session.delete(row))

        await session.commit()
Exemplo n.º 4
0
    async def get_roles(
            cls, session: AsyncSession,
            guild_id: t.Union[str, int, discord.Guild]) -> t.Optional[dict]:
        guild_id = get_datatype_int(guild_id)

        try:
            row = await session.run_sync(lambda session: session.query(cls).
                                         filter_by(guild_id=guild_id).first())
        except NoResultFound:
            return None

        if row is not None:
            return row.dict()
Exemplo n.º 5
0
    async def get_id(cls, engine: AsyncEngine, guild: t.Union[str, int,
                                                              Guild]) -> int:
        session = AsyncSession(bind=engine)
        guild = get_str_guild(guild)

        # Logic for increasing strike ID if it was already found
        # but using the default if the entry is new
        row = await session.run_sync(
            lambda session: session.query(cls).filter_by(guild=guild).one())
        next_id = row.next_id + 1
        row.next_id = next_id
        await session.commit()
        await session.close()
        return next_id
    async def get_command_stats(
        cls, session: AsyncSession, command_name: str
    ) -> t.Optional[t.List[dict]]:
        try:
            row = await session.run_sync(
                lambda session: session.query(cls)
                .filter_by(command=command_name)
                .first()
            )
        except NoResultFound:
            return None

        if row is not None:
            return row.dict()
    async def get_next_id(
        cls, session: AsyncSession, guild: t.Union[int, Guild]
    ) -> int:
        guild_id = get_datatype_int(guild)

        row = await session.run_sync(
            lambda session: session.query(
                cls).filter_by(guild_id=guild_id).first()
        )

        row.next_id += 1
        next_id = row.next_id

        await session.commit()
        return next_id
Exemplo n.º 8
0
    async def remove_strike(cls, engine: AsyncEngine, guild: t.Union[str, int,
                                                                     Guild],
                            strike_id: int) -> dict:
        session = AsyncSession(bind=engine)

        guild = get_str_guild(guild)

        row = await session.run_sync(lambda session: session.query(
            cls).filter_by(guild=guild, id=strike_id).one())
        dct = row.to_dict()
        await session.run_sync(lambda session: session.delete(row))
        await session.close()

        logger.debug(f"Strike {strike_id} has been removed")

        return dct
Exemplo n.º 9
0
    async def get_roles(cls, engine: AsyncEngine,
                        guild: t.Union[str, int, Guild]) -> dict:
        """Obtain roles on `guild` from the database."""
        session = AsyncSession(bind=engine)

        guild = get_str_guild(guild)
        try:
            row = await session.run_sync(lambda session: session.query(cls).
                                         filter_by(guild=guild).one())
        except NoResultFound:
            dct = {col: None for col in cls.__table__.columns.keys()}
            dct.update({'guild': guild})
            return dct
        else:
            return row.to_dict()
        finally:
            await session.close()
Exemplo n.º 10
0
    async def get_guild_strikes(cls, engine: AsyncEngine,
                                guild: t.Union[str, int, Guild]) -> list:
        """Obtain all strikes belonging to `guild` from the database."""
        session = AsyncSession(bind=engine)

        guild = get_str_guild(guild)

        try:
            rows = await session.run_sync(lambda session: session.query(cls).
                                          filter_by(guild=guild).all())
        except NoResultFound:
            return []
        else:
            strikes = []
            for row in rows:
                strikes.append(row.to_dict())
            return strikes
        finally:
            session.close()
Exemplo n.º 11
0
def create_user(dbsession: AsyncSession, username: str, password: str,
                role: str) -> (bool, Users):
    user = dbsession.query(Users).filter_by(username=username).first()
    if user is not None:
        return False, user

    salt, hash_password = generate_salt_and_password(password)
    user = Users(username=username,
                 password=hash_password,
                 salt=salt,
                 role=role)
    dbsession.add(user)
    try:
        dbsession.commit()
    except Exception as e:  # noqa
        dbsession.rollback()
        logger.exception("create_user failure")
        return False, None

    return True, user
Exemplo n.º 12
0
    async def get_author_strikes(
            cls, engine: AsyncEngine, guild: t.Union[str, int, Guild],
            author: t.Union[str, int, Member, User]) -> list:
        """Obtain all strikes on `guild` by `author` from the database."""
        session = AsyncSession(bind=engine)

        guild = get_str_guild(guild)
        author = get_str_user(author)

        try:
            rows = await session.run_sync(lambda session: session.query(
                cls).filter_by(guild=guild, author=author).all())
        except NoResultFound:
            return []
        else:
            strikes = []
            for row in rows:
                strikes.append(row.to_dict())
            return strikes
        finally:
            await session.close()