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))
async def aux_broadcast(ctx, message: str): guild = ctx.guild general_text_channel = hpf.get_general_text_channel(guild) if general_text_channel: await general_text_channel.send( btm.broadcast_message_from(ctx.author, message)) for group in hpf.all_existing_lab_groups(guild): text_channel = hpf.get_lab_text_channel(guild, group.name) if GUILD_CONFIG.broadcast_to_empty_groups(guild) or len( hpf.all_students_in_group(guild, group.name)) > 0: await text_channel.send( btm.broadcast_message_from(ctx.author, message))
def aux_group_details(ctx, group: discord.CategoryChannel, details: bool = False, none_if_empty: bool = False) -> Optional[str]: guild = ctx.guild members = hpf.all_students_in_group(guild, group.name) if not members and none_if_empty: return None if details: return btm.info_group_details(members, group, is_open=is_open_group(guild, group)) else: return btm.message_list_group_members( hpf.get_lab_group_number(group.name), members)
async def map_size_of_members_to_group( guild: discord.Guild) -> Dict[int, List[discord.CategoryChannel]]: print('Getting num of members per group') size_to_group = {} print('Created dictionary') for group in hpf.all_existing_lab_groups(guild): print('Processing group ' + group.name) group_num = hpf.get_lab_group_number(group.name) if is_open_group(guild, group): size = len(hpf.all_students_in_group(guild, group_num)) print('The group is open and it has ' + str(size) + " members") groups = size_to_group.get(size) if groups is None: groups = [] size_to_group[size] = groups groups.append(group) print('Done. The dictionary: ' + str(size_to_group)) return size_to_group
async def aux_move_to(ctx, member: discord.Member, group: Optional[int]): max_group_size = GUILD_CONFIG.max_students_per_group(ctx.guild) if group and len(hpf.all_students_in_group(ctx.guild, group)) >= max_group_size: await ctx.send( btm.message_max_members_in_group_error( hpf.get_lab_group_name(group), max_group_size)) else: await aux_leave_group(ctx, member, show_not_in_group_error=False, general_message=group is None) await asyncio.sleep(1) if group and await aux_join_group( ctx, member, group, general_message=False): await ctx.send( btm.message_member_moved(member, hpf.get_lab_group_name(group)))
async def random_assignment( ctx, member: discord.Member, available_existing_groups: List[discord.CategoryChannel]): if not member.nick: await ctx.send(btm.message_member_need_name_error(member)) return available_existing_groups max_group_size = GUILD_CONFIG.max_students_per_group(ctx.guild) while len(available_existing_groups) > 0: random_lab_group = random.choice(available_existing_groups) random_group = hpf.get_lab_group_number(random_lab_group.name) if random_group and len( hpf.all_students_in_group(ctx.guild, random_group)) < max_group_size: success = await aux_join_group(ctx, member, random_group) if success: return available_existing_groups available_existing_groups.remove(random_lab_group) new_group = await aux_create_group(ctx) new_group_number = hpf.get_lab_group_number(new_group.name) if await aux_join_group(ctx, member, new_group_number): ctx.send(btm.message_default_error()) available_existing_groups.append(new_group) return available_existing_groups
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