Exemplo n.º 1
0
    async def cut_part(
        self, ctx: NewMessage, arguments_string: str
    ) -> CutterParsingResponse[Mention[T]]:
        parsing_response = cut_part_via_regex(
            self.mention_regex, arguments_string
        )
        match_object: typing.Match = parsing_response.extra["match_object"]
        page_id = int(match_object.group("id"))
        page_type = match_object.group("page_type")

        if page_type == "id":
            page_type = PageType.USER
        else:
            page_type = PageType.GROUP

        try:
            casted_part = await self._cast_type(ctx, page_id, page_type)
        # Если ID невалидно
        except APIError as err:
            raise BadArgumentError("Invalid id") from err
        else:
            parsing_response.parsed_part = Mention(
                alias=match_object.group("alias"),
                entity=casted_part,
                page_type=page_type,
            )
            return parsing_response
Exemplo n.º 2
0
    async def _link_method(
        self, ctx: NewMessage, arguments_string: str
    ) -> CutterParsingResponse:
        parsing_response = cut_part_via_regex(
            self.screen_name_regex, arguments_string, group="screen_name"
        )

        resolved_screen_name = await ctx.api.use_cache().method(
            "utils.resolve_screen_name",
            screen_name=parsing_response.parsed_part,
        )
        if not resolved_screen_name:
            raise BadArgumentError("Invalid screen name")

        if resolved_screen_name["type"] == "user":
            page_type = PageType.USER
        elif resolved_screen_name["type"] == "user":
            page_type = PageType.GROUP
        else:
            raise BadArgumentError("Invalid screen name")

        parsing_response.parsed_part = await self._cast_type(
            ctx, resolved_screen_name["object_id"], page_type
        )

        return parsing_response
Exemplo n.º 3
0
 async def cut_part(
     self, ctx: NewMessage, arguments_string: str
 ) -> CutterParsingResponse[str]:
     return cut_part_via_regex(
         self._pattern,
         arguments_string,
         error_description=self.gen_message_doc(),
     )
Exemplo n.º 4
0
 async def cut_part(
     self, ctx: NewMessage, arguments_string: str
 ) -> CutterParsingResponse:
     for typevar in self._container_values:
         try:
             return cut_part_via_regex(typevar, arguments_string)
         except BadArgumentError:
             continue
     raise BadArgumentError(self.gen_message_doc())
Exemplo n.º 5
0
    async def _raw_id_method(
        self, ctx: NewMessage, arguments_string: str
    ) -> CutterParsingResponse:
        parsing_response = cut_part_via_regex(
            self.raw_id_regex, arguments_string
        )
        match_object: typing.Match = parsing_response.extra["match_object"]

        if match_object.group("type") in ("+", "id", ""):
            page_type = PageType.USER
        else:
            page_type = PageType.GROUP

        parsing_response.parsed_part = await self._cast_type(
            ctx, match_object.group("id"), page_type
        )

        return parsing_response