Example #1
0
async def aux_invite_member(ctx, host_member: discord.Member,
                            invited_member: discord.Member):
    guild = ctx.guild
    existing_lab_group = hpf.existing_member_lab_group(host_member)
    if not existing_lab_group:
        await ctx.send(btm.error_not_in_group_for_invite(host_member))
    elif hpf.member_in_teaching_team(invited_member, guild):
        await ctx.send(btm.error_can_not_invite_teaching_team())
    else:
        async with invites_lock:
            group_num = hpf.get_lab_group_number(existing_lab_group.name)
            invite_list = GUILD_CONFIG.group_invites(guild)
            if hpf.existing_member_lab_group(invited_member):
                await ctx.send(
                    btm.error_member_already_in_group(invited_member.name,
                                                      existing_lab_group.name))
            elif invite_list.has_invite(invited_member.id, group_num):
                await ctx.send(btm.error_invite_already_sent(invited_member))
            else:
                # Add invitation
                invite_list.add_invite(invited_member.id, group_num)
                general_text_channel = hpf.get_general_text_channel(guild)
                if general_text_channel:
                    await general_text_channel.send(
                        btm.success_invite_sent_to_group(
                            invited_member, existing_lab_group, group_num))
                group_channel = hpf.get_lab_text_channel(guild, group_num)
                if group_channel:
                    await group_channel.send(
                        btm.success_invite_sent(invited_member))
async def aux_raise_hand(ctx):
    member = ctx.author
    existing_lab_group = hpf.existing_member_lab_group(member)
    general_text_channel = hpf.get_general_text_channel(ctx.guild)
    if not existing_lab_group:
        await ctx.channel.send(btm.message_member_not_in_group_for_help())
    elif ctx.channel != hpf.existing_member_lab_text_channel(member):
        await ctx.channel.send(
            btm.error_stay_in_your_seat(ctx.author, existing_lab_group))
    elif general_text_channel:
        group_num = hpf.get_lab_group_number(existing_lab_group.name)
        # Check if group already asked for help
        async with help_queue_lock:
            help_queue = GUILD_CONFIG.help_queue(ctx.guild)
            queue_size = help_queue.size()
            if group_num in help_queue:
                await ctx.channel.send(
                    btm.info_help_queue_size(queue_size -
                                             1) if queue_size > 1 else ":eyes:"
                )
                return
        # Get help
        online_team = hpf.all_teaching_team_members(ctx.author.guild)
        available_team = [
            member for member in online_team
            if not hpf.existing_member_lab_group(member)
        ]
        help_message = None
        if available_team:
            await ctx.channel.send(btm.message_asking_for_help())
            help_message = await general_text_channel.send(
                btm.message_call_for_help(existing_lab_group.name,
                                          available_team))
        elif online_team:
            await ctx.channel.send(btm.message_no_one_available_error())
            help_message = await general_text_channel.send(
                btm.message_call_for_help(existing_lab_group.name,
                                          online_team))
        else:
            await ctx.channel.send(btm.message_no_one_online_error())
        if help_message:
            async with help_queue_lock:
                if queue_size > 0:
                    await ctx.channel.send(btm.info_help_queue_size(queue_size)
                                           )
                help_queue.add(group=group_num, message_id=help_message.id)
    else:
        await ctx.channel.send(btm.message_can_not_get_help_error())
async def go_for_help_from_message(member: discord.Member,
                                   message: discord.Message,
                                   group: Union[int, str]) -> bool:
    guild = member.guild
    if hpf.existing_member_lab_role(member):
        await leave_group(guild, member, hpf.existing_member_lab_role(member),
                          hpf.existing_member_lab_group(member))
    lab_group = hpf.get_lab_group(guild, group)
    async with help_queue_lock:
        message_id = GUILD_CONFIG.help_queue(guild).extract_group(group)
    if not message_id:
        return False
    if message.channel:
        await message.channel.send(
            btm.info_on_the_way_to(member, lab_group.name))
    group_text_channel = hpf.get_lab_text_channel(guild, lab_group.name)
    if group_text_channel:
        await group_text_channel.send(
            btm.message_help_on_the_way(member, show_mention=True))
    if hpf.get_lab_role(guild, group):
        await join_group(guild,
                         member,
                         hpf.get_lab_role(guild, group),
                         lab_group,
                         group_message=False)
        return True
    return False
Example #4
0
async def aux_leave_group(ctx,
                          member: discord.Member,
                          group_message: bool = True,
                          general_message: bool = True,
                          show_open_message: bool = True,
                          show_not_in_group_error: bool = True):
    guild = ctx.guild
    existing_lab_role = hpf.existing_member_lab_role(member)
    if existing_lab_role:
        existing_lab_group = hpf.existing_member_lab_group(member)
        await leave_group(guild,
                          member,
                          existing_lab_role,
                          existing_lab_group,
                          group_message=group_message,
                          general_message=general_message)
        # If group get empty, open it
        if len(hpf.all_students_in_group(guild, existing_lab_group.name)) < 1 \
                and is_closed_group(guild, existing_lab_group) \
                and not hpf.member_in_teaching_team(member, guild):
            await open_group(guild, existing_lab_group)
            if show_open_message:
                general_text_channel = hpf.get_general_text_channel(guild)
                await general_text_channel.send(
                    btm.success_group_open(existing_lab_group))
    elif show_not_in_group_error:
        await ctx.send(btm.message_member_not_in_any_group(member))
Example #5
0
async def aux_whereis(ctx,
                      members: List[discord.Member],
                      invalid_name: Optional[str] = None):
    if invalid_name:
        await ctx.send(btm.message_member_not_exists(invalid_name))
    for member in members:
        lab_group = hpf.existing_member_lab_group(member)
        if lab_group:
            await ctx.send(btm.message_where_is_member(member, lab_group))
        else:
            await ctx.send(btm.message_member_not_in_any_group(member))
async def aux_close_group(ctx, group: Optional[discord.CategoryChannel]):
    guild = ctx.guild
    member_group = hpf.existing_member_lab_group(ctx.author)
    is_in_teaching_team = hpf.member_in_teaching_team(ctx.author, guild)
    if not member_group and not is_in_teaching_team:
        await ctx.send(btm.message_member_not_in_any_group(ctx.author))
    elif group and (not (is_in_teaching_team or group == member_group)):
        await ctx.send(
            btm.error_member_not_part_of_group(
                ctx.author, group if group else member_group))
    else:
        group_to_be_closed = group if group else member_group
        await close_group(guild, group_to_be_closed)
        general_text_channel = hpf.get_general_text_channel(guild)
        if general_text_channel:
            await general_text_channel.send(
                btm.success_group_closed(group_to_be_closed))
        print("OPEN_GROUPS", GUILD_CONFIG.open_groups(guild))
        print("CLOSED_GROUPS", GUILD_CONFIG.closed_groups(guild))
Example #7
0
async def aux_make_group(
        ctx,
        members: List[discord.Member],
        random_choice: bool = False) -> Optional[discord.CategoryChannel]:
    guild = ctx.guild
    if not hpf.member_in_teaching_team(ctx.author,
                                       guild) and ctx.author not in members:
        members.append(ctx.author)
    members = set(members)
    max_group_size = GUILD_CONFIG.max_students_per_group(guild)
    # Check if there are not more members than allowed
    if len(members) > max_group_size:
        await ctx.send(btm.message_too_many_members_error(max_group_size))
        return None
    members_with_groups = False
    # Check if any member is already in any group
    for member in members:
        existing_lab_group = hpf.existing_member_lab_group(member)
        if existing_lab_group:
            members_with_groups = True
            await ctx.send(
                btm.error_member_already_in_group(hpf.get_nick(member),
                                                  existing_lab_group.name))
    if members_with_groups:
        return None
    empty_groups = hpf.all_empty_groups(guild)
    if not empty_groups:
        extra_group = await aux_create_group(ctx)
        empty_groups.append(extra_group)
    if random_choice:
        new_group = random.choice(empty_groups)
    else:
        new_group = sorted(empty_groups, key=lambda g: g.name,
                           reverse=False)[0]
    print(
        f'Moving members {" ".join([hpf.get_nick(m) for m in members])} to {new_group.name}'
    )
    success = True
    for member in members:
        success = success and await aux_join_group(ctx, member, new_group.name)
    if success:
        return new_group
Example #8
0
async def aux_join_group(ctx,
                         member: discord.Member,
                         group: Union[int, str],
                         group_message: bool = True,
                         general_message: bool = True) -> bool:
    guild = ctx.guild
    new_role = hpf.get_lab_role(guild, group)
    new_lab_group = hpf.get_lab_group(guild, group)
    existing_lab_group = hpf.existing_member_lab_group(member)
    max_group_size = GUILD_CONFIG.max_students_per_group(guild)
    if GUILD_CONFIG.require_nickname(guild) and not member.nick:
        await ctx.send(btm.message_member_need_name_error(member))
    elif existing_lab_group:
        await ctx.send(
            btm.error_member_already_in_group(hpf.get_nick(member),
                                              existing_lab_group.name))
    elif not new_role:
        await ctx.send(btm.message_lab_group_not_exists(new_lab_group.name))
    elif not hpf.member_in_teaching_team(member, guild) and len(
            hpf.all_students_in_group(guild, group)) >= max_group_size:
        await ctx.send(
            btm.message_max_members_in_group_error(new_lab_group.name,
                                                   max_group_size))
    else:
        if not hpf.member_in_teaching_team(member, guild):
            group_num = hpf.get_lab_group_number(new_lab_group.name)
            async with invites_lock:
                invite_list = GUILD_CONFIG.group_invites(guild)
                invited = invite_list.has_invite(member.id, group_num)
                if is_closed_group(guild, new_lab_group) and not invited:
                    text_channel = hpf.get_lab_text_channel(guild, group)
                    if text_channel:
                        await text_channel.send(
                            btm.error_someone_try_to_enter(member))
                    await ctx.send(btm.error_lab_group_is_closed(new_lab_group)
                                   )
                    return False
                if invited:
                    invite_list.remove_invite(member.id, group_num)
                await join_group(guild,
                                 member,
                                 new_role,
                                 new_lab_group,
                                 group_message=group_message,
                                 general_message=general_message)
        else:
            await join_group(guild,
                             member,
                             new_role,
                             new_lab_group,
                             group_message=group_message,
                             general_message=general_message)
        # Remove other invitations
        async with invites_lock:
            invite_list = GUILD_CONFIG.group_invites(guild)
            old_invites = invite_list.retrieve_invites(member.id)
            for group_invite in old_invites:
                text_channel = hpf.get_lab_text_channel(guild, group_invite)
                await text_channel.send(
                    btm.info_member_accepted_another_invite(member))
        return True
    return False