Ejemplo n.º 1
0
async def test_recalculate_leader_returns_new_highest_points_user(
        target_user: User, old_point_leader: User, guild: Guild):
    target_user.points = OLD_POINT_LEADER_POINTS + 1
    target_user.save()

    new_leader = await recalculate_leader(guild)

    assert new_leader == target_user
Ejemplo n.º 2
0
def add_xp(user: User, amount: int, cooldown=timedelta(minutes=5)) -> User:
    if user.xp_last_update_time + cooldown > datetime.now():
        return user
    LOG.info(f"Adding {amount} xp for user {user.name}")
    user.xp += amount
    user.xp_last_update_time = datetime.now()
    user.save()
    return user
Ejemplo n.º 3
0
def test_add_points_only_adds_points_to_correct_guild(
        command_user: User, target_user: User,
        target_user_in_other_guild: User):
    target_user = add_points(command_user, target_user, POINTS_COUNT)
    target_user_in_other_guild = User.get_by_id(target_user_in_other_guild.id)

    assert target_user.points != target_user_in_other_guild.points
Ejemplo n.º 4
0
def database_user_list() -> List[User]:
    users = []
    for i in range(10):
        users.append(
            User.create(discord_id=USER_ID + i,
                        guild_id=GUILD_ID,
                        name="Guild 1 User",
                        points=i,
                        xp=i))
    for i in range(10):
        users.append(
            User.create(discord_id=USER_ID + i,
                        guild_id=GUILD_ID + 1,
                        name="Guild 2 User",
                        points=i,
                        xp=i))
    return users
Ejemplo n.º 5
0
async def test_recalculate_leader_reassigns_roles(
        target_user: User, old_point_leader: User,
        leader_role: PointLeaderRole):
    target_user.points = OLD_POINT_LEADER_POINTS + 1
    target_user.save()

    discord_role = MagicMock()
    old_leader_member = AsyncMock(roles=[discord_role], spec=Member)
    new_leader_member = AsyncMock(roles=[], spec=Member)
    guild = AsyncMock(id=GUILD_ID, spec=Guild)
    guild.get_member.return_value = new_leader_member
    guild.members = [new_leader_member, old_leader_member]
    guild.get_role.return_value = discord_role
    await recalculate_leader(guild)

    new_leader_member.add_roles.assert_called_with(discord_role)
    old_leader_member.remove_roles.assert_called_with(discord_role)
Ejemplo n.º 6
0
def test_get_or_create_user_user_creates_user(discord_user: DiscordUser,
                                              guild: Guild):
    created_user = get_or_create_user(discord_user, guild)

    assert User.select().count() == 1
    assert created_user.discord_id == discord_user.id
    assert created_user.guild_id == guild.id
    assert created_user.name == discord_user.name
    assert created_user.points == NO_POINTS
Ejemplo n.º 7
0
def get_or_create_user(discord_user: DiscordUser, guild: Guild) -> User:
    user, created = User.get_or_create(discord_id=discord_user.id,
                                       guild_id=guild.id,
                                       defaults={"name": discord_user.name})
    if created:
        LOG.info(f"Created user {discord_user.name}")
    if user.name != discord_user.name:
        LOG.info(f"Changing name for user {user.name} to {discord_user.name}")
        user.name = discord_user.name
        user.save()
    return user
Ejemplo n.º 8
0
async def recalculate_leader(guild: Guild) -> User:
    new_leader: User = User.select().where(User.guild_id == guild.id).order_by(
        User.points.desc()).get()

    role: Optional[PointLeaderRole] = PointLeaderRole.get_or_none(
        PointLeaderRole.guild_id == guild.id)
    if not role:
        return new_leader

    discord_role: Optional[DiscordRole] = guild.get_role(role.role_id)
    if not discord_role:
        LOG.info(f"Point leader role no longer exists in guild {guild.name}")
        role.delete_instance()
        return new_leader

    for member in guild.members:
        if discord_role in member.roles:
            await member.remove_roles(discord_role)

    new_leader_discord_member: Member = guild.get_member(new_leader.discord_id)
    await new_leader_discord_member.add_roles(discord_role)

    return new_leader
Ejemplo n.º 9
0
def user_in_other_guild() -> User:
    return User.create(discord_id=USER_ID,
                       guild_id=GUILD_ID + 1,
                       name=USERNAME)
Ejemplo n.º 10
0
def command_user() -> User:
    return User.create(discord_id=COMMAND_USER_ID,
                       guild_id=GUILD_ID,
                       name=COMMAND_USERNAME)
Ejemplo n.º 11
0
def database_user() -> User:
    user = User.create(discord_id=USER_ID,
                       guild_id=GUILD_ID,
                       name=USERNAME,
                       points=LOTS_OF_POINTS)
    return user
Ejemplo n.º 12
0
def get_xp_leaderboard_users(guild: Guild) -> List[User]:
    return User.select().where(User.guild_id == guild.id).order_by(
        User.xp.desc())
Ejemplo n.º 13
0
def add_points(command_user: User, target_user: User, points: int) -> User:
    LOG.info(f"Giving {target_user.name} {points}")
    log_points_change(command_user, target_user, points)
    target_user.points += points
    target_user.save()
    return target_user
Ejemplo n.º 14
0
def test_add_xp_adds_correct_amount_of_xp(user: User):
    user.xp = 100
    user = add_xp(user, XP_COUNT, cooldown=timedelta(0))
    assert user.xp == 100 + XP_COUNT
Ejemplo n.º 15
0
def test_add_xp_only_adds_xp_to_correct_guild(user: User,
                                              user_in_other_guild: User):
    user = add_xp(user, XP_COUNT, cooldown=timedelta(0))
    user_in_other_guild = User.get_by_id(user_in_other_guild.id)

    assert user.xp != user_in_other_guild.xp
Ejemplo n.º 16
0
def target_user():
    user = User.create(discord_id=TARGET_USER_ID,
                       guild_id=GUILD_ID,
                       name=TARGET_USERNAME)
    return user
Ejemplo n.º 17
0
def test_add_xp_adds_xp_to_correct_user(user: User):
    add_xp(user, XP_COUNT, cooldown=timedelta(0))
    user = User.get_by_id(user.id)
    assert user.xp == XP_COUNT
Ejemplo n.º 18
0
def user() -> User:
    return User.create(discord_id=USER_ID, guild_id=GUILD_ID, name=USERNAME)
Ejemplo n.º 19
0
def target_user_in_other_guild() -> User:
    return User.create(discord_id=TARGET_USER_ID,
                       guild_id=GUILD_ID + 1,
                       name=TARGET_USERNAME)
Ejemplo n.º 20
0
def test_add_points_adds_correct_amount_of_points(command_user: User,
                                                  target_user: User):
    target_user.points = 100
    target_user = add_points(command_user, target_user, POINTS_COUNT)
    assert target_user.points == 100 + POINTS_COUNT
Ejemplo n.º 21
0
def test_add_points_adds_points_to_correct_user(command_user: User,
                                                target_user: User):
    add_points(command_user, target_user, POINTS_COUNT)
    target_user = User.get(User.id == target_user.id)
    assert target_user.points == POINTS_COUNT
Ejemplo n.º 22
0
def old_point_leader() -> User:
    return User.create(discord_id=OLD_POINT_LEADER_ID,
                       guild_id=GUILD_ID,
                       name=OLD_POINT_LEADER_NAME,
                       points=OLD_POINT_LEADER_POINTS)