Esempio n. 1
0
    def get_context(self, message, prefix):
        view = StringView(message.content)
        view.skip_string(prefix)
        ctx = ExtContext(prefix=prefix, view=view, bot=self, message=message)

        invoker = view.get_word()
        ctx.invoked_with = invoker
        ctx.command = self.all_commands.get(invoker)
        return ctx
Esempio n. 2
0
    def is_command(self, invoked_prefix):
        view = StringView(self.text)
        if self.text.startswith(invoked_prefix):
            view.skip_string(invoked_prefix)

        invoker = view.get_word()
        cmd = self.bot.all_commands.get(invoker)
        if cmd:
            return True

        return False
Esempio n. 3
0
    def _get_ctx(self,
                 message,
                 view_content,
                 invoked_prefix,
                 *,
                 cls=ExtraContext):
        view = StringView(view_content)
        ctx = cls(prefix=None, view=view, bot=self, message=message)
        ctx.prefix = invoked_prefix
        view.skip_string(invoked_prefix)

        invoker = view.get_word()
        ctx.invoked_with = invoker
        ctx.command = self.all_commands.get(invoker)
        ctx.view_content = view_content

        return ctx
Esempio n. 4
0
    async def get_invoked_prefix(self, message):
        prefix = await self.get_prefix(message)
        view = StringView(message.content)
        try:
            # if the context class' __init__ consumes something from the view this
            # will be wrong.  That seems unreasonable though.
            if message.content.startswith(tuple(prefix)):
                return discord.utils.find(view.skip_string, prefix)
            return None

        except TypeError:
            return None
Esempio n. 5
0
    async def _get_contexts(self, message, *, cls=ExtraContext):
        prefix = await self.get_prefix(message)
        contexts = []
        command_contents = message.content.split(' |> ')

        first_content = command_contents.pop(0)
        view = StringView(first_content)
        ctx = cls(prefix=None, view=view, bot=self, message=message)
        ctx.view_content = first_content
        if command_contents:
            ctx.be_piped = True

        if self._skip_check(message.author.id, self.user.id):
            return [ctx]

        try:
            # if the context class' __init__ consumes something from the view this
            # will be wrong.  That seems unreasonable though.
            if message.content.startswith(tuple(prefix)):
                invoked_prefix = discord.utils.find(view.skip_string, prefix)
            else:
                return [ctx]

        except TypeError:
            if not isinstance(prefix, list):
                raise TypeError(
                    "get_prefix must return either a string or a list of string, "
                    "not {}".format(prefix.__class__.__name__))

            # It's possible a bad command_prefix got us here.
            for value in prefix:
                if not isinstance(value, str):
                    raise TypeError(
                        "Iterable command_prefix or list returned from get_prefix must "
                        "contain only strings, not {}".format(
                            value.__class__.__name__))

            # Getting here shouldn't happen
            raise

        invoker = view.get_word()
        ctx.invoked_with = invoker
        ctx.prefix = invoked_prefix
        ctx.command = self.all_commands.get(invoker)
        contexts.append(ctx)

        while command_contents:
            content = command_contents.pop(0)

            # _message = message
            view_content = invoked_prefix + content.lstrip(' ')

            ctx = self.get_ctx(message, view_content, invoked_prefix, cls=cls)

            if not ctx.command:
                view_content = contexts[-1].view_content + ' |> ' + content
                ctx = self.get_ctx(message,
                                   view_content,
                                   invoked_prefix,
                                   cls=cls)
                ctx.be_piped = contexts[-1].be_piped
                if not command_contents:
                    ctx.be_piped = False

                contexts[-1] = ctx
                continue

            try:
                await ctx.command._parse_arguments(ctx)
            except commands.MissingRequiredArgument:
                ctx = self.get_ctx(message,
                                   view_content,
                                   invoked_prefix,
                                   cls=cls)
            else:
                ctx = self.get_ctx(message,
                                   view_content,
                                   invoked_prefix,
                                   cls=cls)
                contexts[-1].be_piped = False

            if command_contents:
                ctx.be_piped = True

            contexts.append(ctx)

        return contexts
Esempio n. 6
0
    async def get_contexts(self,
                           message,
                           group,
                           invoked_prefix,
                           *,
                           cls=ExtraContext):

        not_parsed_contents = union(rebase_symbols(self, group.text, ' |> '),
                                    invoked_prefix)
        contexts = []

        first_content = not_parsed_contents.pop(0)
        view = StringView(first_content.text)
        ctx = cls(prefix=None, view=view, bot=self, message=message)

        if not_parsed_contents:
            ctx.be_piped = True

        if self._skip_check(message.author.id, self.user.id):
            return [ctx]

        if first_content.text.startswith(invoked_prefix):
            view.skip_string(invoked_prefix)

        invoker = view.get_word()
        ctx.invoked_with = invoker
        ctx.prefix = invoked_prefix
        ctx.command = self.all_commands.get(invoker)
        contexts.append(ctx)

        while not_parsed_contents:
            content = not_parsed_contents.pop(0)
            if isinstance(content, Symbol):
                continue

            view = StringView(content.text)
            ctx = cls(prefix=None, view=view, bot=self, message=message)
            ctx.prefix = invoked_prefix
            if content.text.startswith(invoked_prefix):
                view.skip_string(invoked_prefix)

            invoker = view.get_word()
            ctx.command = self.all_commands.get(invoker)

            try:
                await ctx.command._parse_arguments(ctx)
            except commands.MissingRequiredArgument:
                ctx = self.get_ctx(StringView(content.text), message,
                                   invoked_prefix, content.text)
            else:
                ctx = self.get_ctx(StringView(content.text), message,
                                   invoked_prefix, content.text)
                contexts[-1].be_piped = False

            if not_parsed_contents:
                ctx.be_piped = True

            contexts.append(ctx)

        return contexts