aboutsummaryrefslogtreecommitdiff
path: root/discord/message.py
diff options
context:
space:
mode:
authorLilly Rose Berner <[email protected]>2021-03-30 06:26:17 +0200
committerGitHub <[email protected]>2021-03-30 00:26:17 -0400
commitf60e91d70037618b2e93f33fb194b83757e2e5dc (patch)
tree6fb5d1bd0ea87aedfb2453e7429d2087d22b2f58 /discord/message.py
parent[docs] copy signature from overridden and inherited methods (diff)
downloaddiscord.py-f60e91d70037618b2e93f33fb194b83757e2e5dc.tar.xz
discord.py-f60e91d70037618b2e93f33fb194b83757e2e5dc.zip
Add support for fail_if_not_exists in MessageReference
Diffstat (limited to 'discord/message.py')
-rw-r--r--discord/message.py34
1 files changed, 28 insertions, 6 deletions
diff --git a/discord/message.py b/discord/message.py
index da2e9938..f85b0bb6 100644
--- a/discord/message.py
+++ b/discord/message.py
@@ -303,6 +303,12 @@ class MessageReference:
The channel id of the message referenced.
guild_id: Optional[:class:`int`]
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`]]
The message that this reference resolved to. If this is ``None``
then the original message was not fetched either due to the Discord API
@@ -315,14 +321,15 @@ class MessageReference:
.. 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.resolved = None
self.message_id = message_id
self.channel_id = channel_id
self.guild_id = guild_id
+ self.fail_if_not_exists = fail_if_not_exists
@classmethod
def with_state(cls, state, data):
@@ -335,7 +342,7 @@ class MessageReference:
return self
@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`.
.. versionadded:: 1.6
@@ -344,13 +351,18 @@ class MessageReference:
----------
message: :class:`~discord.Message`
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
-------
:class:`MessageReference`
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
return self
@@ -376,6 +388,8 @@ class MessageReference:
result['channel_id'] = self.channel_id
if self.guild_id is not None:
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
to_message_reference_dict = to_dict
@@ -1335,18 +1349,26 @@ class Message(Hashable):
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.
.. 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
---------
:class:`~discord.MessageReference`
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):
data = {