1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
|
"""
The MIT License (MIT)
Copyright (c) 2015-present Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import datetime
from typing import Any, Dict, List, Mapping, Optional, Protocol, TYPE_CHECKING, TypeVar, Union
from . import utils
from .colour import Colour
from .flags import AttachmentFlags, EmbedFlags
# fmt: off
__all__ = (
'Embed',
)
# fmt: on
class EmbedProxy:
def __init__(self, layer: Dict[str, Any]):
self.__dict__.update(layer)
def __len__(self) -> int:
return len(self.__dict__)
def __repr__(self) -> str:
inner = ', '.join((f'{k}={getattr(self, k)!r}' for k in dir(self) if not k.startswith('_')))
return f'EmbedProxy({inner})'
def __getattr__(self, attr: str) -> None:
return None
def __eq__(self, other: object) -> bool:
return isinstance(other, EmbedProxy) and self.__dict__ == other.__dict__
class EmbedMediaProxy(EmbedProxy):
def __init__(self, layer: Dict[str, Any]):
super().__init__(layer)
self._flags = self.__dict__.pop('flags', 0)
@property
def flags(self) -> AttachmentFlags:
return AttachmentFlags._from_value(self._flags or 0)
if TYPE_CHECKING:
from typing_extensions import Self
from .types.embed import Embed as EmbedData, EmbedType
T = TypeVar('T')
class _EmbedFooterProxy(Protocol):
text: Optional[str]
icon_url: Optional[str]
class _EmbedFieldProxy(Protocol):
name: Optional[str]
value: Optional[str]
inline: bool
class _EmbedMediaProxy(Protocol):
url: Optional[str]
proxy_url: Optional[str]
height: Optional[int]
width: Optional[int]
flags: AttachmentFlags
class _EmbedProviderProxy(Protocol):
name: Optional[str]
url: Optional[str]
class _EmbedAuthorProxy(Protocol):
name: Optional[str]
url: Optional[str]
icon_url: Optional[str]
proxy_icon_url: Optional[str]
class Embed:
"""Represents a Discord embed.
.. container:: operations
.. describe:: len(x)
Returns the total size of the embed.
Useful for checking if it's within the 6000 character limit.
.. describe:: bool(b)
Returns whether the embed has any data set.
.. versionadded:: 2.0
.. describe:: x == y
Checks if two embeds are equal.
.. versionadded:: 2.0
For ease of use, all parameters that expect a :class:`str` are implicitly
casted to :class:`str` for you.
.. versionchanged:: 2.0
``Embed.Empty`` has been removed in favour of ``None``.
Attributes
-----------
title: Optional[:class:`str`]
The title of the embed.
This can be set during initialisation.
Can only be up to 256 characters.
type: :class:`str`
The type of embed. Usually "rich".
This can be set during initialisation.
Possible strings for embed types can be found on discord's
:ddocs:`api docs <resources/message#embed-object-embed-types>`
description: Optional[:class:`str`]
The description of the embed.
This can be set during initialisation.
Can only be up to 4096 characters.
url: Optional[:class:`str`]
The URL of the embed.
This can be set during initialisation.
timestamp: Optional[:class:`datetime.datetime`]
The timestamp of the embed content. This is an aware datetime.
If a naive datetime is passed, it is converted to an aware
datetime with the local timezone.
colour: Optional[Union[:class:`Colour`, :class:`int`]]
The colour code of the embed. Aliased to ``color`` as well.
This can be set during initialisation.
"""
__slots__ = (
'title',
'url',
'type',
'_timestamp',
'_colour',
'_footer',
'_image',
'_thumbnail',
'_video',
'_provider',
'_author',
'_fields',
'description',
'_flags',
)
def __init__(
self,
*,
colour: Optional[Union[int, Colour]] = None,
color: Optional[Union[int, Colour]] = None,
title: Optional[Any] = None,
type: EmbedType = 'rich',
url: Optional[Any] = None,
description: Optional[Any] = None,
timestamp: Optional[datetime.datetime] = None,
):
self.colour = colour if colour is not None else color
self.title: Optional[str] = title
self.type: EmbedType = type
self.url: Optional[str] = url
self.description: Optional[str] = description
self._flags: int = 0
if self.title is not None:
self.title = str(self.title)
if self.description is not None:
self.description = str(self.description)
if self.url is not None:
self.url = str(self.url)
if timestamp is not None:
self.timestamp = timestamp
@classmethod
def from_dict(cls, data: Mapping[str, Any]) -> Self:
"""Converts a :class:`dict` to a :class:`Embed` provided it is in the
format that Discord expects it to be in.
You can find out about this format in the :ddocs:`official Discord documentation <resources/message#embed-object>`.
Parameters
-----------
data: :class:`dict`
The dictionary to convert into an embed.
"""
# we are bypassing __init__ here since it doesn't apply here
self = cls.__new__(cls)
# fill in the basic fields
self.title = data.get('title', None)
self.type = data.get('type', None)
self.description = data.get('description', None)
self.url = data.get('url', None)
self._flags = data.get('flags', 0)
if self.title is not None:
self.title = str(self.title)
if self.description is not None:
self.description = str(self.description)
if self.url is not None:
self.url = str(self.url)
# try to fill in the more rich fields
try:
self._colour = Colour(value=data['color'])
except KeyError:
pass
try:
self._timestamp = utils.parse_time(data['timestamp'])
except KeyError:
pass
for attr in ('thumbnail', 'video', 'provider', 'author', 'fields', 'image', 'footer'):
try:
value = data[attr]
except KeyError:
continue
else:
setattr(self, '_' + attr, value)
return self
def copy(self) -> Self:
"""Returns a shallow copy of the embed."""
return self.__class__.from_dict(self.to_dict())
def __len__(self) -> int:
total = len(self.title or '') + len(self.description or '')
for field in getattr(self, '_fields', []):
total += len(field['name']) + len(field['value'])
try:
footer_text = self._footer['text']
except (AttributeError, KeyError):
pass
else:
total += len(footer_text)
try:
author = self._author
except AttributeError:
pass
else:
total += len(author['name'])
return total
def __bool__(self) -> bool:
return any(
(
self.title,
self.url,
self.description,
self.colour,
self.fields,
self.timestamp,
self.author,
self.thumbnail,
self.footer,
self.image,
self.provider,
self.video,
)
)
def __eq__(self, other: Embed) -> bool:
return isinstance(other, Embed) and (
self.type == other.type
and self.title == other.title
and self.url == other.url
and self.description == other.description
and self.colour == other.colour
and self.fields == other.fields
and self.timestamp == other.timestamp
and self.author == other.author
and self.thumbnail == other.thumbnail
and self.footer == other.footer
and self.image == other.image
and self.provider == other.provider
and self.video == other.video
and self._flags == other._flags
)
@property
def flags(self) -> EmbedFlags:
""":class:`EmbedFlags`: The flags of this embed.
.. versionadded:: 2.5
"""
return EmbedFlags._from_value(self._flags or 0)
@property
def colour(self) -> Optional[Colour]:
return getattr(self, '_colour', None)
@colour.setter
def colour(self, value: Optional[Union[int, Colour]]) -> None:
if value is None:
self._colour = None
elif isinstance(value, Colour):
self._colour = value
elif isinstance(value, int):
self._colour = Colour(value=value)
else:
raise TypeError(f'Expected discord.Colour, int, or None but received {value.__class__.__name__} instead.')
color = colour
@property
def timestamp(self) -> Optional[datetime.datetime]:
return getattr(self, '_timestamp', None)
@timestamp.setter
def timestamp(self, value: Optional[datetime.datetime]) -> None:
if isinstance(value, datetime.datetime):
if value.tzinfo is None:
value = value.astimezone()
self._timestamp = value
elif value is None:
self._timestamp = None
else:
raise TypeError(f"Expected datetime.datetime or None received {value.__class__.__name__} instead")
@property
def footer(self) -> _EmbedFooterProxy:
"""Returns an ``EmbedProxy`` denoting the footer contents.
See :meth:`set_footer` for possible values you can access.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_footer', {})) # type: ignore
def set_footer(self, *, text: Optional[Any] = None, icon_url: Optional[Any] = None) -> Self:
"""Sets the footer for the embed content.
This function returns the class instance to allow for fluent-style
chaining.
Parameters
-----------
text: :class:`str`
The footer text. Can only be up to 2048 characters.
icon_url: :class:`str`
The URL of the footer icon. Only HTTP(S) is supported.
Inline attachment URLs are also supported, see :ref:`local_image`.
"""
self._footer = {}
if text is not None:
self._footer['text'] = str(text)
if icon_url is not None:
self._footer['icon_url'] = str(icon_url)
return self
def remove_footer(self) -> Self:
"""Clears embed's footer information.
This function returns the class instance to allow for fluent-style
chaining.
.. versionadded:: 2.0
"""
try:
del self._footer
except AttributeError:
pass
return self
@property
def image(self) -> _EmbedMediaProxy:
"""Returns an ``EmbedProxy`` denoting the image contents.
Possible attributes you can access are:
- ``url`` for the image URL.
- ``proxy_url`` for the proxied image URL.
- ``width`` for the image width.
- ``height`` for the image height.
- ``flags`` for the image's attachment flags.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return EmbedMediaProxy(getattr(self, '_image', {})) # type: ignore
def set_image(self, *, url: Optional[Any]) -> Self:
"""Sets the image for the embed content.
This function returns the class instance to allow for fluent-style
chaining.
Parameters
-----------
url: Optional[:class:`str`]
The source URL for the image. Only HTTP(S) is supported.
If ``None`` is passed, any existing image is removed.
Inline attachment URLs are also supported, see :ref:`local_image`.
"""
if url is None:
try:
del self._image
except AttributeError:
pass
else:
self._image = {
'url': str(url),
}
return self
@property
def thumbnail(self) -> _EmbedMediaProxy:
"""Returns an ``EmbedProxy`` denoting the thumbnail contents.
Possible attributes you can access are:
- ``url`` for the thumbnail URL.
- ``proxy_url`` for the proxied thumbnail URL.
- ``width`` for the thumbnail width.
- ``height`` for the thumbnail height.
- ``flags`` for the thumbnail's attachment flags.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return EmbedMediaProxy(getattr(self, '_thumbnail', {})) # type: ignore
def set_thumbnail(self, *, url: Optional[Any]) -> Self:
"""Sets the thumbnail for the embed content.
This function returns the class instance to allow for fluent-style
chaining.
Parameters
-----------
url: Optional[:class:`str`]
The source URL for the thumbnail. Only HTTP(S) is supported.
If ``None`` is passed, any existing thumbnail is removed.
Inline attachment URLs are also supported, see :ref:`local_image`.
"""
if url is None:
try:
del self._thumbnail
except AttributeError:
pass
else:
self._thumbnail = {
'url': str(url),
}
return self
@property
def video(self) -> _EmbedMediaProxy:
"""Returns an ``EmbedProxy`` denoting the video contents.
Possible attributes include:
- ``url`` for the video URL.
- ``proxy_url`` for the proxied video URL.
- ``height`` for the video height.
- ``width`` for the video width.
- ``flags`` for the video's attachment flags.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return EmbedMediaProxy(getattr(self, '_video', {})) # type: ignore
@property
def provider(self) -> _EmbedProviderProxy:
"""Returns an ``EmbedProxy`` denoting the provider contents.
The only attributes that might be accessed are ``name`` and ``url``.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_provider', {})) # type: ignore
@property
def author(self) -> _EmbedAuthorProxy:
"""Returns an ``EmbedProxy`` denoting the author contents.
See :meth:`set_author` for possible values you can access.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_author', {})) # type: ignore
def set_author(self, *, name: Any, url: Optional[Any] = None, icon_url: Optional[Any] = None) -> Self:
"""Sets the author for the embed content.
This function returns the class instance to allow for fluent-style
chaining.
Parameters
-----------
name: :class:`str`
The name of the author. Can only be up to 256 characters.
url: :class:`str`
The URL for the author.
icon_url: :class:`str`
The URL of the author icon. Only HTTP(S) is supported.
Inline attachment URLs are also supported, see :ref:`local_image`.
"""
self._author = {
'name': str(name),
}
if url is not None:
self._author['url'] = str(url)
if icon_url is not None:
self._author['icon_url'] = str(icon_url)
return self
def remove_author(self) -> Self:
"""Clears embed's author information.
This function returns the class instance to allow for fluent-style
chaining.
.. versionadded:: 1.4
"""
try:
del self._author
except AttributeError:
pass
return self
@property
def fields(self) -> List[_EmbedFieldProxy]:
"""List[``EmbedProxy``]: Returns a :class:`list` of ``EmbedProxy`` denoting the field contents.
See :meth:`add_field` for possible values you can access.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return [EmbedProxy(d) for d in getattr(self, '_fields', [])] # type: ignore
def add_field(self, *, name: Any, value: Any, inline: bool = True) -> Self:
"""Adds a field to the embed object.
This function returns the class instance to allow for fluent-style
chaining. Can only be up to 25 fields.
Parameters
-----------
name: :class:`str`
The name of the field. Can only be up to 256 characters.
value: :class:`str`
The value of the field. Can only be up to 1024 characters.
inline: :class:`bool`
Whether the field should be displayed inline.
"""
field = {
'inline': inline,
'name': str(name),
'value': str(value),
}
try:
self._fields.append(field)
except AttributeError:
self._fields = [field]
return self
def insert_field_at(self, index: int, *, name: Any, value: Any, inline: bool = True) -> Self:
"""Inserts a field before a specified index to the embed.
This function returns the class instance to allow for fluent-style
chaining. Can only be up to 25 fields.
.. versionadded:: 1.2
Parameters
-----------
index: :class:`int`
The index of where to insert the field.
name: :class:`str`
The name of the field. Can only be up to 256 characters.
value: :class:`str`
The value of the field. Can only be up to 1024 characters.
inline: :class:`bool`
Whether the field should be displayed inline.
"""
field = {
'inline': inline,
'name': str(name),
'value': str(value),
}
try:
self._fields.insert(index, field)
except AttributeError:
self._fields = [field]
return self
def clear_fields(self) -> Self:
"""Removes all fields from this embed.
This function returns the class instance to allow for fluent-style
chaining.
.. versionchanged:: 2.0
This function now returns the class instance.
"""
try:
self._fields.clear()
except AttributeError:
self._fields = []
return self
def remove_field(self, index: int) -> Self:
"""Removes a field at a specified index.
If the index is invalid or out of bounds then the error is
silently swallowed.
This function returns the class instance to allow for fluent-style
chaining.
.. note::
When deleting a field by index, the index of the other fields
shift to fill the gap just like a regular list.
.. versionchanged:: 2.0
This function now returns the class instance.
Parameters
-----------
index: :class:`int`
The index of the field to remove.
"""
try:
del self._fields[index]
except (AttributeError, IndexError):
pass
return self
def set_field_at(self, index: int, *, name: Any, value: Any, inline: bool = True) -> Self:
"""Modifies a field to the embed object.
The index must point to a valid pre-existing field. Can only be up to 25 fields.
This function returns the class instance to allow for fluent-style
chaining.
Parameters
-----------
index: :class:`int`
The index of the field to modify.
name: :class:`str`
The name of the field. Can only be up to 256 characters.
value: :class:`str`
The value of the field. Can only be up to 1024 characters.
inline: :class:`bool`
Whether the field should be displayed inline.
Raises
-------
IndexError
An invalid index was provided.
"""
try:
field = self._fields[index]
except (TypeError, IndexError, AttributeError):
raise IndexError('field index out of range')
field['name'] = str(name)
field['value'] = str(value)
field['inline'] = inline
return self
def to_dict(self) -> EmbedData:
"""Converts this embed object into a dict."""
# add in the raw data into the dict
# fmt: off
result = {
key[1:]: getattr(self, key)
for key in self.__slots__
if key[0] == '_' and hasattr(self, key)
}
# fmt: on
# deal with basic convenience wrappers
try:
colour = result.pop('colour')
except KeyError:
pass
else:
if colour:
result['color'] = colour.value
try:
timestamp = result.pop('timestamp')
except KeyError:
pass
else:
if timestamp:
if timestamp.tzinfo:
result['timestamp'] = timestamp.astimezone(tz=datetime.timezone.utc).isoformat()
else:
result['timestamp'] = timestamp.replace(tzinfo=datetime.timezone.utc).isoformat()
# add in the non raw attribute ones
if self.type:
result['type'] = self.type
if self.description:
result['description'] = self.description
if self.url:
result['url'] = self.url
if self.title:
result['title'] = self.title
return result # type: ignore # This payload is equivalent to the EmbedData type
|