|
@ -33,10 +33,12 @@ from .message import Message |
|
|
from .channel import * |
|
|
from .channel import * |
|
|
from .member import Member |
|
|
from .member import Member |
|
|
from .role import Role |
|
|
from .role import Role |
|
|
from . import utils, compat |
|
|
|
|
|
from .enums import Status, ChannelType, try_enum |
|
|
from .enums import Status, ChannelType, try_enum |
|
|
from .calls import GroupCall |
|
|
from .calls import GroupCall |
|
|
|
|
|
|
|
|
|
|
|
import discord.utils |
|
|
|
|
|
import discord.compat |
|
|
|
|
|
|
|
|
from collections import deque, namedtuple |
|
|
from collections import deque, namedtuple |
|
|
import copy, enum, math |
|
|
import copy, enum, math |
|
|
import datetime |
|
|
import datetime |
|
@ -180,7 +182,7 @@ class ConnectionState: |
|
|
self._private_channels_by_user.pop(channel.recipient.id, None) |
|
|
self._private_channels_by_user.pop(channel.recipient.id, None) |
|
|
|
|
|
|
|
|
def _get_message(self, msg_id): |
|
|
def _get_message(self, msg_id): |
|
|
return utils.find(lambda m: m.id == msg_id, self.messages) |
|
|
return discord.utils.find(lambda m: m.id == msg_id, self.messages) |
|
|
|
|
|
|
|
|
def _add_guild_from_data(self, guild): |
|
|
def _add_guild_from_data(self, guild): |
|
|
guild = Guild(data=guild, state=self.ctx) |
|
|
guild = Guild(data=guild, state=self.ctx) |
|
@ -251,7 +253,7 @@ class ConnectionState: |
|
|
factory, _ = _channel_factory(pm['type']) |
|
|
factory, _ = _channel_factory(pm['type']) |
|
|
self._add_private_channel(factory(me=self.user, data=pm, state=self.ctx)) |
|
|
self._add_private_channel(factory(me=self.user, data=pm, state=self.ctx)) |
|
|
|
|
|
|
|
|
compat.create_task(self._delay_ready(), loop=self.loop) |
|
|
discord.compat.create_task(self._delay_ready(), loop=self.loop) |
|
|
|
|
|
|
|
|
def parse_resumed(self, data): |
|
|
def parse_resumed(self, data): |
|
|
self.dispatch('resumed') |
|
|
self.dispatch('resumed') |
|
@ -317,7 +319,7 @@ class ConnectionState: |
|
|
self.dispatch('reaction_remove', reaction, user) |
|
|
self.dispatch('reaction_remove', reaction, user) |
|
|
|
|
|
|
|
|
def parse_presence_update(self, data): |
|
|
def parse_presence_update(self, data): |
|
|
guild = self._get_guild(utils._get_as_snowflake(data, 'guild_id')) |
|
|
guild = self._get_guild(discord.utils._get_as_snowflake(data, 'guild_id')) |
|
|
if guild is None: |
|
|
if guild is None: |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
@ -342,7 +344,7 @@ class ConnectionState: |
|
|
self.user = User(state=self.ctx, data=data) |
|
|
self.user = User(state=self.ctx, data=data) |
|
|
|
|
|
|
|
|
def parse_channel_delete(self, data): |
|
|
def parse_channel_delete(self, data): |
|
|
guild = self._get_guild(utils._get_as_snowflake(data, 'guild_id')) |
|
|
guild = self._get_guild(discord.utils._get_as_snowflake(data, 'guild_id')) |
|
|
channel_id = int(data['id']) |
|
|
channel_id = int(data['id']) |
|
|
if guild is not None: |
|
|
if guild is not None: |
|
|
channel = guild.get_channel(channel_id) |
|
|
channel = guild.get_channel(channel_id) |
|
@ -365,7 +367,7 @@ class ConnectionState: |
|
|
self.dispatch('channel_update', old_channel, channel) |
|
|
self.dispatch('channel_update', old_channel, channel) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
guild = self._get_guild(utils._get_as_snowflake(data, 'guild_id')) |
|
|
guild = self._get_guild(discord.utils._get_as_snowflake(data, 'guild_id')) |
|
|
if guild is not None: |
|
|
if guild is not None: |
|
|
channel = guild.get_channel(channel_id) |
|
|
channel = guild.get_channel(channel_id) |
|
|
if channel is not None: |
|
|
if channel is not None: |
|
@ -380,7 +382,7 @@ class ConnectionState: |
|
|
channel = factory(me=self.user, data=data, state=self.ctx) |
|
|
channel = factory(me=self.user, data=data, state=self.ctx) |
|
|
self._add_private_channel(channel) |
|
|
self._add_private_channel(channel) |
|
|
else: |
|
|
else: |
|
|
guild = self._get_guild(utils._get_as_snowflake(data, 'guild_id')) |
|
|
guild = self._get_guild(discord.utils._get_as_snowflake(data, 'guild_id')) |
|
|
if guild is not None: |
|
|
if guild is not None: |
|
|
channel = factory(guild=guild, state=self.ctx, data=data) |
|
|
channel = factory(guild=guild, state=self.ctx, data=data) |
|
|
guild._add_channel(channel) |
|
|
guild._add_channel(channel) |
|
@ -406,7 +408,7 @@ class ConnectionState: |
|
|
def _make_member(self, guild, data): |
|
|
def _make_member(self, guild, data): |
|
|
roles = [guild.default_role] |
|
|
roles = [guild.default_role] |
|
|
for roleid in data.get('roles', []): |
|
|
for roleid in data.get('roles', []): |
|
|
role = utils.get(guild.roles, id=roleid) |
|
|
role = discord.utils.get(guild.roles, id=roleid) |
|
|
if role is not None: |
|
|
if role is not None: |
|
|
roles.append(role) |
|
|
roles.append(role) |
|
|
|
|
|
|
|
@ -514,7 +516,7 @@ class ConnectionState: |
|
|
|
|
|
|
|
|
# since we're not waiting for 'useful' READY we'll just |
|
|
# since we're not waiting for 'useful' READY we'll just |
|
|
# do the chunk request here |
|
|
# do the chunk request here |
|
|
compat.create_task(self._chunk_and_dispatch(guild, unavailable), loop=self.loop) |
|
|
discord.compat.create_task(self._chunk_and_dispatch(guild, unavailable), loop=self.loop) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
# Dispatch available if newly available |
|
|
# Dispatch available if newly available |
|
@ -561,7 +563,7 @@ class ConnectionState: |
|
|
guild = self._get_guild(int(data['guild_id'])) |
|
|
guild = self._get_guild(int(data['guild_id'])) |
|
|
if guild is not None: |
|
|
if guild is not None: |
|
|
user_id = data.get('user', {}).get('id') |
|
|
user_id = data.get('user', {}).get('id') |
|
|
member = utils.get(guild.members, id=user_id) |
|
|
member = discord.utils.get(guild.members, id=user_id) |
|
|
if member is not None: |
|
|
if member is not None: |
|
|
self.dispatch('member_ban', member) |
|
|
self.dispatch('member_ban', member) |
|
|
|
|
|
|
|
@ -583,7 +585,7 @@ class ConnectionState: |
|
|
guild = self._get_guild(int(data['guild_id'])) |
|
|
guild = self._get_guild(int(data['guild_id'])) |
|
|
if guild is not None: |
|
|
if guild is not None: |
|
|
role_id = int(data['role_id']) |
|
|
role_id = int(data['role_id']) |
|
|
role = utils.find(lambda r: r.id == role_id, guild.roles) |
|
|
role = discord.utils.find(lambda r: r.id == role_id, guild.roles) |
|
|
try: |
|
|
try: |
|
|
guild._remove_role(role) |
|
|
guild._remove_role(role) |
|
|
except ValueError: |
|
|
except ValueError: |
|
@ -596,7 +598,7 @@ class ConnectionState: |
|
|
if guild is not None: |
|
|
if guild is not None: |
|
|
role_data = data['role'] |
|
|
role_data = data['role'] |
|
|
role_id = int(role_data['id']) |
|
|
role_id = int(role_data['id']) |
|
|
role = utils.find(lambda r: r.id == role_id, guild.roles) |
|
|
role = discord.utils.find(lambda r: r.id == role_id, guild.roles) |
|
|
if role is not None: |
|
|
if role is not None: |
|
|
old_role = copy.copy(role) |
|
|
old_role = copy.copy(role) |
|
|
role._update(role_data) |
|
|
role._update(role_data) |
|
@ -615,8 +617,8 @@ class ConnectionState: |
|
|
self.process_listeners(ListenerType.chunk, guild, len(members)) |
|
|
self.process_listeners(ListenerType.chunk, guild, len(members)) |
|
|
|
|
|
|
|
|
def parse_voice_state_update(self, data): |
|
|
def parse_voice_state_update(self, data): |
|
|
guild = self._get_guild(utils._get_as_snowflake(data, 'guild_id')) |
|
|
guild = self._get_guild(discord.utils._get_as_snowflake(data, 'guild_id')) |
|
|
channel_id = utils._get_as_snowflake(data, 'channel_id') |
|
|
channel_id = discord.utils._get_as_snowflake(data, 'channel_id') |
|
|
if guild is not None: |
|
|
if guild is not None: |
|
|
if int(data['user_id']) == self.user.id: |
|
|
if int(data['user_id']) == self.user.id: |
|
|
voice = self._get_voice_client(guild.id) |
|
|
voice = self._get_voice_client(guild.id) |
|
@ -636,13 +638,13 @@ class ConnectionState: |
|
|
channel = self.get_channel(int(data['channel_id'])) |
|
|
channel = self.get_channel(int(data['channel_id'])) |
|
|
if channel is not None: |
|
|
if channel is not None: |
|
|
member = None |
|
|
member = None |
|
|
user_id = utils._get_as_snowflake(data, 'user_id') |
|
|
user_id = discord.utils._get_as_snowflake(data, 'user_id') |
|
|
if isinstance(channel, DMChannel): |
|
|
if isinstance(channel, DMChannel): |
|
|
member = channel.recipient |
|
|
member = channel.recipient |
|
|
elif isinstance(channel, TextChannel): |
|
|
elif isinstance(channel, TextChannel): |
|
|
member = channel.guild.get_member(user_id) |
|
|
member = channel.guild.get_member(user_id) |
|
|
elif isinstance(channel, GroupChannel): |
|
|
elif isinstance(channel, GroupChannel): |
|
|
member = utils.find(lambda x: x.id == user_id, channel.recipients) |
|
|
member = discord.utils.find(lambda x: x.id == user_id, channel.recipients) |
|
|
|
|
|
|
|
|
if member is not None: |
|
|
if member is not None: |
|
|
timestamp = datetime.datetime.utcfromtimestamp(data.get('timestamp')) |
|
|
timestamp = datetime.datetime.utcfromtimestamp(data.get('timestamp')) |
|
@ -673,12 +675,12 @@ class ConnectionState: |
|
|
elif isinstance(channel, TextChannel): |
|
|
elif isinstance(channel, TextChannel): |
|
|
return channel.guild.get_member(user_id) |
|
|
return channel.guild.get_member(user_id) |
|
|
elif isinstance(channel, GroupChannel): |
|
|
elif isinstance(channel, GroupChannel): |
|
|
return utils.find(lambda m: m.id == user_id, channel.recipients) |
|
|
return discord.utils.find(lambda m: m.id == user_id, channel.recipients) |
|
|
else: |
|
|
else: |
|
|
return None |
|
|
return None |
|
|
|
|
|
|
|
|
def _get_reaction_emoji(self, data): |
|
|
def _get_reaction_emoji(self, data): |
|
|
emoji_id = utils._get_as_snowflake(data, 'id') |
|
|
emoji_id = discord.utils._get_as_snowflake(data, 'id') |
|
|
|
|
|
|
|
|
if not emoji_id: |
|
|
if not emoji_id: |
|
|
return data['name'] |
|
|
return data['name'] |
|
|