async def clean_all_command(ctx, *args): async with ctx.channel.typing(): excluded_groups = hpf.get_excluded_groups(*args) groups_to_be_cleaned = [group for group in hpf.all_existing_lab_groups(ctx.guild) if hpf.get_lab_group_number(group.name) not in excluded_groups and is_open_group(ctx.guild, group)] for group in sorted(groups_to_be_cleaned, key=lambda c: c.name, reverse=False): await aux_clean_group(ctx, group.name)
async def aux_get_list(ctx, message_size: int = 200, only_open_groups: bool = False, exclude_empty: bool = True): existing_lab_groups = hpf.all_existing_lab_groups(ctx.guild) if not existing_lab_groups: await ctx.send(btm.info_no_groups()) else: message_list = [] message_acc = "**List**" for lab_group in sorted(existing_lab_groups, key=lambda g: g.name): if only_open_groups and is_closed_group(ctx.guild, lab_group): continue message = aux_group_details(ctx, lab_group, details=True, none_if_empty=exclude_empty) if message and len(message_acc) + len(message) < message_size: message_acc += '\n' + message elif message: message_list.append(message_acc) message_acc = '...\n' + message message_list.append(message_acc) if existing_lab_groups: for message in message_list: await ctx.send(message)
async def update_previous_lab_groups_permission( role: discord.Role, category: discord.CategoryChannel, allow_mask: Optional[int] = None, deny_mask: Optional[int] = None) -> None: guild = category.guild existing_lab_groups = hpf.all_existing_lab_groups(guild) for lab_group in existing_lab_groups: if lab_group != category: await update_permission(role, lab_group, allow_mask, deny_mask)
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))
async def aux_random_join(ctx, member_mention: discord.Member, *args): member = discord.utils.get(ctx.message.mentions, name=member_mention.name) excluded_groups = hpf.get_excluded_groups(*args) if not member: await ctx.send(btm.message_member_not_exists(member_mention.nick)) elif not excluded_groups: await ctx.send("All extra arguments should be integers!") else: available_lab_groups = [] for group in hpf.all_existing_lab_groups(ctx.guild): group_number = hpf.get_lab_group_number(group.name) if group_number and group not in excluded_groups and is_open_group( ctx.guild, group): available_lab_groups.append(group) await random_assignment(ctx, member, available_lab_groups)
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_get_list(ctx, message_size: int = 200, only_open_groups: bool = False, exclude_empty: bool = True, exclude_no_group: bool = False, only_online = False): existing_lab_groups = hpf.all_existing_lab_groups(ctx.guild) if not existing_lab_groups: await ctx.send(btm.info_no_groups()) else: message_list = [] message_acc = "**List**" if only_open_groups or exclude_empty or exclude_no_group or only_online: message_acc += " **[Excluding:" if only_open_groups: message_acc += " closed-groups" if exclude_empty: message_acc += " empty-groups" if exclude_no_group: message_acc += " no-group" if only_online: message_acc += " not-online" message_acc += "]**" for lab_group in sorted(existing_lab_groups, key=lambda g: g.name): if only_open_groups and is_closed_group(ctx.guild, lab_group): continue message = aux_group_details(ctx, lab_group, details=True, none_if_empty=exclude_empty, only_online=only_online) if message and len(message_acc) + len(message) < message_size: message_acc += '\n' + message elif message: message_list.append(message_acc) message_acc = '...\n' + message if not exclude_no_group: members_no_group = hpf.all_students_with_no_group(ctx.guild) if members_no_group: if only_online: members_no_group = hpf.select_online_members(ctx.guild, members_no_group) if members_no_group: message = btm.message_list_no_group_members(members_no_group) if message and len(message_acc) + len(message) < message_size: message_acc += '\n' + message elif message: message_list.append(message_acc) message_acc = '...\n' + message message_list.append(message_acc) if existing_lab_groups: for message in message_list: await ctx.send(message)
async def aux_random_join_all(ctx, *args): # Get excluded groups excluded_groups = hpf.get_excluded_groups(*args) #if not excluded_groups: # await ctx.send("All extra arguments should be integers for excluded groups.") # return # Get available groups available_lab_groups = [] for group in hpf.all_existing_lab_groups(ctx.guild): group_number = hpf.get_lab_group_number(group.name) if group_number and group not in excluded_groups and is_open_group( ctx.guild, group): available_lab_groups.append(group) no_group_members = hpf.all_students_with_no_group(ctx.guild) # Assign groups for member in no_group_members: # if member.status == discord.Status.online: available_lab_groups = await random_assignment(ctx, member, available_lab_groups)
async def aux_init_guild(guild: discord.Guild): if not guild in GUILD_CONFIG: return #print(f'{guild.name} (id: {guild.id})') print(guild.name) members = '\n - '.join([hpf.get_nick(member) for member in guild.members]) print(f'Guild Members:\n - {members}') # Create or update roles all_allow = discord.Permissions.all() almost_all = discord.Permissions(PMask.ALL_BUT_ADMIN_AND_GUILD | PMask.STREAM) text_and_voice_allow = discord.Permissions(PMask.CHANGE_NICKNAME | PMask.PARTIAL_TEXT | PMask.PARTIAL_VOICE) await create_new_role(guild, PROFESSOR_ROLE_NAME, permissions=all_allow, colour=discord.Colour.blue(), hoist=True, mentionable=True) await create_new_role(guild, HEAD_TA_ROLE_NAME, permissions=all_allow, colour=discord.Colour.red(), hoist=True, mentionable=True) await create_new_role(guild, TA_ROLE_NAME, permissions=almost_all, colour=discord.Colour.purple(), hoist=True, mentionable=True) await create_new_role(guild, STUDENT_ROLE_NAME, permissions=text_and_voice_allow, colour=discord.Colour.gold(), hoist=True, mentionable=True) # Double check existing lab groups for group in hpf.all_existing_lab_groups(guild): if not (is_open_group(guild, group) or is_closed_group(guild, group)): await open_group(guild, group)
async def delete_all_groups(ctx): async with ctx.channel.typing(): for group in sorted(hpf.all_existing_lab_groups(ctx.guild), key=lambda c: c.name, reverse=True): await cdg.aux_delete_group(ctx, group.name)
def all_closed_groups(guild: discord.Guild) -> List[discord.CategoryChannel]: return [ group for group in hpf.all_existing_lab_groups(guild) if is_closed_group(guild, group) ]
async def aux_create_group(ctx) -> Optional[discord.CategoryChannel]: async with create_delete_lock: # Get existing lab groups guild = ctx.guild existing_lab_groups = hpf.all_existing_lab_groups(guild) # Get new group number (assumes a previous lab group could have been deleted) next_num = 1 for idx, category in enumerate( sorted(existing_lab_groups, key=lambda c: c.name), 2): pattern = re.compile(f"Group[\s]+{next_num}") if re.search(pattern, category.name) is None: break next_num = idx # Create new names new_category_name = hpf.get_lab_group_name(next_num) new_role_name = hpf.get_role_name(next_num) text_channel_name = hpf.get_text_channel_name(next_num) voice_channel_name = hpf.get_voice_channel_name(next_num) # Check if category or channels already exist existing_category = discord.utils.get(guild.categories, name=new_category_name) existing_text_channel = discord.utils.get(guild.channels, name=text_channel_name) existing_voice_channel = discord.utils.get(guild.channels, name=voice_channel_name) if not (existing_category or existing_text_channel or existing_voice_channel): try: # Create new role new_role = await create_new_role(guild, new_role_name, mentionable=True) # Set lab group permissions default = discord.Permissions() allow_text_voice_stream = discord.Permissions( PMask.VIEW | PMask.PARTIAL_TEXT | PMask.PARTIAL_VOICE | PMask.STREAM) can_not_view_channel = discord.Permissions(PMask.VIEW) overwrites = { role: discord.PermissionOverwrite.from_pair( default, can_not_view_channel) for role in hpf.all_existing_lab_roles(guild) } student_role = discord.utils.get(guild.roles, name=STUDENT_ROLE_NAME) if student_role: overwrites[ student_role] = discord.PermissionOverwrite.from_pair( default, can_not_view_channel) overwrites[new_role] = discord.PermissionOverwrite.from_pair( allow_text_voice_stream, default) # Create new lab group print(f'Creating a new category: {new_category_name}') new_category = await guild.create_category_channel( new_category_name, overwrites=overwrites) # Deny access to the lab groups created before await update_previous_lab_groups_permission( new_role, new_category, deny_mask=PMask.VIEW) # Create new text and voice channels print( f'Creating a new channels: ({text_channel_name}) and ({voice_channel_name})' ) text_channel = await new_category.create_text_channel( text_channel_name) await new_category.create_voice_channel(voice_channel_name) # Success message general_text_channel = hpf.get_general_text_channel(guild) if general_text_channel: await general_text_channel.send( btm.message_group_created(new_category_name, next_num)) await text_channel.send( btm.message_welcome_group(new_category_name)) await open_group(guild, new_category) return new_category except Exception as e: print(e) await ctx.send(btm.message_unexpected_error("create-group")) await aux_delete_group(ctx, next_num, show_bot_message=False) raise e