Esempio n. 1
0
    async def create(self) -> guilds.RESTGuild:
        route = routes.POST_GUILDS.compile()
        payload = data_binding.JSONObjectBuilder()
        payload.put("name", self.name)
        payload.put_array("roles",
                          self._roles if self._roles else undefined.UNDEFINED)
        payload.put_array(
            "channels",
            self._channels if self._channels else undefined.UNDEFINED)
        payload.put("region", self.region)
        payload.put("verification_level", self.verification_level)
        payload.put("default_message_notifications",
                    self.default_message_notifications)
        payload.put("explicit_content_filter",
                    self.explicit_content_filter_level)

        if self.icon is not undefined.UNDEFINED:
            icon = files.ensure_resource(self.icon)

            async with icon.stream(executor=self._executor) as stream:
                data_uri = await stream.data_uri()
                payload.put("icon", data_uri)

        raw_response = await self._request_call(route, json=payload)
        response = typing.cast(data_binding.JSONObject, raw_response)
        return self._entity_factory.deserialize_rest_guild(response)
Esempio n. 2
0
    def set_footer(self,
                   *,
                   text: typing.Optional[str],
                   icon: typing.Optional[files.Resourceish] = None) -> Embed:
        """Set the footer of this embed.

        Parameters
        ----------
        text : typing.Optional[str]
            The mandatory text string to set in the footer.
            If `builtins.None`, the footer is removed.
        icon : typing.Optional[hikari.files.Resourceish]
            The optional image to show next to the embed footer.

            This can be many different things, to aid in convenience.

            - If `builtins.None`, nothing is set.
            - If a `pathlib.PurePath` or `builtins.str` to a valid URL, the URL
                is linked to directly.
            - Subclasses of `hikari.files.WebResource` such as
                `hikari.files.URL`,
                `hikari.messages.Attachment`,
                `hikari.emojis.Emoji`,
                `EmbedResource`, etc will have their URL linked to directly.
                this field.
            - If a `hikari.files.Bytes` is passed, or a `builtins.str`
                that contains a valid data URI is passed, then this is uploaded
                as an attachment and linked into the embed.
            - If a `hikari.files.File`, `pathlib.PurePath` or
                `builtins.str` that is an absolute or relative path to a file
                on your file system is passed, then this resource is uploaded
                as an attachment using non-blocking code internally and linked
                into the embed.

        Returns
        -------
        Embed
            This embed. Allows for call chaining.
        """
        if text is None:
            if icon is not None:
                raise TypeError(
                    "Cannot specify footer text in embed to be None while setting a non-None icon. "
                    "Set some textual content in order to use a footer icon.")

            self._footer = None
        else:
            self._footer = EmbedFooter()
            self._footer.text = text
            if icon is not None:
                self._footer.icon = EmbedResourceWithProxy(
                    resource=files.ensure_resource(icon))
            else:
                self._footer.icon = None
        return self
Esempio n. 3
0
    def set_author(
        self,
        *,
        name: typing.Optional[str] = None,
        url: typing.Optional[str] = None,
        icon: typing.Optional[files.Resourceish] = None,
    ) -> Embed:
        """Set the author of this embed.

        Parameters
        ----------
        name : typing.Optional[builtins.str]
            The optional name of the author.
        url : typing.Optional[builtins.str]
            The optional URL of the author.
        icon : typing.Optional[hikari.files.Resourceish]
            The optional image to show next to the embed author.

            This can be many different things, to aid in convenience.

            - If `builtins.None`, nothing is set.
            - If a `pathlib.PurePath` or `builtins.str` to a valid URL, the URL
                is linked to directly.
            - Subclasses of `hikari.files.WebResource` such as
                `hikari.files.URL`,
                `hikari.messages.Attachment`,
                `hikari.emojis.Emoji`,
                `EmbedResource`, etc will have their URL linked to directly.
                this field.
            - If a `hikari.files.Bytes` is passed, or a `builtins.str`
                that contains a valid data URI is passed, then this is uploaded
                as an attachment and linked into the embed.
            - If a `hikari.files.File`, `pathlib.PurePath` or
                `builtins.str` that is an absolute or relative path to a file
                on your file system is passed, then this resource is uploaded
                as an attachment using non-blocking code internally and linked
                into the embed.

        Returns
        -------
        Embed
            This embed. Allows for call chaining.
        """
        if name is None and url is None and icon is None:
            self._author = None
        else:
            self._author = EmbedAuthor()
            self._author.name = name
            self._author.url = url
            if icon is not None:
                self._author.icon = EmbedResourceWithProxy(
                    resource=files.ensure_resource(icon))
            else:
                self._author.icon = None
        return self
Esempio n. 4
0
    def test_build_handles_attachments(self):
        mock_attachment = mock.Mock()
        mock_other_attachment = mock.Mock()
        mock_entity_factory = mock.Mock()
        mock_entity_factory.serialize_embed.return_value = (object(), [
            mock_other_attachment
        ])
        builder = (special_endpoints.InteractionMessageBuilder(
            base_interactions.ResponseType.MESSAGE_CREATE).add_attachment(
                mock_attachment).add_embed(object()))

        _, attachments = builder.build(mock_entity_factory)
        assert attachments == [
            files.ensure_resource(mock_attachment), mock_other_attachment
        ]
Esempio n. 5
0
    def set_image(self,
                  image: typing.Optional[files.Resourceish] = None,
                  /) -> Embed:
        """Set the image on this embed.

        Parameters
        ----------
        image : typing.Optional[hikari.files.Resourceish]
            The optional resource to show for the embed image.

            This can be many different things, to aid in convenience.

            - If `builtins.None`, nothing is set.
            - If a `pathlib.PurePath` or `builtins.str` to a valid URL, the URL
                is linked to directly.
            - Subclasses of `hikari.files.WebResource` such as
                `hikari.files.URL`,
                `hikari.messages.Attachment`,
                `hikari.emojis.Emoji`,
                `EmbedResource`, etc will have their URL linked to directly.
                this field.
            - If a `hikari.files.Bytes` is passed, or a `builtins.str`
                that contains a valid data URI is passed, then this is uploaded
                as an attachment and linked into the embed.
            - If a `hikari.files.File`, `pathlib.PurePath` or
                `builtins.str` that is an absolute or relative path to a file
                on your file system is passed, then this resource is uploaded
                as an attachment using non-blocking code internally and linked
                into the embed.

        Returns
        -------
        Embed
            This embed. Allows for call chaining.
        """
        if image is not None:
            self._image = EmbedImage(resource=files.ensure_resource(image))
        else:
            self._image = None

        return self
Esempio n. 6
0
def _ensure_embed_resource(resource: files.Resourceish,
                           cls: typing.Type[_T]) -> _T:
    if isinstance(resource, EmbedResource):
        return cls(resource=resource.resource)

    return cls(resource=files.ensure_resource(resource))