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 servers(self, ctx): instances = get_available_instances(ctx.author.id, ctx.guild.id) instances = [ self.bot.cache.instance(inst.id, by_inst_id=True).update() for inst, perms in instances if perms_to_dict(perms)["public"] == True ] for inst in instances: embed = self.create_server_embed(inst) await ctx.send(embed=embed)
async def servers(self, ctx): instances = get_available_instances(ctx.author, ctx.guild.id) instances = [ self.bot.cache.instance(inst.id, by_inst_id=True).update() for inst, perms in instances if perms.public ] for inst in instances: embed = self.create_server_embed(inst) await ctx.send(embed=embed) if not instances: await ctx.send( ":no_entry_sign: There are no instances that let you do this!")
def _get_selected_instance(self, user_id, guild_id=None): if user_id not in self.selected_instance: self.selected_instance[user_id] = -1 try: instances.Instance(self.selected_instance[user_id]) except: try: self.selected_instance[ user_id] = instances.get_available_instances( user_id, guild_id)[0][0].id except: self.selected_instance[user_id] = -1 return self.selected_instance[user_id]
async def server(self, ctx, *, instance_id=None): if instance_id: instances = get_available_instances(ctx.author, ctx.guild.id) inst = None for i, (instance, perms) in enumerate(instances): if str(i + 1) == instance_id or instance.name.lower( ) == instance_id.lower(): inst = instance break if not inst: raise commands.BadArgument( "No instance found with name or ID %s" % instance_id) if not perms.public: raise commands.BadArgument( "Missing required permissions for this instance") inst = self.bot.cache.instance(inst.id, by_inst_id=True).update() else: inst = self.bot.cache.instance(ctx.author, ctx.guild.id).update() embed = self.create_server_embed(inst) await ctx.send(embed=embed)
async def select_instance(self, ctx, *, instance_id: str): instances = get_available_instances(ctx.author.id, ctx.guild.id) inst = None for i, (instance, perms) in enumerate(instances): if str(i + 1) == instance_id or instance.name.lower( ) == instance_id.lower(): inst = instance break if not inst: raise commands.BadArgument("No instance found with name or ID %s" % instance_id) self.bot.cache.selected_instance[ctx.author.id] = instance.id perms = ", ".join( [perm for perm, val in perms_to_dict(perms).items() if val]) embed = discord.Embed(title=f'Selected the "{instance.name}" instance', description=f"> **Perms:** {perms}") embed.set_author(name=str(ctx.author), icon_url=ctx.author.avatar_url) msg = await ctx.send(embed=embed) await asyncio.sleep(4) await msg.delete()
async def command_prefix(bot, msg): res = re.search(r"\A[rR](\d*)!", msg.content) try: prefix = res.group() inst_id = res.groups()[0] except: return ("r!", "R!") if inst_id: inst_id = int(inst_id) instances = get_available_instances(msg.author.id, msg.guild.id) try: inst = [ inst for i, (inst, perms) in enumerate(instances) if i + 1 == inst_id ][0] except IndexError: await msg.channel.send( ":no_entry_sign: Invalid command prefix!\n`No instance found with ID %s`" % inst_id) return ("r!", "R!") bot.cache.selected_instance[msg.author.id] = inst.id return prefix
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)