Browse Source

Add support for fail_if_not_exists in MessageReference

pull/6589/head
Lilly Rose Berner 4 years ago
committed by GitHub
parent
commit
f60e91d700
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      discord/message.py

34
discord/message.py

@ -303,6 +303,12 @@ class MessageReference:
The channel id of the message referenced. The channel id of the message referenced.
guild_id: Optional[:class:`int`] guild_id: Optional[:class:`int`]
The guild id of the message referenced. The guild id of the message referenced.
fail_if_not_exists: :class:`bool`
Whether replying to the referenced message should raise :class:`HTTPException`
if the message no longer exists or Discord could not fetch the message.
.. versionadded:: 1.7
resolved: Optional[Union[:class:`Message`, :class:`DeletedReferencedMessage`]] resolved: Optional[Union[:class:`Message`, :class:`DeletedReferencedMessage`]]
The message that this reference resolved to. If this is ``None`` The message that this reference resolved to. If this is ``None``
then the original message was not fetched either due to the Discord API then the original message was not fetched either due to the Discord API
@ -315,14 +321,15 @@ class MessageReference:
.. versionadded:: 1.6 .. versionadded:: 1.6
""" """
__slots__ = ('message_id', 'channel_id', 'guild_id', 'resolved', '_state') __slots__ = ('message_id', 'channel_id', 'guild_id', 'fail_if_not_exists', 'resolved', '_state')
def __init__(self, *, message_id, channel_id, guild_id=None): def __init__(self, *, message_id, channel_id, guild_id=None, fail_if_not_exists=True):
self._state = None self._state = None
self.resolved = None self.resolved = None
self.message_id = message_id self.message_id = message_id
self.channel_id = channel_id self.channel_id = channel_id
self.guild_id = guild_id self.guild_id = guild_id
self.fail_if_not_exists = fail_if_not_exists
@classmethod @classmethod
def with_state(cls, state, data): def with_state(cls, state, data):
@ -335,7 +342,7 @@ class MessageReference:
return self return self
@classmethod @classmethod
def from_message(cls, message): def from_message(cls, message, *, fail_if_not_exists=True):
"""Creates a :class:`MessageReference` from an existing :class:`~discord.Message`. """Creates a :class:`MessageReference` from an existing :class:`~discord.Message`.
.. versionadded:: 1.6 .. versionadded:: 1.6
@ -344,13 +351,18 @@ class MessageReference:
---------- ----------
message: :class:`~discord.Message` message: :class:`~discord.Message`
The message to be converted into a reference. The message to be converted into a reference.
fail_if_not_exists: :class:`bool`
Whether replying to the referenced message should raise :class:`HTTPException`
if the message no longer exists or Discord could not fetch the message.
.. versionadded:: 1.7
Returns Returns
------- -------
:class:`MessageReference` :class:`MessageReference`
A reference to the message. A reference to the message.
""" """
self = cls(message_id=message.id, channel_id=message.channel.id, guild_id=getattr(message.guild, 'id', None)) self = cls(message_id=message.id, channel_id=message.channel.id, guild_id=getattr(message.guild, 'id', None), fail_if_not_exists=fail_if_not_exists)
self._state = message._state self._state = message._state
return self return self
@ -376,6 +388,8 @@ class MessageReference:
result['channel_id'] = self.channel_id result['channel_id'] = self.channel_id
if self.guild_id is not None: if self.guild_id is not None:
result['guild_id'] = self.guild_id result['guild_id'] = self.guild_id
if self.fail_if_not_exists is not None:
result['fail_if_not_exists'] = self.fail_if_not_exists
return result return result
to_message_reference_dict = to_dict to_message_reference_dict = to_dict
@ -1335,18 +1349,26 @@ class Message(Hashable):
return await self.channel.send(content, reference=self, **kwargs) return await self.channel.send(content, reference=self, **kwargs)
def to_reference(self): def to_reference(self, *, fail_if_not_exists=True):
"""Creates a :class:`~discord.MessageReference` from the current message. """Creates a :class:`~discord.MessageReference` from the current message.
.. versionadded:: 1.6 .. versionadded:: 1.6
Parameters
----------
fail_if_not_exists: :class:`bool`
Whether replying using the message reference should raise :class:`HTTPException`
if the message no longer exists or Discord could not fetch the message.
.. versionadded:: 1.7
Returns Returns
--------- ---------
:class:`~discord.MessageReference` :class:`~discord.MessageReference`
The reference to this message. The reference to this message.
""" """
return MessageReference.from_message(self) return MessageReference.from_message(self, fail_if_not_exists=fail_if_not_exists)
def to_message_reference_dict(self): def to_message_reference_dict(self):
data = { data = {

Loading…
Cancel
Save