Beispiel #1
0
    async def beam_edit(self, ctx, name: str, key: str, value: str):
        """Edit beam"""
        if not repo_b.exists(name):
            raise errors.BadArgument("Invalid beam")

        if key in ("active", "admin_id", "replace", "timeout"):
            try:
                value = int(value)
            except ValueError:
                raise errors.BadArgument("Value has to be integer.")

        announce = True
        if key in ("admin_id"):
            announce = False

        repo_b.set(name=name, key=key, value=value)

        await self.event.sudo(ctx,
                              f"Beam **{name}** updated: {key} = {value}.")
        if not announce:
            return
        await self.announce(beam=name,
                            message=f"Beam updated: {key} is now {value}.")
Beispiel #2
0
    async def user_list(self, ctx, restraint: str = None):
        """List all registered users

        restraint: beam name, wormhole ID or user attribute
        """
        if restraint is None:
            db_users = repo_u.list_objects()
        elif repo_b.exists(restraint):
            db_users = repo_u.list_objects_by_beam(restraint)
        elif restraint in ("restricted", "readonly", "mod"):
            db_users = repo_u.list_objects_by_attribute(restraint)
        elif is_id(restraint) and repo_w.exists(int(restraint)):
            db_users = repo_u.list_objects_by_wormhole(int(restraint))
        else:
            raise errors.BadArgument("Value is not beam name nor wormhole ID.")

        template = "\n{nickname} ({name}, {id}):"
        template_home = "- {beam}: {home} ({name}, {guild})"

        # sort
        db_users.sort(key=lambda x: x.nickname)

        result = []
        for db_user in db_users:
            # get user
            user = self.bot.get_user(db_user.discord_id)
            user_name = str(user) if hasattr(user, "name") else "---"
            result.append(
                template.format(id=db_user.discord_id,
                                name=user_name,
                                nickname=db_user.nickname))
            for beam, discord_id in db_user.home_ids.items():
                if restraint and restraint != beam and restraint != str(
                        discord_id):
                    continue
                channel = self.bot.get_channel(discord_id)
                result.append(
                    template_home.format(
                        beam=beam,
                        home=discord_id,
                        name=channel.name
                        if hasattr(channel, "name") else "---",
                        guild=channel.guild.name if hasattr(
                            channel.guild, "name") else "---",
                    ))
            # attributes
            attrs = []
            if db_user.mod:
                attrs.append("MOD")
            if db_user.readonly:
                attrs.append("READ ONLY")
            if db_user.restricted:
                attrs.append("RESTRICTED")
            if len(attrs):
                result.append("- " + ", ".join(attrs))

        async def send_output(output: str):
            if hasattr(ctx.channel, "id") and repo_w.exists(ctx.channel.id):
                await ctx.author.send("```" + output + "```")
            else:
                await ctx.send("```" + output + "```")

        # iterate over the result
        output = ""
        for line in result:
            if len(output) > 1600:
                await send_output(output)
                output = ""
            output += "\n" + line
        if len(result) == 0:
            output = "No users."
        await send_output(output)