Beispiel #1
0
def finite_float(arg: str) -> float:
    try:
        ret = float(arg)
    except ValueError:
        raise commands.BadArgument(_("`{arg}` is not a number.").format(arg=arg))
    if not math.isfinite(ret):
        raise commands.BadArgument(_("`{arg}` is not a finite number.").format(arg=ret))
    return ret
Beispiel #2
0
    async def convert(
        self, ctx: commands.Context, arg: str
    ) -> Union[discord.Guild, discord.abc.GuildChannel, discord.abc.User,
               discord.Role]:
        bot: commands.Bot = ctx.bot
        _id = _match_id(arg)

        if _id is not None:
            guild: discord.Guild = bot.get_guild(_id)
            if guild is not None:
                return guild
            channel: discord.abc.GuildChannel = bot.get_channel(_id)
            if channel is not None:
                return channel

            user: discord.User = bot.get_user(_id)
            if user is not None:
                return user

            async for guild in AsyncIter(bot.guilds, steps=100):
                role: discord.Role = guild.get_role(_id)
                if role is not None:
                    return role

        all_roles = [
            filter(lambda r: not r.is_default(), guild.roles)
            async for guild in AsyncIter(bot.guilds, steps=100)
        ]

        objects = itertools.chain(bot.get_all_channels(), bot.users,
                                  bot.guilds, *all_roles)

        maybe_matches = []
        async for obj in AsyncIter(objects, steps=100):
            if obj.name == arg or str(obj) == arg:
                maybe_matches.append(obj)

        if ctx.guild is not None:
            async for member in AsyncIter(ctx.guild.members, steps=100):
                if member.nick == arg and not any(obj.id == member.id
                                                  for obj in maybe_matches):
                    maybe_matches.append(member)

        if not maybe_matches:
            raise commands.BadArgument(
                _('"{arg}" was not found. It must be the ID, mention, or name of a server, '
                  "channel, user or role which the bot can see.").format(
                      arg=arg))
        elif len(maybe_matches) == 1:
            return maybe_matches[0]
        else:
            raise commands.BadArgument(
                _('"{arg}" does not refer to a unique server, channel, user or role. Please use '
                  "the ID for whatever/whoever you're trying to specify, or mention it/them."
                  ).format(arg=arg))
Beispiel #3
0
    async def convert(self, ctx: commands.Context, arg: str) -> discord.Role:
        admin = ctx.command.cog
        if admin is None:
            raise commands.BadArgument(_("The Admin cog is not loaded."))

        role_converter = commands.RoleConverter()
        role = await role_converter.convert(ctx, arg)

        selfroles = await admin.config.guild(ctx.guild).selfroles()

        if role.id not in selfroles:
            raise commands.BadArgument(_("The provided role is not a valid selfrole."))
        return role
Beispiel #4
0
def RuleType(arg: str) -> bool:
    if arg.lower() in ("allow", "whitelist", "allowed"):
        return True
    if arg.lower() in ("deny", "blacklist", "denied"):
        return False

    raise commands.BadArgument(
        _('"{arg}" is not a valid rule. Valid rules are "allow" or "deny"').
        format(arg=arg))
Beispiel #5
0
    async def convert(
        self, ctx: commands.Context, arg: str
    ) -> Union[discord.abc.GuildChannel, discord.Member, discord.Role]:
        guild: discord.Guild = ctx.guild
        _id = _match_id(arg)

        if _id is not None:
            channel: discord.abc.GuildChannel = guild.get_channel(_id)
            if channel is not None:
                return channel

            member: discord.Member = guild.get_member(_id)
            if member is not None:
                return member

            role: discord.Role = guild.get_role(_id)
            if role is not None and not role.is_default():
                return role

        objects = itertools.chain(
            guild.channels, guild.members,
            filter(lambda r: not r.is_default(), guild.roles))

        maybe_matches = []
        async for obj in AsyncIter(objects, steps=100):
            if obj.name == arg or str(obj) == arg:
                maybe_matches.append(obj)
            try:
                if obj.nick == arg:
                    maybe_matches.append(obj)
            except AttributeError:
                pass

        if not maybe_matches:
            raise commands.BadArgument(
                _('"{arg}" was not found. It must be the ID, mention, or name of a channel, '
                  "user or role in this server.").format(arg=arg))
        elif len(maybe_matches) == 1:
            return maybe_matches[0]
        else:
            raise commands.BadArgument(
                _('"{arg}" does not refer to a unique channel, user or role. Please use the ID '
                  "for whatever/whoever you're trying to specify, or mention it/them."
                  ).format(arg=arg))
Beispiel #6
0
def ClearableRuleType(arg: str) -> Optional[bool]:
    if arg.lower() in ("allow", "whitelist", "allowed"):
        return True
    if arg.lower() in ("deny", "blacklist", "denied"):
        return False
    if arg.lower() in ("clear", "reset"):
        return None

    raise commands.BadArgument(
        _('"{arg}" is not a valid rule. Valid rules are "allow" or "deny", or "clear" to '
          "remove the rule").format(arg=arg))
    async def convert(cls, ctx: commands.Context, arg: str) -> InstalledModule:
        downloader = ctx.bot.get_cog("Downloader")
        if downloader is None:
            raise commands.CommandError(_("No Downloader cog found."))

        cog = discord.utils.get(await downloader.installed_cogs(), name=arg)
        if cog is None:
            raise commands.BadArgument(
                _("Cog `{cog_name}` is not installed.").format(cog_name=arg))

        return cog
    async def convert(cls, ctx: commands.Context, argument: str) -> Repo:
        downloader_cog = ctx.bot.get_cog("Downloader")
        if downloader_cog is None:
            raise commands.CommandError(_("No Downloader cog found."))

        # noinspection PyProtectedMember
        repo_manager = downloader_cog._repo_manager
        poss_repo = repo_manager.get_repo(argument)
        if poss_repo is None:
            raise commands.BadArgument(
                _('Repo by the name "{repo_name}" does not exist.').format(
                    repo_name=argument))
        return poss_repo
 async def convert(self, ctx: commands.Context, arg: str) -> MutableMapping:
     """Get playlist for all scopes that match the argument user provided"""
     cog = ctx.cog
     user_matches = []
     guild_matches = []
     global_matches = []
     if cog:
         global_matches = await get_all_playlist_converter(
             PlaylistScope.GLOBAL.value,
             ctx.bot,
             cog.playlist_api,
             arg,
             guild=ctx.guild,
             author=ctx.author,
         )
         guild_matches = await get_all_playlist_converter(
             PlaylistScope.GUILD.value,
             ctx.bot,
             cog.playlist_api,
             arg,
             guild=ctx.guild,
             author=ctx.author,
         )
         user_matches = await get_all_playlist_converter(
             PlaylistScope.USER.value,
             ctx.bot,
             cog.playlist_api,
             arg,
             guild=ctx.guild,
             author=ctx.author,
         )
     if not user_matches and not guild_matches and not global_matches:
         raise commands.BadArgument(
             _("Could not match '{}' to a playlist.").format(arg))
     return {
         PlaylistScope.GLOBAL.value: global_matches,
         PlaylistScope.GUILD.value: guild_matches,
         PlaylistScope.USER.value: user_matches,
         "all": [*global_matches, *guild_matches, *user_matches],
         "arg": arg,
     }
    async def convert(
        self, ctx: commands.Context, argument: str
    ) -> Tuple[str, discord.User, Optional[discord.Guild], bool, str,
               discord.User, Optional[discord.Guild], bool, ]:

        target_scope: Optional[str] = None
        target_user: Optional[Union[discord.Member, discord.User]] = None
        target_guild: Optional[discord.Guild] = None
        specified_target_user = False

        source_scope: Optional[str] = None
        source_user: Optional[Union[discord.Member, discord.User]] = None
        source_guild: Optional[discord.Guild] = None
        specified_source_user = False

        argument = argument.replace("—", "--")

        command, *arguments = argument.split(" -- ")
        if arguments:
            argument = " -- ".join(arguments)
        else:
            command = ""

        parser = NoExitParser(description="Playlist Scope Parsing.",
                              add_help=False)

        parser.add_argument("--to-scope",
                            nargs="*",
                            dest="to_scope",
                            default=[])
        parser.add_argument("--to-guild",
                            nargs="*",
                            dest="to_guild",
                            default=[])
        parser.add_argument("--to-server",
                            nargs="*",
                            dest="to_server",
                            default=[])
        parser.add_argument("--to-author",
                            nargs="*",
                            dest="to_author",
                            default=[])
        parser.add_argument("--to-user", nargs="*", dest="to_user", default=[])
        parser.add_argument("--to-member",
                            nargs="*",
                            dest="to_member",
                            default=[])

        parser.add_argument("--from-scope",
                            nargs="*",
                            dest="from_scope",
                            default=[])
        parser.add_argument("--from-guild",
                            nargs="*",
                            dest="from_guild",
                            default=[])
        parser.add_argument("--from-server",
                            nargs="*",
                            dest="from_server",
                            default=[])
        parser.add_argument("--from-author",
                            nargs="*",
                            dest="from_author",
                            default=[])
        parser.add_argument("--from-user",
                            nargs="*",
                            dest="from_user",
                            default=[])
        parser.add_argument("--from-member",
                            nargs="*",
                            dest="from_member",
                            default=[])

        if not command:
            parser.add_argument("command", nargs="*")

        try:
            vals = vars(parser.parse_args(argument.split()))
        except Exception as exc:
            raise commands.BadArgument() from exc

        is_owner = await ctx.bot.is_owner(ctx.author)
        valid_scopes = PlaylistScope.list() + [
            "GLOBAL",
            "GUILD",
            "AUTHOR",
            "USER",
            "SERVER",
            "MEMBER",
            "BOT",
        ]

        if vals["to_scope"]:
            to_scope_raw = " ".join(vals["to_scope"]).strip()
            to_scope = to_scope_raw.upper().strip()
            if to_scope not in valid_scopes:
                raise commands.ArgParserFailure("--to-scope",
                                                to_scope_raw,
                                                custom_help=_SCOPE_HELP)
            target_scope = standardize_scope(to_scope)
        elif "--to-scope" in argument and not vals["to_scope"]:
            raise commands.ArgParserFailure("--to-scope",
                                            _("Nothing"),
                                            custom_help=_(_SCOPE_HELP))

        if vals["from_scope"]:
            from_scope_raw = " ".join(vals["from_scope"]).strip()
            from_scope = from_scope_raw.upper().strip()

            if from_scope not in valid_scopes:
                raise commands.ArgParserFailure("--from-scope",
                                                from_scope_raw,
                                                custom_help=_SCOPE_HELP)
            source_scope = standardize_scope(from_scope)
        elif "--from-scope" in argument and not vals["to_scope"]:
            raise commands.ArgParserFailure("--to-scope",
                                            _("Nothing"),
                                            custom_help=_(_SCOPE_HELP))

        to_guild = vals.get("to_guild", None) or vals.get("to_server", None)
        if is_owner and to_guild:
            target_server_error = ""
            target_guild = None
            to_guild_raw = " ".join(to_guild).strip()
            try:
                target_guild = await global_unique_guild_finder(
                    ctx, to_guild_raw)
            except TooManyMatches as err:
                target_server_error = f"{err}\n"
            except NoMatchesFound as err:
                target_server_error = f"{err}\n"
            if target_guild is None:
                raise commands.ArgParserFailure(
                    "--to-guild",
                    to_guild_raw,
                    custom_help=f"{target_server_error}{_(_GUILD_HELP)}",
                )
        elif not is_owner and (to_guild
                               or any(x in argument
                                      for x in ["--to-guild", "--to-server"])):
            raise commands.BadArgument(_("You cannot use `--to-server`"))
        elif any(x in argument for x in ["--to-guild", "--to-server"]):
            raise commands.ArgParserFailure("--to-server",
                                            _("Nothing"),
                                            custom_help=_(_GUILD_HELP))

        from_guild = vals.get("from_guild", None) or vals.get(
            "from_server", None)
        if is_owner and from_guild:
            source_server_error = ""
            source_guild = None
            from_guild_raw = " ".join(from_guild).strip()
            try:
                source_guild = await global_unique_guild_finder(
                    ctx, from_guild_raw)
            except TooManyMatches as err:
                source_server_error = f"{err}\n"
            except NoMatchesFound as err:
                source_server_error = f"{err}\n"
            if source_guild is None:
                raise commands.ArgParserFailure(
                    "--from-guild",
                    from_guild_raw,
                    custom_help=f"{source_server_error}{_(_GUILD_HELP)}",
                )
        elif not is_owner and (from_guild or any(
                x in argument for x in ["--from-guild", "--from-server"])):
            raise commands.BadArgument(_("You cannot use `--from-server`"))
        elif any(x in argument for x in ["--from-guild", "--from-server"]):
            raise commands.ArgParserFailure("--from-server",
                                            _("Nothing"),
                                            custom_help=_(_GUILD_HELP))

        to_author = (vals.get("to_author", None) or vals.get("to_user", None)
                     or vals.get("to_member", None))
        if to_author:
            target_user_error = ""
            target_user = None
            to_user_raw = " ".join(to_author).strip()
            try:
                target_user = await global_unique_user_finder(
                    ctx, to_user_raw, guild=target_guild)
                specified_target_user = True
            except TooManyMatches as err:
                target_user_error = f"{err}\n"
            except NoMatchesFound as err:
                target_user_error = f"{err}\n"
            if target_user is None:
                raise commands.ArgParserFailure(
                    "--to-author",
                    to_user_raw,
                    custom_help=f"{target_user_error}{_(_USER_HELP)}")
        elif any(x in argument
                 for x in ["--to-author", "--to-user", "--to-member"]):
            raise commands.ArgParserFailure("--to-user",
                                            _("Nothing"),
                                            custom_help=_(_USER_HELP))

        from_author = (vals.get("from_author", None)
                       or vals.get("from_user", None)
                       or vals.get("from_member", None))
        if from_author:
            source_user_error = ""
            source_user = None
            from_user_raw = " ".join(from_author).strip()
            try:
                source_user = await global_unique_user_finder(
                    ctx, from_user_raw, guild=target_guild)
                specified_target_user = True
            except TooManyMatches as err:
                source_user_error = f"{err}\n"
            except NoMatchesFound as err:
                source_user_error = f"{err}\n"
            if source_user is None:
                raise commands.ArgParserFailure(
                    "--from-author",
                    from_user_raw,
                    custom_help=f"{source_user_error}{_(_USER_HELP)}",
                )
        elif any(x in argument
                 for x in ["--from-author", "--from-user", "--from-member"]):
            raise commands.ArgParserFailure("--from-user",
                                            _("Nothing"),
                                            custom_help=_(_USER_HELP))

        target_scope = target_scope or PlaylistScope.GUILD.value
        target_user = target_user or ctx.author
        target_guild = target_guild or ctx.guild

        source_scope = source_scope or PlaylistScope.GUILD.value
        source_user = source_user or ctx.author
        source_guild = source_guild or ctx.guild

        return (
            source_scope,
            source_user,
            source_guild,
            specified_source_user,
            target_scope,
            target_user,
            target_guild,
            specified_target_user,
        )
    async def convert(
        self, ctx: commands.Context, argument: str
    ) -> Tuple[Optional[str], discord.User, Optional[discord.Guild], bool]:

        target_scope: Optional[str] = None
        target_user: Optional[Union[discord.Member, discord.User]] = None
        target_guild: Optional[discord.Guild] = None
        specified_user = False

        argument = argument.replace("—", "--")

        command, *arguments = argument.split(" -- ")
        if arguments:
            argument = " -- ".join(arguments)
        else:
            command = ""

        parser = NoExitParser(description="Playlist Scope Parsing.",
                              add_help=False)
        parser.add_argument("--scope", nargs="*", dest="scope", default=[])
        parser.add_argument("--guild", nargs="*", dest="guild", default=[])
        parser.add_argument("--server", nargs="*", dest="guild", default=[])
        parser.add_argument("--author", nargs="*", dest="author", default=[])
        parser.add_argument("--user", nargs="*", dest="author", default=[])
        parser.add_argument("--member", nargs="*", dest="author", default=[])

        if not command:
            parser.add_argument("command", nargs="*")

        try:
            vals = vars(parser.parse_args(argument.split()))
        except Exception as exc:
            raise commands.BadArgument() from exc

        if vals["scope"]:
            scope_raw = " ".join(vals["scope"]).strip()
            scope = scope_raw.upper().strip()
            valid_scopes = PlaylistScope.list() + [
                "GLOBAL",
                "GUILD",
                "AUTHOR",
                "USER",
                "SERVER",
                "MEMBER",
                "BOT",
            ]
            if scope not in valid_scopes:
                raise commands.ArgParserFailure("--scope",
                                                scope_raw,
                                                custom_help=_(_SCOPE_HELP))
            target_scope = standardize_scope(scope)
        elif "--scope" in argument and not vals["scope"]:
            raise commands.ArgParserFailure("--scope",
                                            _("Nothing"),
                                            custom_help=_(_SCOPE_HELP))

        is_owner = await ctx.bot.is_owner(ctx.author)
        guild = vals.get("guild", None) or vals.get("server", None)
        if is_owner and guild:
            server_error = ""
            target_guild = None
            guild_raw = " ".join(guild).strip()
            try:
                target_guild = await global_unique_guild_finder(ctx, guild_raw)
            except TooManyMatches as err:
                server_error = f"{err}\n"
            except NoMatchesFound as err:
                server_error = f"{err}\n"
            if target_guild is None:
                raise commands.ArgParserFailure(
                    "--guild",
                    guild_raw,
                    custom_help=f"{server_error}{_(_GUILD_HELP)}")

        elif not is_owner and (guild or any(x in argument
                                            for x in ["--guild", "--server"])):
            raise commands.BadArgument(_("You cannot use `--guild`"))
        elif any(x in argument for x in ["--guild", "--server"]):
            raise commands.ArgParserFailure("--guild",
                                            _("Nothing"),
                                            custom_help=_(_GUILD_HELP))

        author = vals.get("author", None) or vals.get(
            "user", None) or vals.get("member", None)
        if author:
            user_error = ""
            target_user = None
            user_raw = " ".join(author).strip()
            try:
                target_user = await global_unique_user_finder(
                    ctx, user_raw, guild=target_guild)
                specified_user = True
            except TooManyMatches as err:
                user_error = f"{err}\n"
            except NoMatchesFound as err:
                user_error = f"{err}\n"

            if target_user is None:
                raise commands.ArgParserFailure(
                    "--author",
                    user_raw,
                    custom_help=f"{user_error}{_(_USER_HELP)}")
        elif any(x in argument for x in ["--author", "--user", "--member"]):
            raise commands.ArgParserFailure("--scope",
                                            _("Nothing"),
                                            custom_help=_(_USER_HELP))

        target_scope: Optional[str] = target_scope or None
        target_user: Union[discord.Member,
                           discord.User] = target_user or ctx.author
        target_guild: discord.Guild = target_guild or ctx.guild

        return target_scope, target_user, target_guild, specified_user
 def error(self, message):
     raise commands.BadArgument()
Beispiel #13
0
    obj: Union[commands.Command, commands.Cog]

    # noinspection PyArgumentList
    @classmethod
    async def convert(cls, ctx: commands.Context, arg: str) -> "CogOrCommand":
        ret = None
        if cog := ctx.bot.get_cog(arg):
            ret = cls(type="COG", name=cog.qualified_name, obj=cog)

        elif cmd := ctx.bot.get_command(arg):
            ret = cls(type="COMMAND", name=cmd.qualified_name, obj=cmd)

        if ret:
            if isinstance(ret.obj, commands.commands._RuleDropper):
                raise commands.BadArgument(
                    "You cannot apply permission rules to this cog or command."
                )
            return ret

        raise commands.BadArgument(
            _('Cog or command "{name}" not found. Please note that this is case sensitive.'
              ).format(name=arg))


def RuleType(arg: str) -> bool:
    if arg.lower() in ("allow", "whitelist", "allowed"):
        return True
    if arg.lower() in ("deny", "blacklist", "denied"):
        return False

    raise commands.BadArgument(