diff --git a/discord/message.py b/discord/message.py index da2e99384..f85b0bb69 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 = {