aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2020-11-27 00:32:08 -0500
committerRapptz <[email protected]>2020-11-27 00:32:08 -0500
commitbd6ab93348fbceee498ac11d5403d0c89c50ca0c (patch)
treec638501eb2f9382e45242f49b793454c5843986f
parentImplement discord.Message.reply (diff)
downloaddiscord.py-bd6ab93348fbceee498ac11d5403d0c89c50ca0c.tar.xz
discord.py-bd6ab93348fbceee498ac11d5403d0c89c50ca0c.zip
Code cleanup involving enums and message replies
-rw-r--r--discord/abc.py13
-rw-r--r--discord/enums.py10
-rw-r--r--discord/message.py72
-rw-r--r--discord/message_reference.py86
4 files changed, 80 insertions, 101 deletions
diff --git a/discord/abc.py b/discord/abc.py
index 4968ce1b..559dbde8 100644
--- a/discord/abc.py
+++ b/discord/abc.py
@@ -39,7 +39,6 @@ from .invite import Invite
from .file import File
from .voice_client import VoiceClient, VoiceProtocol
from . import utils
-from .message_reference import _MessageType, MessageReference
class _Undefined:
def __repr__(self):
@@ -897,15 +896,13 @@ class Messageable(metaclass=abc.ABCMeta):
if mention_author is not None:
allowed_mentions = allowed_mentions or {}
- allowed_mentions['replied_user'] = mention_author
+ allowed_mentions['replied_user'] = bool(mention_author)
if reference is not None:
- if isinstance(reference, _MessageType):
- if not isinstance(reference, MessageReference):
- reference = reference.to_reference()
- reference = reference.to_dict()
- else:
- raise InvalidArgument('reference parameter must be Message or MessageReference')
+ try:
+ reference = reference.to_message_reference_dict()
+ except AttributeError:
+ raise InvalidArgument('reference parameter must be Message or MessageReference') from None
if file is not None and files is not None:
raise InvalidArgument('cannot pass both file and files parameter to send()')
diff --git a/discord/enums.py b/discord/enums.py
index 871d7e49..fff7a153 100644
--- a/discord/enums.py
+++ b/discord/enums.py
@@ -446,6 +446,11 @@ class ExpireBehaviour(Enum):
ExpireBehavior = ExpireBehaviour
+class StickerType(Enum):
+ png = 1
+ apng = 2
+ lottie = 3
+
def try_enum(cls, val):
"""A function that tries to turn the value into enum ``cls``.
@@ -456,8 +461,3 @@ def try_enum(cls, val):
return cls._enum_value_map_[val]
except (KeyError, TypeError, AttributeError):
return val
-
-class StickerType(Enum):
- png = 1
- apng = 2
- lottie = 3
diff --git a/discord/message.py b/discord/message.py
index d1928464..3144c7ad 100644
--- a/discord/message.py
+++ b/discord/message.py
@@ -44,7 +44,6 @@ from .utils import escape_mentions
from .guild import Guild
from .mixins import Hashable
from .mentions import AllowedMentions
-from .message_reference import _MessageType, MessageReference
from .sticker import Sticker
@@ -213,6 +212,64 @@ class Attachment:
return File(io.BytesIO(data), filename=self.filename, spoiler=spoiler)
+class MessageReference:
+ """Represents a reference to a :class:`~discord.Message`.
+
+ .. versionadded:: 1.5
+
+ Attributes
+ -----------
+ message_id: Optional[:class:`int`]
+ The id of the message referenced.
+ channel_id: :class:`int`
+ The channel id of the message referenced.
+ guild_id: Optional[:class:`int`]
+ The guild id of the message referenced.
+ """
+
+ __slots__ = ('message_id', 'channel_id', 'guild_id', '_state')
+
+ def __init__(self, state, **kwargs):
+ self.message_id = utils._get_as_snowflake(kwargs, 'message_id')
+ self.channel_id = int(kwargs.pop('channel_id'))
+ self.guild_id = utils._get_as_snowflake(kwargs, 'guild_id')
+ self._state = state
+
+ @classmethod
+ def from_message(cls, message):
+ """Creates a :class:`MessageReference` from an existing :class:`~discord.Message`.
+
+ .. versionadded:: 1.6
+
+ Parameters
+ ----------
+ message: :class:`~discord.Message`
+ The message to be converted into a reference.
+
+ Returns
+ -------
+ :class:`MessageReference`
+ A reference to the message.
+ """
+ return cls(message._state, message_id=message.id, channel_id=message.channel.id, guild_id=getattr(message.guild, 'id', None))
+
+ @property
+ def cached_message(self):
+ """Optional[:class:`~discord.Message`]: The cached message, if found in the internal message cache."""
+ return self._state._get_message(self.message_id)
+
+ def __repr__(self):
+ return '<MessageReference message_id={0.message_id!r} channel_id={0.channel_id!r} guild_id={0.guild_id!r}>'.format(self)
+
+ def to_dict(self):
+ result = {'message_id': self.message_id} if self.message_id is not None else {}
+ result['channel_id'] = self.channel_id
+ if self.guild_id is not None:
+ result['guild_id'] = self.guild_id
+ return result
+
+ to_message_reference_dict = to_dict
+
def flatten_handlers(cls):
prefix = len('_handle_')
handlers = [
@@ -230,7 +287,7 @@ def flatten_handlers(cls):
return cls
@flatten_handlers
-class Message(Hashable, _MessageType):
+class Message(Hashable):
r"""Represents a message from Discord.
There should be no need to create one of these manually.
@@ -1158,3 +1215,14 @@ class Message(Hashable, _MessageType):
"""
return MessageReference.from_message(self)
+
+ def to_message_reference_dict(self):
+ data = {
+ 'message_id': self.id,
+ 'channel_id': self.channel.id,
+ }
+
+ if self.guild is not None:
+ data['guild_id'] = self.guild.id
+
+ return data
diff --git a/discord/message_reference.py b/discord/message_reference.py
deleted file mode 100644
index 3dcb7871..00000000
--- a/discord/message_reference.py
+++ /dev/null
@@ -1,86 +0,0 @@
-# -*- coding: utf-8 -*-
-
-"""
-The MIT License (MIT)
-
-Copyright (c) 2015-2020 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 . import utils
-
-class _MessageType:
- __slots__ = ()
-
-class MessageReference(_MessageType):
- """Represents a reference to a :class:`~discord.Message`.
-
- .. versionadded:: 1.5
-
- Attributes
- -----------
- message_id: Optional[:class:`int`]
- The id of the message referenced.
- channel_id: :class:`int`
- The channel id of the message referenced.
- guild_id: Optional[:class:`int`]
- The guild id of the message referenced.
- """
-
- __slots__ = ('message_id', 'channel_id', 'guild_id', '_state')
-
- def __init__(self, state, **kwargs):
- self.message_id = utils._get_as_snowflake(kwargs, 'message_id')
- self.channel_id = int(kwargs.pop('channel_id'))
- self.guild_id = utils._get_as_snowflake(kwargs, 'guild_id')
- self._state = state
-
- @classmethod
- def from_message(cls, message):
- """Creates a :class:`MessageReference` from an existing :class:`~discord.Message`.
-
- .. versionadded:: 1.6
-
- Parameters
- ----------
- message: :class:`~discord.Message`
- The message to be converted into a reference.
-
- Returns
- -------
- :class:`MessageReference`
- A reference to the message.
- """
- return cls(message._state, message_id=message.id, channel_id=message.channel.id, guild_id=getattr(message.guild, 'id', None))
-
- @property
- def cached_message(self):
- """Optional[:class:`~discord.Message`]: The cached message, if found in the internal message cache."""
- return self._state._get_message(self.message_id)
-
- def __repr__(self):
- return '<MessageReference message_id={0.message_id!r} channel_id={0.channel_id!r} guild_id={0.guild_id!r}>'.format(self)
-
- def to_dict(self):
- result = {'message_id': self.message_id} if self.message_id is not None else {}
- result['channel_id'] = self.channel_id
- if self.guild_id is not None:
- result['guild_id'] = self.guild_id
- return result