Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
Add presence tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
b1naryth1ef committed Oct 9, 2016
1 parent d8f5036 commit fc53496
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
19 changes: 8 additions & 11 deletions disco/gateway/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import inflection
import six

from disco.types import Guild, Channel, User, GuildMember, Role, Message, VoiceState
from disco.types.base import Model, Field, snowflake, listof, text
from disco.types.user import User, Presence
from disco.types.channel import Channel
from disco.types.message import Message
from disco.types.voice import VoiceState
from disco.types.guild import Guild, GuildMember, Role

from disco.types.base import Model, Field, snowflake, listof


class GatewayEvent(Model):
Expand Down Expand Up @@ -267,21 +272,13 @@ class MessageDeleteBulk(GatewayEvent):
ids = Field(listof(snowflake))


@wraps_model(Presence)
class PresenceUpdate(GatewayEvent):
"""
Sent when a users presence is updated.
"""
class Game(Model):
# TODO enum
type = Field(int)
name = Field(text)
url = Field(text)

user = Field(User)
guild_id = Field(snowflake)
roles = Field(listof(snowflake))
game = Field(Game)
status = Field(text)


class TypingStart(GatewayEvent):
Expand Down
5 changes: 5 additions & 0 deletions disco/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class State(object):
'Ready', 'GuildCreate', 'GuildUpdate', 'GuildDelete', 'GuildMemberAdd', 'GuildMemberRemove',
'GuildMemberUpdate', 'GuildMembersChunk', 'GuildRoleCreate', 'GuildRoleUpdate', 'GuildRoleDelete',
'ChannelCreate', 'ChannelUpdate', 'ChannelDelete', 'VoiceStateUpdate', 'MessageCreate',
'PresenceUpdate'
]

def __init__(self, client, config=None):
Expand Down Expand Up @@ -262,3 +263,7 @@ def on_guild_role_delete(self, event):
return

del self.guilds[event.guild_id].roles[event.role.id]

def on_presence_update(self, event):
if event.user.id in self.users:
self.users[event.user.id].presence = event.presence
15 changes: 14 additions & 1 deletion disco/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import functools

from holster.enum import BaseEnumMeta
from datetime import datetime as real_datetime

DATETIME_FORMATS = [
Expand All @@ -10,10 +11,19 @@
]


class ConversionError(Exception):
def __init__(self, field, raw, e):
super(ConversionError, self).__init__(
'Failed to convert `{}` (`{}`) to {}: {}'.format(
raw, field.src_name, field.typ, e))


class FieldType(object):
def __init__(self, typ):
if isinstance(typ, FieldType) or inspect.isclass(typ) and issubclass(typ, Model):
self.typ = typ
elif isinstance(typ, BaseEnumMeta):
self.typ = lambda raw, _: typ.get(raw)
else:
self.typ = lambda raw, _: typ(raw)

Expand Down Expand Up @@ -48,7 +58,10 @@ def has_default(self):
return self.default is not None

def try_convert(self, raw, client):
return self.typ(raw, client)
try:
return self.typ(raw, client)
except Exception as e:
raise ConversionError(self, raw, e)


class _Dict(FieldType):
Expand Down
30 changes: 30 additions & 0 deletions disco/types/user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from holster.enum import Enum

from disco.types.base import Model, Field, snowflake, text, binary, with_equality, with_hash


Expand All @@ -9,6 +11,8 @@ class User(Model, with_equality('id'), with_hash('id')):
verified = Field(bool)
email = Field(str)

presence = None

@property
def mention(self):
return '<@{}>'.format(self.id)
Expand All @@ -21,3 +25,29 @@ def __str__(self):

def on_create(self):
self.client.state.users[self.id] = self


GameType = Enum(
DEFAULT=0,
STREAMING=1,
)

Status = Enum(
'ONLINE',
'IDLE',
'DND',
'INVISIBLE',
'OFFLINE'
)


class Game(Model):
type = Field(GameType)
name = Field(text)
url = Field(text)


class Presence(Model):
user = Field(User)
game = Field(Game)
status = Field(Status)

0 comments on commit fc53496

Please sign in to comment.