|
@ -1,25 +1,19 @@ |
|
|
import inflection |
|
|
import inflection |
|
|
import skema |
|
|
|
|
|
import six |
|
|
import six |
|
|
|
|
|
|
|
|
from disco.util import skema_find_recursive_by_type |
|
|
|
|
|
from disco.types import Guild, Channel, User, GuildMember, Role, Message, VoiceState |
|
|
from disco.types import Guild, Channel, User, GuildMember, Role, Message, VoiceState |
|
|
|
|
|
from disco.types.base import Model, snowflake, alias, listof |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: clean this... use BaseType, etc |
|
|
# TODO: clean this... use BaseType, etc |
|
|
class GatewayEvent(skema.Model): |
|
|
class GatewayEvent(Model): |
|
|
@staticmethod |
|
|
@staticmethod |
|
|
def from_dispatch(client, data): |
|
|
def from_dispatch(client, data): |
|
|
cls = globals().get(inflection.camelize(data['t'].lower())) |
|
|
cls = globals().get(inflection.camelize(data['t'].lower())) |
|
|
if not cls: |
|
|
if not cls: |
|
|
raise Exception('Could not find cls for {}'.format(data['t'])) |
|
|
raise Exception('Could not find cls for {}'.format(data['t'])) |
|
|
|
|
|
|
|
|
obj = cls.create(data['d']) |
|
|
return cls.create(data['d']) |
|
|
|
|
|
|
|
|
for field, value in skema_find_recursive_by_type(obj, skema.ModelType): |
|
|
|
|
|
value.client = client |
|
|
|
|
|
|
|
|
|
|
|
return obj |
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
@classmethod |
|
|
def create(cls, obj): |
|
|
def create(cls, obj): |
|
@ -28,31 +22,29 @@ class GatewayEvent(skema.Model): |
|
|
alias, model = cls._wraps_model |
|
|
alias, model = cls._wraps_model |
|
|
|
|
|
|
|
|
data = { |
|
|
data = { |
|
|
k: obj.pop(k) for k in six.iterkeys(model._fields_by_stored_name) if k in obj |
|
|
k: obj.pop(k) for k in six.iterkeys(model._fields) if k in obj |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
obj[alias] = data |
|
|
obj[alias] = data |
|
|
|
|
|
|
|
|
self = cls(obj) |
|
|
return cls(obj) |
|
|
self.validate() |
|
|
|
|
|
return self |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wraps_model(model, alias=None): |
|
|
def wraps_model(model, alias=None): |
|
|
alias = alias or model.__name__.lower() |
|
|
alias = alias or model.__name__.lower() |
|
|
|
|
|
|
|
|
def deco(cls): |
|
|
def deco(cls): |
|
|
cls.add_field(alias, skema.ModelType(model)) |
|
|
cls._fields[alias] = model |
|
|
cls._wraps_model = (alias, model) |
|
|
cls._wraps_model = (alias, model) |
|
|
return cls |
|
|
return cls |
|
|
return deco |
|
|
return deco |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Ready(GatewayEvent): |
|
|
class Ready(GatewayEvent): |
|
|
version = skema.IntType(stored_name='v') |
|
|
version = alias(int, 'v') |
|
|
session_id = skema.StringType() |
|
|
session_id = str |
|
|
user = skema.ModelType(User) |
|
|
user = User |
|
|
guilds = skema.ListType(skema.ModelType(Guild)) |
|
|
guilds = listof(Guild) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Resumed(GatewayEvent): |
|
|
class Resumed(GatewayEvent): |
|
@ -61,17 +53,17 @@ class Resumed(GatewayEvent): |
|
|
|
|
|
|
|
|
@wraps_model(Guild) |
|
|
@wraps_model(Guild) |
|
|
class GuildCreate(GatewayEvent): |
|
|
class GuildCreate(GatewayEvent): |
|
|
unavailable = skema.BooleanType(default=None) |
|
|
unavailable = bool |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@wraps_model(Guild) |
|
|
@wraps_model(Guild) |
|
|
class GuildUpdate(GatewayEvent): |
|
|
class GuildUpdate(GatewayEvent): |
|
|
guild = skema.ModelType(Guild) |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GuildDelete(GatewayEvent): |
|
|
class GuildDelete(GatewayEvent): |
|
|
id = skema.SnowflakeType() |
|
|
id = snowflake |
|
|
unavailable = skema.BooleanType(default=None) |
|
|
unavailable = bool |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@wraps_model(Channel) |
|
|
@wraps_model(Channel) |
|
@ -90,8 +82,8 @@ class ChannelDelete(ChannelCreate): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChannelPinsUpdate(GatewayEvent): |
|
|
class ChannelPinsUpdate(GatewayEvent): |
|
|
channel_id = skema.SnowflakeType() |
|
|
channel_id = snowflake |
|
|
last_pin_timestamp = skema.IntType() |
|
|
last_pin_timestamp = int |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@wraps_model(User) |
|
|
@wraps_model(User) |
|
@ -112,8 +104,8 @@ class GuildIntegrationsUpdate(GatewayEvent): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GuildMembersChunk(GatewayEvent): |
|
|
class GuildMembersChunk(GatewayEvent): |
|
|
guild_id = skema.SnowflakeType() |
|
|
guild_id = snowflake |
|
|
members = skema.ListType(skema.ModelType(GuildMember)) |
|
|
members = listof(GuildMember) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@wraps_model(GuildMember, alias='member') |
|
|
@wraps_model(GuildMember, alias='member') |
|
@ -122,29 +114,27 @@ class GuildMemberAdd(GatewayEvent): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GuildMemberRemove(GatewayEvent): |
|
|
class GuildMemberRemove(GatewayEvent): |
|
|
guild_id = skema.SnowflakeType() |
|
|
guild_id = snowflake |
|
|
user = skema.ModelType(User) |
|
|
user = User |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GuildMemberUpdate(GatewayEvent): |
|
|
class GuildMemberUpdate(GatewayEvent): |
|
|
guild_id = skema.SnowflakeType() |
|
|
guild_id = snowflake |
|
|
user = skema.ModelType(User) |
|
|
user = User |
|
|
roles = skema.ListType(skema.SnowflakeType()) |
|
|
roles = listof(snowflake) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GuildRoleCreate(GatewayEvent): |
|
|
class GuildRoleCreate(GatewayEvent): |
|
|
guild_id = skema.SnowflakeType() |
|
|
guild_id = snowflake |
|
|
role = skema.ModelType(Role) |
|
|
role = Role |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GuildRoleUpdate(GatewayEvent): |
|
|
class GuildRoleUpdate(GuildRoleCreate): |
|
|
guild_id = skema.SnowflakeType() |
|
|
pass |
|
|
role = skema.ModelType(Role) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GuildRoleDelete(GatewayEvent): |
|
|
class GuildRoleDelete(GuildRoleCreate): |
|
|
guild_id = skema.SnowflakeType() |
|
|
pass |
|
|
role = skema.ModelType(Role) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@wraps_model(Message) |
|
|
@wraps_model(Message) |
|
@ -159,32 +149,33 @@ class MessageUpdate(MessageCreate): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MessageDelete(GatewayEvent): |
|
|
class MessageDelete(GatewayEvent): |
|
|
id = skema.SnowflakeType() |
|
|
id = snowflake |
|
|
channel_id = skema.SnowflakeType() |
|
|
channel_id = snowflake |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MessageDeleteBulk(GatewayEvent): |
|
|
class MessageDeleteBulk(GatewayEvent): |
|
|
channel_id = skema.SnowflakeType() |
|
|
channel_id = snowflake |
|
|
ids = skema.ListType(skema.SnowflakeType()) |
|
|
ids = listof(snowflake) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PresenceUpdate(GatewayEvent): |
|
|
class PresenceUpdate(GatewayEvent): |
|
|
class Game(skema.Model): |
|
|
class Game(Model): |
|
|
type = skema.IntType() |
|
|
# TODO enum |
|
|
name = skema.StringType() |
|
|
type = int |
|
|
url = skema.StringType(required=False) |
|
|
name = str |
|
|
|
|
|
url = str |
|
|
|
|
|
|
|
|
user = skema.ModelType(User) |
|
|
user = User |
|
|
guild_id = skema.SnowflakeType() |
|
|
guild_id = snowflake |
|
|
roles = skema.ListType(skema.SnowflakeType()) |
|
|
roles = listof(snowflake) |
|
|
game = skema.ModelType(Game) |
|
|
game = Game |
|
|
status = skema.StringType() |
|
|
status = str |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TypingStart(GatewayEvent): |
|
|
class TypingStart(GatewayEvent): |
|
|
channel_id = skema.SnowflakeType() |
|
|
channel_id = snowflake |
|
|
user_id = skema.SnowflakeType() |
|
|
user_id = snowflake |
|
|
timestamp = skema.IntType() |
|
|
timestamp = snowflake |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@wraps_model(VoiceState, alias='state') |
|
|
@wraps_model(VoiceState, alias='state') |
|
@ -193,6 +184,6 @@ class VoiceStateUpdate(GatewayEvent): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VoiceServerUpdate(GatewayEvent): |
|
|
class VoiceServerUpdate(GatewayEvent): |
|
|
token = skema.StringType() |
|
|
token = str |
|
|
endpoint = skema.StringType() |
|
|
endpoint = str |
|
|
guild_id = skema.SnowflakeType() |
|
|
guild_id = snowflake |
|
|