From a8f3e87f8c98c3c5d67dd009209b54dc86e99daa Mon Sep 17 00:00:00 2001 From: Andrei Date: Tue, 27 Sep 2016 20:37:33 -0500 Subject: [PATCH] wip guild events in state tracking --- disco/state.py | 49 ++++++++++++++++++++++++++++++++++++++++++-- disco/types/guild.py | 1 + 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/disco/state.py b/disco/state.py index 532afd4..0833892 100644 --- a/disco/state.py +++ b/disco/state.py @@ -38,8 +38,15 @@ class State(object): self.client.events.on('GuildUpdate', self.on_guild_update) self.client.events.on('GuildDelete', self.on_guild_delete) - # TODO: guild members - # TODO: guild roles + # Guild members + self.client.events.on('GuildMemberAdd', self.on_guild_member_add) + self.client.events.on('GuildMemberRemove', self.on_guild_member_remove) + self.client.events.on('GuildMemberUpdate', self.on_guild_member_update) + + # Guild roles + self.client.events.on('GuildRoleCreate', self.on_guild_role_create) + self.client.events.on('GuildRoleUpdate', self.on_guild_role_update) + self.client.events.on('GuildROleDelete', self.on_guild_role_delete) # Channels self.client.events.on('ChannelCreate', self.on_channel_create) @@ -123,3 +130,41 @@ class State(object): del guild.voice_states[event.state.session_id] elif event.state.channel_id: guild.voice_states[event.state.session_id] = event.state + + def on_guild_member_add(self, event): + if event.member.user.id not in self.users: + self.users[event.member.user.id] = event.member.user + else: + event.member.user = self.users[event.member.user.id] + + if event.member.guild.id not in self.guilds: + return + + self.guilds[event.member.guild.id].members[event.member.id] = event.member + + def on_guild_member_update(self, event): + if event.member.guild.id not in self.guilds: + return + + # Ensure the reference is correct + assert(event.member.user.id in self.users) + event.member.user = self.users[event.member.user.id] + self.guilds[event.member.guild.id].members[event.member.id] = event.member + + def on_guild_member_remove(self, event): + if event.member.guild.id not in self.guilds: + return + + if event.member.id not in self.guilds[event.member.guild.id].members: + return + + del self.guilds[event.member.guild.id].members[event.member.id] + + def on_guild_role_create(self, event): + pass + + def on_guild_role_update(self, event): + pass + + def on_guild_role_delete(self, event): + pass diff --git a/disco/types/guild.py b/disco/types/guild.py index bbb4570..f8e1774 100644 --- a/disco/types/guild.py +++ b/disco/types/guild.py @@ -29,6 +29,7 @@ class Role(BaseType): class GuildMember(BaseType): user = skema.ModelType(User) + guild = skema.ModelType(Guild) mute = skema.BooleanType() deaf = skema.BooleanType() joined_at = PreHookType(lambda k: k[:-6], skema.DateTimeType())