other methods connected with embed field

This commit is contained in:
grey-cat-1908 2022-04-14 11:59:26 +03:00
parent c783acd340
commit d998a0408d
6 changed files with 129 additions and 22 deletions

View file

@ -249,6 +249,7 @@ class Channel(APIModelBase):
return Channel.from_dict(data)
class MessageableChannel(Channel):
"""A subclass of ``Channel`` with methods that are only available for channels,
where user can send messages."""
@ -343,13 +344,13 @@ class MessageableChannel(Channel):
Parameters
----------
limit : Optional[:class:`~.melisa.Snowflake`]
limit : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Max number of messages to return (1-100).
around : Optional[:class:`~.melisa.Snowflake`]
around : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Get messages around this message ID.
before : Optional[:class:`~.melisa.Snowflake`]
before : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Get messages before this message ID.
after : Optional[:class:`~.melisa.Snowflake`]
after : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Get messages after this message ID.
Raises
@ -402,7 +403,7 @@ class MessageableChannel(Channel):
Parameters
----------
message_id : Optional[Union[:class:`int`, :class:`str`, :class:`~.melisa.Snowflake`]]
message_id : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Id of message to fetch.
Raises
@ -457,7 +458,7 @@ class MessageableChannel(Channel):
Parameters
----------
messages: List[:class:`~.melisa.Snowflake`]
messages: List[:class:`~.melisa.utils.snowflake.Snowflake`]
The list of message IDs to delete (2-100).
reason: Optional[:class:`str`]
The reason of the bulk delete messages operation.
@ -485,7 +486,7 @@ class MessageableChannel(Channel):
Parameters
----------
message_id: Union[:class:`int`, :class:`str`, :class:`~.melisa.Snowflake`]
message_id: Union[:class:`int`, :class:`str`, :class:`~.melisa.utils.snowflake.Snowflake`]
Id of message to delete.
reason: Optional[:class:`str`]
The reason of the message delete operation.
@ -570,13 +571,13 @@ class MessageableChannel(Channel):
Parameters
----------
limit : Optional[:class:`~.melisa.Snowflake`]
limit : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Max number of messages to purge.
around : Optional[:class:`~.melisa.Snowflake`]
around : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Get messages around this message ID.
before : Optional[:class:`~.melisa.Snowflake`]
before : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Get messages before this message ID.
after : Optional:class:`~.melisa.Snowflake`]
after : Optional[:class:`~.melisa.utils.snowflake.Snowflake`]
Get messages after this message ID.
reason: Optional[:class:`str`]
The reason of the channel purge operation.
@ -636,7 +637,8 @@ class MessageableChannel(Channel):
Parameters
----------
before: Optional[Union[:class:`~melisa.utils.Snowflake`, :class:`~melisa.utils.Timestamp`]]
before: Optional[Union[:class:`~melisa.utils.snowflake.Snowflake`,
:class:`~melisa.utils.snowflake.Timestamp`]]
Retrieve archived channels before the given date or ID.
limit: Optional[:class:`int`]
The number of threads to retrieve.
@ -834,9 +836,9 @@ class ThreadsList(APIModelBase):
Attributes
----------
threads: List[:class:`~melisa.Thread`]
threads: List[:class:`~melisa.models.guild.channel.Thread`]
Async iterator of threads. To get their type use them `.type` attribute.
members: List[:class:`~melisa.ThreadMember`]
members: List[:class:`~melisa.models.guild.thread.ThreadMember`]
Async iterator of thread members.
has_more: Optional[:class:`bool`]
Whether there are potentially additional threads that could be returned on a subsequent cal

View file

@ -87,10 +87,7 @@ class Webhook(APIModelBase):
url: APINullable[str] = None
async def delete(
self,
*,
webhook_id: Optional[Snowflake] = None,
reason: Optional[str] = None
self, *, webhook_id: Optional[Snowflake] = None, reason: Optional[str] = None
):
"""|coro|
Delete a webhook permanently. Requires the ``MANAGE_WEBHOOKS`` permission.

View file

@ -10,6 +10,7 @@ from enum import Enum
from typing import List, Union, Optional
from melisa.exceptions import EmbedFieldError
from melisa.utils.types import UNDEFINED, UndefinedOr
from melisa.utils.api_model import APIModelBase, APINullable
from melisa.utils.timestamp import Timestamp
@ -189,8 +190,6 @@ class EmbedField(APIModelBase):
@dataclass(repr=False)
class Embed(APIModelBase):
# ToDo: Add fields set method
"""Represents an embed sent in with message within Discord.
Attributes
@ -400,6 +399,85 @@ class Embed(APIModelBase):
return self
def edit_field(
self,
index: int,
*,
name: UndefinedOr[str] = UNDEFINED,
value: UndefinedOr[str] = UNDEFINED,
inline: UndefinedOr[bool] = UNDEFINED,
) -> Embed:
"""Edit an existing field on this embed.
Parameters
----------
index: :class:`int`
The index of the field to edit.
name: UndefinedOr[:class:`str`]
The name of the field.
value: UndefinedOr[:class:`str`]
The value of the field.
inline: UndefinedOr[:class:`bool`]
Whether the field should be displayed inline.
Returns
-------
Embed
This embed.
Raises
------
:class:`IndexError`
Raised if the index is greater than `len(embed.fields) - 1` or
less than `-len(embed.fields)`
"""
if not self.fields:
raise IndexError(index)
field = self.fields[index]
if name is not UNDEFINED:
field.name = name
if value is not UNDEFINED:
field.value = value
if inline is not UNDEFINED:
field.is_inline = inline
return self
def remove_field(self, index: int) -> Embed:
"""Remove an existing field from this embed.
Parameters
----------
index: :class:`int`
The index of the embed field to remove.
Returns
-------
Embed
This embed.
Raises
------
:class:`IndexError`
Raised if the index is greater than `len(embed.fields) - 1` or
less than `-len(embed.fields)`
"""
if self.fields:
del self.fields[index]
if not self.fields:
self.fields = None
return self
def clear_fields(self) -> Embed:
"""Removes all fields from this embed."""
self.fields.clear()
return self
def total_length(self) -> int:
"""Get the total character count of the embed.

View file

@ -1,10 +1,10 @@
# Copyright MelisaDev 2022 - Present
# Full MIT License can be found in `LICENSE.txt` at the project root.
from .types import Coro
from .types import Coro, UNDEFINED
from .timestamp import Timestamp
from .snowflake import Snowflake
from .api_model import APIModelBase
from .conversion import remove_none
__all__ = ("Coro", "Snowflake", "APIModelBase", "remove_none", "Timestamp")
__all__ = ("Coro", "Snowflake", "APIModelBase", "remove_none", "Timestamp", "UNDEFINED")

View file

@ -7,12 +7,38 @@ from sys import modules
from typing import TypeVar, Callable, Coroutine, Any, Union
class UndefinedType:
"""The type of the `UNDEFINED`."""
def __bool__(self):
return False
def __copy__(self):
return self
def __getstate__(self):
return False
def __repr__(self) -> str:
return "<UNDEFINED>"
def __reduce__(self) -> str:
return "<UNDEFINED>"
def __str__(self) -> str:
return "<UNDEFINED>"
UNDEFINED = UndefinedType()
T = TypeVar("T")
Coro = TypeVar("Coro", bound=Callable[..., Coroutine[Any, Any, Any]])
APINullable = Union[T, None]
UndefinedOr = Union[T, UndefinedType]
class Singleton(type):
# Thanks to this stackoverflow answer (method 3):

View file

@ -44,6 +44,10 @@ class TestEmbed:
embed = Embed().set_footer(text="cool footer text")
assert embed.total_length() == 16
def test_total_length_field_value(self):
embed = Embed().add_field(name="", value="best field value")
assert embed.total_length() == 16
def test_total_length_all(self):
embed = Embed(title="my title", description="simple description")
embed.set_author(name="best author")