async def list_instances(self, ctx): instances = get_available_instances(ctx.author.id, ctx.guild.id) current_instance = self.bot.cache._get_selected_instance(ctx.author.id) try: current_instance = Instance(current_instance).name except: current_instance = "None" embed = discord.Embed( title=f"Available Instances ({str(len(instances))})", description=f'> Currently selected: {current_instance}') embed.set_author(name=str(ctx.author), icon_url=ctx.author.avatar_url) for i, (inst, perms) in enumerate(instances): try: self.bot.cache.instance(inst.id, by_inst_id=True) except: availability = "\🔴" else: availability = "\🟢" perms = ", ".join( [perm for perm, val in perms_to_dict(perms).items() if val]) embed.add_field(name=f"{str(i+1)} | {inst.name} {availability}", value=f"> **Perms:** {perms}") embed = add_empty_fields(embed) await ctx.send(embed=embed)
async def instance_config(self, ctx, key: str = None, value=None): instance_id = self.bot.cache._get_selected_instance(ctx.author.id) instance = Instance(instance_id) if key: key = key.lower() if key == None or key not in instance.config.keys(): embed = base_embed(instance.id, title='Config values') for key, value in instance.config.items(): value = str(value) if str(value) else "NULL" embed.add_field(name=key, value=value) embed = add_empty_fields(embed) await ctx.send(embed=embed) elif value == None: embed = base_embed( instance.id, title='Config values', description= f"> **Current value:**\n> {key} = {instance.config[key]}") desc = CONFIG_DESC[key] if key in CONFIG_DESC.keys( ) else f"**{key}**\nNo description found." embed.description = embed.description + "\n\n" + desc await ctx.send(embed=embed) else: old_value = instance.config[key] try: value = type(old_value)(value) except: raise commands.BadArgument( '%s should be %s, not %s' % (value, type(old_value).__name__, type(value).__name__)) else: if key == "guild_id": guild = self.bot.get_guild(int(value)) if not guild: raise commands.BadArgument( 'Unable to find a guild with ID %s' % value) member = guild.get_member(ctx.author.id) if not member: raise commands.BadArgument( 'You haven\'t joined that guild yourself') if not member.guild_permissions.administrator: raise commands.BadArgument( 'You need to have administrator permissions in that guild' ) instance.config[key] = value instance.store_config() if not old_value: old_value = "None" embed = base_embed(instance.id, title='Updated config') embed.add_field(name="Old Value", value=str(old_value)) embed.add_field(name="New value", value=str(value)) await ctx.send(embed=embed)
async def squad(self, ctx, team_id: int, squad_id: int): if team_id not in [1, 2]: raise commands.BadArgument('team_id needs to be either 1 or 2') inst = self.bot.cache.instance(ctx.author, ctx.guild.id).update() if team_id == 1: team = inst.team1 elif team_id == 2: team = inst.team2 squad = None for itersquad in team.squads: if itersquad.id == squad_id: squad = itersquad break if not squad: raise commands.BadArgument( 'No squad was found with the given squad id') creator = squad.creator_name if squad.creator_name else inst.get_player( squad.creator) if not creator: creator = str(squad.creator) else: creator = f'{creator} ({squad.creator})' embed = base_embed( inst.id, title=squad.name, description= f"> Size: {str(len(squad))}\n> Squad ID: {str(squad_id)}\n> Team: {team.faction}\n> Created by: {creator}" ) players = [ inst.get_player(player_id) for player_id in squad.player_ids ] for player_num, player in enumerate(players): player_num += 1 if player_num == 1: player_num = "SQL" else: player_num = str(player_num) embed.add_field( name=f"{player_num} | {player.name}", value= f"*{str(player.steam_id)}*\n> Player ID: {str(player.player_id)}\n> Online For: {str(player.online_time())}m" ) # Add empty fields if needed to make the embed look nicer embed = add_empty_fields(embed) await ctx.send(embed=embed)
def build_embed(team_id): if team_id == 1: team = inst.team1 elif team_id == 2: team = inst.team2 embed = base_embed(inst.id, title=f"Team {str(team_id)} - {team.faction}", description=f"{str(len(team))} players") # Add a field for each squad for squad in team.squads: name = f"{str(squad.id)} | {squad.name} ({str(len(squad.player_ids))})" players = "\n".join([ f"> {inst.get_player(player_id).name}" for player_id in squad.player_ids ]) embed.add_field(name=name, value=players) # Add empty fields if needed to make the embed look nicer embed = add_empty_fields(embed) # Add fields with unassigned players if team.unassigned: embed.add_field(name=" ", value=" ", inline=False) field1 = [] field2 = [] field3 = [] for i, player_id in enumerate(team.unassigned): if i % 3 == 0: field1.append(player_id) if i % 3 == 1: field2.append(player_id) if i % 3 == 2: field3.append(player_id) total_lines = len(field1) for field in [field1, field2, field3]: name = f"Unassigned Players ({str(len(team.unassigned))})" if field == field1 else " " lines = ["> "] * total_lines for i, player_id in enumerate(field): lines[i] = "> " + inst.get_player(player_id).name value = "\n".join(lines) embed.add_field(name=name, value=value) return embed
async def guild_permissions(self, ctx, operation: str = '', value: int = None): # List guild permissions if operation.lower() in ['', 'list', 'view', 'show']: instances = get_guild_instances(ctx.guild.id) if instances: embed = discord.Embed(title="Standard guild permissions") embed.set_author(icon_url=ctx.guild.icon_url, name=ctx.guild.name) for i, (instance, perms) in enumerate(instances): perms = ", ".join([ perm for perm, val in perms_to_dict(perms).items() if val ]) embed.add_field(name=f"{str(i+1)} | {instance.name}", value=f"> **Perms:** {perms}") embed = add_empty_fields(embed) else: embed = discord.Embed( title="Standard guild permissions", description= "There aren't any instances assigned to this guild just yet." ) embed.set_author(icon_url=ctx.guild.icon_url, name=ctx.guild.name) await ctx.send(embed=embed) # Set guild permissions for the selected instance elif operation.lower() in ['set']: instance = Instance( self.bot.cache._get_selected_instance(ctx.author.id)) # Update the guild permissions for the selected instance if value != None and int(value) >= 0 and int(value) <= 31: old_value = instance.default_perms instance.set_default_perms(value) embed = base_embed( instance.id, title=f"Changed guild permissions for {ctx.guild.name}") old_perms = ", ".join([ perm for perm, val in perms_to_dict(old_value).items() if val ]) new_perms = ", ".join([ perm for perm, val in perms_to_dict(value).items() if val ]) embed.add_field(name="Old Perms", value=f"> {old_perms}") embed.add_field(name="New Perms", value=f"> {new_perms}") await ctx.send(embed=embed) # Error else: raise commands.BadArgument("Permissions value out of range") # Unknown operation else: raise commands.BadArgument( 'Operation needs to be either "list" or "set", not "%s"' % operation)
async def permissions_group(self, ctx, user: discord.Member = None, operation: str = "", value: int = None): instance_id = self.bot.cache._get_selected_instance(ctx.author.id) instance = Instance(instance_id) # Limit command usage when missing permissions if not has_perms(ctx, instance=True): user = ctx.author operation = "" value = None # Default user to message author if not user: user = ctx.author # List all instances this user has access to here if operation.lower() == '': instances = get_available_instances(user.id, ctx.guild.id) if instances: embed = discord.Embed(title=f"Permissions in {ctx.guild.name}") embed.set_author(icon_url=user.avatar_url, name=f"{user.name}#{user.discriminator}") for i, (instance, perms) in enumerate(instances): perms = ", ".join([ perm for perm, val in perms_to_dict(perms).items() if val ]) embed.add_field(name=f"{str(i+1)} | {instance.name}", value=f"> **Perms:** {perms}") embed = add_empty_fields(embed) else: embed = discord.Embed( title=f"Permissions in {ctx.guild.name}", description= "You don't have access to any instances yet!\n\nInstances can be created by server owners, assuming they are an administrator of that guild.\n`r!instance create" ) embed.set_author(icon_url=user.avatar_url, name=f"{user.name}#{user.discriminator}") await ctx.send(embed=embed) # List user permissions for this user elif operation.lower() in ['list', 'view', 'show']: perms = get_perms(user.id, -1, instance_id, is_dict=False) perms_dict = perms_to_dict(perms) perms_str = ", ".join( [perm for perm, val in perms_dict.items() if val]) embed = base_embed( instance.id, title= f'Permission overwrites for {user.name}#{user.discriminator}', description= f"> Current value: {str(perms)}\n> Perms: {perms_str}") await ctx.send(embed=embed) # Set user permissions for the selected instance elif operation.lower() in ['set']: # Update the user permissions for the selected instance if value != None and int(value) >= 0 and int(value) <= 31: old_perms = ", ".join([ perm for perm, val in get_perms(user.id, -1, instance_id).items() if val ]) new_perms = ", ".join([ perm for perm, val in perms_to_dict(value).items() if val ]) set_player_perms(user.id, instance_id, value) embed = base_embed( instance.id, title= f"Changed permission overwrites for {user.name}#{user.discriminator}" ) embed.add_field(name="Old Perms", value=f"> {old_perms}") embed.add_field(name="New Perms", value=f"> {new_perms}") await ctx.send(embed=embed) # Error else: raise commands.BadArgument("Permissions value out of range") # Reset user permissions for the selected instance elif operation.lower() in ['reset']: reset_player_perms(user.id, instance_id) embed = base_embed( instance.id, title= f"Removed permission overwrites for {user.name}#{user.discriminator}" ) await ctx.send(embed=embed) # Unknown operation else: raise commands.BadArgument( 'Operation needs to be either "list" or "set" or "reset", not "%s"' % operation)