Пример #1
0
    async def set_role(
        cls,
        engine: AsyncEngine,
        role_type: str,
        guild: t.Union[str, int, Guild],
        role: t.Union[str, int, Role],
    ) -> None:
        """Store given `role` as `role_type` role for on `guild` into the database."""
        session = AsyncSession(bind=engine)

        role_type = cls._get_normalized_role_type(role_type)
        guild = get_str_guild(guild)
        role = get_str_role(role)

        logger.debug(f"Setting {role_type} on {guild} to {role}")

        await upsert(session,
                     cls,
                     conflict_columns=["guild"],
                     values={
                         "guild": guild,
                         role_type: role
                     })
        await session.commit()
        await session.close()
Пример #2
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
Пример #3
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
Пример #4
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()
Пример #5
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()
Пример #6
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()
Пример #7
0
    async def set_strike(cls,
                         engine: AsyncEngine,
                         guild: t.Union[str, int, Guild],
                         author: t.Union[str, int, Member],
                         user: t.Union[str, int, Member, User],
                         strike_type: str,
                         reason: t.Optional[str],
                         strike_id: t.Optional[int] = None) -> int:
        session = AsyncSession(bind=engine)

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

        # We don't usually expect we have passed id, instead we're determining
        # which ID should be used from the index table to keep the strikes serial
        # with their specific guild, if strike is specified, it means we're updating
        if not strike_id:
            strike_id = await StrikeIndex.get_id(session, guild)

        logger.debug(
            f"Adding {strike_type} strike to {user} from {author} for {reason}: id: {strike_id}"
        )

        await upsert(session,
                     cls,
                     conflict_columns=["guild", "id"],
                     values={
                         "guild": guild,
                         "id": strike_id,
                         "author": author,
                         "user": user,
                         "type": strike_type,
                         "reason": reason
                     })
        await session.commit()
        await session.close()
        return strike_id
Пример #8
0
    async def set_role_permission(cls, engine: AsyncEngine, time_type: str,
                                  guild: t.Union[str, int, Guild],
                                  role: t.Union[str, int, Role],
                                  time: t.Union[int, float]) -> None:
        """Store given `time` as `time_type` permission for `role` on `guild` into the database."""
        session = AsyncSession(bind=engine)

        guild = get_str_guild(guild)
        role = get_str_role(role)
        time_type = cls._get_normalized_time_type(time_type)
        time = cls._get_int_time(time)

        logger.debug(f"Setting {time_type} for {role} on {guild} to {time}")

        await upsert(session,
                     cls,
                     conflict_columns=["role", "guild"],
                     values={
                         "guild": guild,
                         "role": role,
                         time_type: time
                     })
        await session.commit()
        await session.close()