Code cleanup involving enums and message replies
This commit is contained in:
parent
d1cb30cccf
commit
bd6ab93348
@ -39,7 +39,6 @@ from .invite import Invite
|
|||||||
from .file import File
|
from .file import File
|
||||||
from .voice_client import VoiceClient, VoiceProtocol
|
from .voice_client import VoiceClient, VoiceProtocol
|
||||||
from . import utils
|
from . import utils
|
||||||
from .message_reference import _MessageType, MessageReference
|
|
||||||
|
|
||||||
class _Undefined:
|
class _Undefined:
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@ -897,15 +896,13 @@ class Messageable(metaclass=abc.ABCMeta):
|
|||||||
|
|
||||||
if mention_author is not None:
|
if mention_author is not None:
|
||||||
allowed_mentions = allowed_mentions or {}
|
allowed_mentions = allowed_mentions or {}
|
||||||
allowed_mentions['replied_user'] = mention_author
|
allowed_mentions['replied_user'] = bool(mention_author)
|
||||||
|
|
||||||
if reference is not None:
|
if reference is not None:
|
||||||
if isinstance(reference, _MessageType):
|
try:
|
||||||
if not isinstance(reference, MessageReference):
|
reference = reference.to_message_reference_dict()
|
||||||
reference = reference.to_reference()
|
except AttributeError:
|
||||||
reference = reference.to_dict()
|
raise InvalidArgument('reference parameter must be Message or MessageReference') from None
|
||||||
else:
|
|
||||||
raise InvalidArgument('reference parameter must be Message or MessageReference')
|
|
||||||
|
|
||||||
if file is not None and files is not None:
|
if file is not None and files is not None:
|
||||||
raise InvalidArgument('cannot pass both file and files parameter to send()')
|
raise InvalidArgument('cannot pass both file and files parameter to send()')
|
||||||
|
@ -446,6 +446,11 @@ class ExpireBehaviour(Enum):
|
|||||||
|
|
||||||
ExpireBehavior = ExpireBehaviour
|
ExpireBehavior = ExpireBehaviour
|
||||||
|
|
||||||
|
class StickerType(Enum):
|
||||||
|
png = 1
|
||||||
|
apng = 2
|
||||||
|
lottie = 3
|
||||||
|
|
||||||
def try_enum(cls, val):
|
def try_enum(cls, val):
|
||||||
"""A function that tries to turn the value into enum ``cls``.
|
"""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]
|
return cls._enum_value_map_[val]
|
||||||
except (KeyError, TypeError, AttributeError):
|
except (KeyError, TypeError, AttributeError):
|
||||||
return val
|
return val
|
||||||
|
|
||||||
class StickerType(Enum):
|
|
||||||
png = 1
|
|
||||||
apng = 2
|
|
||||||
lottie = 3
|
|
||||||
|
@ -44,7 +44,6 @@ from .utils import escape_mentions
|
|||||||
from .guild import Guild
|
from .guild import Guild
|
||||||
from .mixins import Hashable
|
from .mixins import Hashable
|
||||||
from .mentions import AllowedMentions
|
from .mentions import AllowedMentions
|
||||||
from .message_reference import _MessageType, MessageReference
|
|
||||||
from .sticker import Sticker
|
from .sticker import Sticker
|
||||||
|
|
||||||
|
|
||||||
@ -213,6 +212,64 @@ class Attachment:
|
|||||||
return File(io.BytesIO(data), filename=self.filename, spoiler=spoiler)
|
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):
|
def flatten_handlers(cls):
|
||||||
prefix = len('_handle_')
|
prefix = len('_handle_')
|
||||||
handlers = [
|
handlers = [
|
||||||
@ -230,7 +287,7 @@ def flatten_handlers(cls):
|
|||||||
return cls
|
return cls
|
||||||
|
|
||||||
@flatten_handlers
|
@flatten_handlers
|
||||||
class Message(Hashable, _MessageType):
|
class Message(Hashable):
|
||||||
r"""Represents a message from Discord.
|
r"""Represents a message from Discord.
|
||||||
|
|
||||||
There should be no need to create one of these manually.
|
There should be no need to create one of these manually.
|
||||||
@ -1158,3 +1215,14 @@ class Message(Hashable, _MessageType):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
return MessageReference.from_message(self)
|
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
|
||||||
|
@ -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
|
|
Loading…
x
Reference in New Issue
Block a user