mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-19 15:36:02 +00:00
Change raw events to use slotted data models instead of parameters.
This allows for internal changes in Discord to not cause a breaking change every time something changes. Also implements #1214 which adds guild_id to every event where applicable. This is a breaking change.
This commit is contained in:
parent
e04629999b
commit
56650ae7c2
146
discord/raw_models.py
Normal file
146
discord/raw_models.py
Normal file
@ -0,0 +1,146 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2018 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.
|
||||
"""
|
||||
|
||||
class RawMessageDeleteEvent:
|
||||
"""Represents the event payload for a :func:`on_raw_message_delete` event.
|
||||
|
||||
Attributes
|
||||
------------
|
||||
channel_id: :class:`int`
|
||||
The channel ID where the deletion took place.
|
||||
guild_id: Optional[:class:`int`]
|
||||
The guild ID where the deletion took place, if applicable.
|
||||
message_id: :class:`int`
|
||||
The message ID that got deleted.
|
||||
"""
|
||||
|
||||
__slots__ = ('message_id', 'channel_id', 'guild_id')
|
||||
|
||||
def __init__(self, data):
|
||||
self.message_id = int(data['id'])
|
||||
self.channel_id = int(data['channel_id'])
|
||||
|
||||
try:
|
||||
self.guild_id = int(data['guild_id'])
|
||||
except KeyError:
|
||||
self.guild_id = None
|
||||
|
||||
class RawBulkMessageDeleteEvent:
|
||||
"""Represents the event payload for a :func:`on_raw_bulk_message_delete` event.
|
||||
|
||||
Attributes
|
||||
-----------
|
||||
message_ids: Set[:class:`int`]
|
||||
A :class:`set` of the message IDs that were deleted.
|
||||
channel_id: :class:`int`
|
||||
The channel ID where the message got deleted.
|
||||
guild_id: Optional[:class:`int`]
|
||||
The guild ID where the message got deleted, if applicable.
|
||||
"""
|
||||
|
||||
__slots__ = ('message_ids', 'channel_id', 'guild_id')
|
||||
|
||||
def __init__(self, data):
|
||||
self.message_ids = { int(x) for x in data.get('ids', []) }
|
||||
self.channel_id = int(data['channel_id'])
|
||||
|
||||
try:
|
||||
self.guild_id = int(data['guild_id'])
|
||||
except KeyError:
|
||||
self.guild_id = None
|
||||
|
||||
class RawMessageUpdateEvent:
|
||||
"""Represents the payload for a :func:`on_raw_message_edit` event.
|
||||
|
||||
Attributes
|
||||
-----------
|
||||
message_id: :class:`int`
|
||||
The message ID that got updated.
|
||||
data: :class:`dict`
|
||||
The raw data given by the
|
||||
`gateway <https://discordapp.com/developers/docs/topics/gateway#message-update>`_
|
||||
"""
|
||||
|
||||
__slots__ = ('message_id', 'data')
|
||||
|
||||
def __init__(self, data):
|
||||
self.message_id = int(data['id'])
|
||||
self.data = data
|
||||
|
||||
class RawReactionActionEvent:
|
||||
"""Represents the payload for a :func:`on_raw_reaction_add` or
|
||||
:func:`on_raw_reaction_remove` event.
|
||||
|
||||
Attributes
|
||||
-----------
|
||||
message_id: :class:`int`
|
||||
The message ID that got or lost a reaction.
|
||||
user_id: :class:`int`
|
||||
The user ID who added or removed the reaction.
|
||||
channel_id: :class:`int`
|
||||
The channel ID where the reaction got added or removed.
|
||||
guild_id: Optional[:class:`int`]
|
||||
The guild ID where the reaction got added or removed, if applicable.
|
||||
emoji: :class:`PartialEmoji`
|
||||
The custom or unicode emoji being used.
|
||||
"""
|
||||
|
||||
__slots__ = ('message_id', 'user_id', 'channel_id', 'guild_id', 'emoji')
|
||||
|
||||
def __init__(self, data, emoji):
|
||||
self.message_id = int(data['message_id'])
|
||||
self.channel_id = int(data['channel_id'])
|
||||
self.user_id = int(data['user_id'])
|
||||
self.emoji = emoji
|
||||
|
||||
try:
|
||||
self.guild_id = int(data['guild_id'])
|
||||
except KeyError:
|
||||
self.guild_id = None
|
||||
|
||||
class RawReactionClearEvent:
|
||||
"""Represents the payload for a :func:`on_raw_reaction_clear` event.
|
||||
|
||||
Attributes
|
||||
-----------
|
||||
message_id: :class:`int`
|
||||
The message ID that got its reactions cleared.
|
||||
channel_id: :class:`int`
|
||||
The channel ID where the reactions got cleared.
|
||||
guild_id: Optional[:class:`int`]
|
||||
The guild ID where the reactions got cleared.
|
||||
"""
|
||||
|
||||
__slots__ = ('message_id', 'channel_id', 'guild_id')
|
||||
|
||||
def __init__(self, data):
|
||||
self.message_id = int(data['message_id'])
|
||||
self.channel_id = int(data['channel_id'])
|
||||
|
||||
try:
|
||||
self.guild_id = int(data['guild_id'])
|
||||
except KeyError:
|
||||
self.guild_id = None
|
@ -31,6 +31,7 @@ from .emoji import Emoji, PartialEmoji
|
||||
from .message import Message
|
||||
from .relationship import Relationship
|
||||
from .channel import *
|
||||
from .raw_models import *
|
||||
from .member import Member
|
||||
from .role import Role
|
||||
from .enums import ChannelType, try_enum, Status
|
||||
@ -243,6 +244,17 @@ class ConnectionState:
|
||||
for chunk in range(math.ceil(guild._member_count / 1000)):
|
||||
yield self.receive_chunk(guild.id)
|
||||
|
||||
def _get_guild_channel(self, data):
|
||||
try:
|
||||
guild = self._get_guild(int(data['guild_id']))
|
||||
except KeyError:
|
||||
channel = self.get_channel(int(data['channel_id']))
|
||||
guild = None
|
||||
else:
|
||||
channel = guild and guild.get_channel(int(data['channel_id']))
|
||||
|
||||
return channel, guild
|
||||
|
||||
@asyncio.coroutine
|
||||
def request_offline_members(self, guilds):
|
||||
# get all the chunks
|
||||
@ -330,34 +342,33 @@ class ConnectionState:
|
||||
self.dispatch('resumed')
|
||||
|
||||
def parse_message_create(self, data):
|
||||
channel = self.get_channel(int(data['channel_id']))
|
||||
channel, _ = self._get_guild_channel(data)
|
||||
message = Message(channel=channel, data=data, state=self)
|
||||
self.dispatch('message', message)
|
||||
self._messages.append(message)
|
||||
|
||||
def parse_message_delete(self, data):
|
||||
message_id = int(data['id'])
|
||||
channel_id = int(data['channel_id'])
|
||||
self.dispatch('raw_message_delete', message_id, channel_id)
|
||||
raw = RawMessageDeleteEvent(data)
|
||||
self.dispatch('raw_message_delete', raw)
|
||||
|
||||
found = self._get_message(message_id)
|
||||
found = self._get_message(raw.message_id)
|
||||
if found is not None:
|
||||
self.dispatch('message_delete', found)
|
||||
self._messages.remove(found)
|
||||
|
||||
def parse_message_delete_bulk(self, data):
|
||||
message_ids = { int(x) for x in data.get('ids', []) }
|
||||
channel_id = int(data['channel_id'])
|
||||
self.dispatch('raw_bulk_message_delete', message_ids, channel_id)
|
||||
to_be_deleted = [message for message in self._messages if message.id in message_ids]
|
||||
raw = RawBulkMessageDeleteEvent(data)
|
||||
self.dispatch('raw_bulk_message_delete', raw)
|
||||
|
||||
to_be_deleted = [message for message in self._messages if message.id in raw.message_ids]
|
||||
for msg in to_be_deleted:
|
||||
self.dispatch('message_delete', msg)
|
||||
self._messages.remove(msg)
|
||||
|
||||
def parse_message_update(self, data):
|
||||
message_id = int(data['id'])
|
||||
self.dispatch('raw_message_edit', message_id, data)
|
||||
message = self._get_message(message_id)
|
||||
raw = RawMessageUpdateEvent(data)
|
||||
self.dispatch('raw_message_edit', raw)
|
||||
message = self._get_message(raw.message_id)
|
||||
if message is not None:
|
||||
older_message = copy.copy(message)
|
||||
if 'call' in data:
|
||||
@ -372,54 +383,47 @@ class ConnectionState:
|
||||
self.dispatch('message_edit', older_message, message)
|
||||
|
||||
def parse_message_reaction_add(self, data):
|
||||
message_id = int(data['message_id'])
|
||||
user_id = int(data['user_id'])
|
||||
channel_id = int(data['channel_id'])
|
||||
|
||||
emoji_data = data['emoji']
|
||||
emoji_id = utils._get_as_snowflake(emoji_data, 'id')
|
||||
emoji = PartialEmoji(animated=emoji_data['animated'], id=emoji_id, name=emoji_data['name'])
|
||||
self.dispatch('raw_reaction_add', emoji, message_id, channel_id, user_id)
|
||||
raw = RawReactionActionEvent(data, emoji)
|
||||
self.dispatch('raw_reaction_add', raw)
|
||||
|
||||
# rich interface here
|
||||
message = self._get_message(message_id)
|
||||
message = self._get_message(raw.message_id)
|
||||
if message is not None:
|
||||
emoji = self._upgrade_partial_emoji(emoji)
|
||||
reaction = message._add_reaction(data, emoji, user_id)
|
||||
user = self._get_reaction_user(message.channel, user_id)
|
||||
reaction = message._add_reaction(data, emoji, raw.user_id)
|
||||
user = self._get_reaction_user(message.channel, raw.user_id)
|
||||
if user:
|
||||
self.dispatch('reaction_add', reaction, user)
|
||||
|
||||
def parse_message_reaction_remove_all(self, data):
|
||||
message_id = int(data['message_id'])
|
||||
channel_id = int(data['channel_id'])
|
||||
self.dispatch('raw_reaction_clear', message_id, channel_id)
|
||||
raw = RawReactionClearEvent(data)
|
||||
self.dispatch('raw_reaction_clear', raw)
|
||||
|
||||
message = self._get_message(message_id)
|
||||
message = self._get_message(raw.message_id)
|
||||
if message is not None:
|
||||
old_reactions = message.reactions.copy()
|
||||
message.reactions.clear()
|
||||
self.dispatch('reaction_clear', message, old_reactions)
|
||||
|
||||
def parse_message_reaction_remove(self, data):
|
||||
message_id = int(data['message_id'])
|
||||
user_id = int(data['user_id'])
|
||||
channel_id = int(data['channel_id'])
|
||||
|
||||
emoji_data = data['emoji']
|
||||
emoji_id = utils._get_as_snowflake(emoji_data, 'id')
|
||||
emoji = PartialEmoji(animated=emoji_data['animated'], id=emoji_id, name=emoji_data['name'])
|
||||
self.dispatch('raw_reaction_remove', emoji, message_id, channel_id, user_id)
|
||||
raw = RawReactionActionEvent(data, emoji)
|
||||
self.dispatch('raw_reaction_remove', raw)
|
||||
|
||||
message = self._get_message(message_id)
|
||||
message = self._get_message(raw.message_id)
|
||||
if message is not None:
|
||||
emoji = self._upgrade_partial_emoji(emoji)
|
||||
try:
|
||||
reaction = message._remove_reaction(data, emoji, user_id)
|
||||
reaction = message._remove_reaction(data, emoji, raw.user_id)
|
||||
except (AttributeError, ValueError) as e: # eventual consistency lol
|
||||
pass
|
||||
else:
|
||||
user = self._get_reaction_user(message.channel, user_id)
|
||||
user = self._get_reaction_user(message.channel, raw.user_id)
|
||||
if user:
|
||||
self.dispatch('reaction_remove', reaction, user)
|
||||
|
||||
@ -804,14 +808,14 @@ class ConnectionState:
|
||||
compat.create_task(vc._create_socket(key_id, data))
|
||||
|
||||
def parse_typing_start(self, data):
|
||||
channel = self.get_channel(int(data['channel_id']))
|
||||
channel, guild = self._get_guild_channel(data)
|
||||
if channel is not None:
|
||||
member = None
|
||||
user_id = utils._get_as_snowflake(data, 'user_id')
|
||||
if isinstance(channel, DMChannel):
|
||||
member = channel.recipient
|
||||
elif isinstance(channel, TextChannel):
|
||||
member = channel.guild.get_member(user_id)
|
||||
member = guild.get_member(user_id)
|
||||
elif isinstance(channel, GroupChannel):
|
||||
member = utils.find(lambda x: x.id == user_id, channel.recipients)
|
||||
|
||||
|
74
docs/api.rst
74
docs/api.rst
@ -232,22 +232,21 @@ to handle it, which defaults to print a traceback and ignoring the exception.
|
||||
|
||||
:param message: A :class:`Message` of the deleted message.
|
||||
|
||||
.. function:: on_raw_message_delete(message_id, channel_id)
|
||||
.. function:: on_raw_message_delete(payload)
|
||||
|
||||
Called when a message is deleted. Unlike :func:`on_message_delete`, this is
|
||||
called regardless of the message being in the internal message cache or not.
|
||||
|
||||
:param int message_id: The message ID of the message being deleted.
|
||||
:param int channel_id: The channel ID where the message was deleted.
|
||||
:param payload: The raw event payload data.
|
||||
:type payload: :class:`RawMessageDeleteEvent`
|
||||
|
||||
.. function:: on_raw_bulk_message_delete(message_ids, channel_id)
|
||||
.. function:: on_raw_bulk_message_delete(payload)
|
||||
|
||||
Called when a bulk delete is triggered. This event is called regardless
|
||||
of the message IDs being in the internal message cache or not.
|
||||
|
||||
:param message_ids: The message IDs that were bulk deleted.
|
||||
:type message_ids: Set[int]
|
||||
:param int channel_id: The channel ID where the messages were deleted.
|
||||
:param payload: The raw event payload data.
|
||||
:type payload: :class:`RawBulkMessageDeleteEvent`
|
||||
|
||||
.. function:: on_message_edit(before, after)
|
||||
|
||||
@ -269,7 +268,7 @@ to handle it, which defaults to print a traceback and ignoring the exception.
|
||||
:param before: A :class:`Message` of the previous version of the message.
|
||||
:param after: A :class:`Message` of the current version of the message.
|
||||
|
||||
.. function:: on_raw_message_edit(message_id, data)
|
||||
.. function:: on_raw_message_edit(payload)
|
||||
|
||||
Called when a message is edited. Unlike :func:`on_message_edit`, this is called
|
||||
regardless of the state of the internal message cache.
|
||||
@ -282,8 +281,8 @@ to handle it, which defaults to print a traceback and ignoring the exception.
|
||||
denotes an "embed" only edit, which is an edit in which only the embeds are updated by the Discord
|
||||
embed server.
|
||||
|
||||
:param int message_id: The message ID of the message being edited.
|
||||
:param dict data: The raw data being passed to the MESSAGE_UPDATE gateway event.
|
||||
:param payload: The raw event payload data.
|
||||
:type payload: :class:`RawMessageUpdateEvent`
|
||||
|
||||
.. function:: on_reaction_add(reaction, user)
|
||||
|
||||
@ -298,16 +297,13 @@ to handle it, which defaults to print a traceback and ignoring the exception.
|
||||
:param reaction: A :class:`Reaction` showing the current state of the reaction.
|
||||
:param user: A :class:`User` or :class:`Member` of the user who added the reaction.
|
||||
|
||||
.. function:: on_raw_reaction_add(emoji, message_id, channel_id, user_id)
|
||||
.. function:: on_raw_reaction_add(payload)
|
||||
|
||||
Called when a reaction has a reaction added. Unlike :func:`on_reaction_add`, this is
|
||||
called regardless of the state of the internal message cache.
|
||||
|
||||
:param emoji: The custom or unicode emoji being reacted to.
|
||||
:type emoji: :class:`PartialEmoji`
|
||||
:param int message_id: The message ID of the message being reacted.
|
||||
:param int channel_id: The channel ID where the message belongs to.
|
||||
:param int user_id: The user ID of the user who did the reaction.
|
||||
:param payload: The raw event payload data.
|
||||
:type payload: :class:`RawReactionActionEvent`
|
||||
|
||||
.. function:: on_reaction_remove(reaction, user)
|
||||
|
||||
@ -322,16 +318,13 @@ to handle it, which defaults to print a traceback and ignoring the exception.
|
||||
:param reaction: A :class:`Reaction` showing the current state of the reaction.
|
||||
:param user: A :class:`User` or :class:`Member` of the user who removed the reaction.
|
||||
|
||||
.. function:: on_raw_reaction_remove(emoji, message_id, channel_id, user_id)
|
||||
.. function:: on_raw_reaction_remove(payload)
|
||||
|
||||
Called when a reaction has a reaction removed. Unlike :func:`on_reaction_remove`, this is
|
||||
called regardless of the state of the internal message cache.
|
||||
|
||||
:param emoji: The custom or unicode emoji that got un-reacted.
|
||||
:type emoji: :class:`PartialEmoji`
|
||||
:param int message_id: The message ID of the message being un-reacted.
|
||||
:param int channel_id: The channel ID where the message belongs to.
|
||||
:param int user_id: The user ID of the user who removed the reaction.
|
||||
:param payload: The raw event payload data.
|
||||
:type payload: :class:`RawReactionActionEvent`
|
||||
|
||||
.. function:: on_reaction_clear(message, reactions)
|
||||
|
||||
@ -342,13 +335,13 @@ to handle it, which defaults to print a traceback and ignoring the exception.
|
||||
:param message: The :class:`Message` that had its reactions cleared.
|
||||
:param reactions: A list of :class:`Reaction`\s that were removed.
|
||||
|
||||
.. function:: on_raw_reaction_clear(message_id, channel_id)
|
||||
.. function:: on_raw_reaction_clear(payload)
|
||||
|
||||
Called when a message has all its reactions removed. Unlike :func:`on_reaction_clear`,
|
||||
this is called regardless of the state of the internal message cache.
|
||||
|
||||
:param int message_id: The message ID of the message having its reactions removed.
|
||||
:param int channel_id: The channel ID of where the message belongs to.
|
||||
:param payload: The raw event payload data.
|
||||
:type payload: :class:`RawReactionClearEvent`
|
||||
|
||||
.. function:: on_private_channel_delete(channel)
|
||||
on_private_channel_create(channel)
|
||||
@ -1994,6 +1987,37 @@ Invite
|
||||
.. autoclass:: Invite()
|
||||
:members:
|
||||
|
||||
RawMessageDeleteEvent
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: RawMessageDeleteEvent()
|
||||
:members:
|
||||
|
||||
RawBulkMessageDeleteEvent
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: RawBulkMessageDeleteEvent()
|
||||
:members:
|
||||
|
||||
RawMessageUpdateEvent
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: RawMessageUpdateEvent()
|
||||
:members:
|
||||
|
||||
RawReactionActionEvent
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: RawReactionActionEvent()
|
||||
:members:
|
||||
|
||||
RawReactionClearEvent
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: RawReactionClearEvent()
|
||||
:members:
|
||||
|
||||
|
||||
.. _discord_api_data:
|
||||
|
||||
Data Classes
|
||||
|
Loading…
x
Reference in New Issue
Block a user