Browse Source

Better listener support, another fix to VoiceStateUpdate

pull/9/head
Andrei 9 years ago
parent
commit
cc8fd6db84
  1. 22
      disco/bot/plugin.py
  2. 8
      disco/state.py
  3. 2
      requirements.txt

22
disco/bot/plugin.py

@ -41,27 +41,27 @@ class PluginDeco(object):
return deco
@classmethod
def listen(cls, event_name, priority=None):
def listen(cls, *args, **kwargs):
"""
Binds the function to listen for a given event name
"""
return cls.add_meta_deco({
'type': 'listener',
'what': 'event',
'desc': event_name,
'priority': priority
'args': args,
'kwargs': kwargs,
})
@classmethod
def listen_packet(cls, op, priority=None):
def listen_packet(cls, *args, **kwargs):
"""
Binds the function to listen for a given gateway op code
"""
return cls.add_meta_deco({
'type': 'listener',
'what': 'packet',
'desc': op,
'priority': priority,
'args': args,
'kwargs': kwargs,
})
@classmethod
@ -187,7 +187,7 @@ class Plugin(LoggingClass, PluginDeco):
def bind_meta(self, member, meta):
if meta['type'] == 'listener':
self.register_listener(member, meta['what'], meta['desc'], meta['priority'])
self.register_listener(member, meta['what'], *meta['args'], **meta['kwargs'])
elif meta['type'] == 'command':
meta['kwargs']['update'] = True
self.register_command(member, *meta['args'], **meta['kwargs'])
@ -248,7 +248,7 @@ class Plugin(LoggingClass, PluginDeco):
return True
def register_listener(self, func, what, desc, priority):
def register_listener(self, func, what, desc, priority=Priority.NONE, conditional=None):
"""
Registers a listener
@ -265,12 +265,10 @@ class Plugin(LoggingClass, PluginDeco):
"""
func = functools.partial(self._dispatch, 'listener', func)
priority = priority or Priority.NONE
if what == 'event':
li = self.bot.client.events.on(desc, func, priority=priority)
li = self.bot.client.events.on(desc, func, priority=priority, conditional=conditional)
elif what == 'packet':
li = self.bot.client.packets.on(desc, func, priority=priority)
li = self.bot.client.packets.on(desc, func, priority=priority, conditional=conditional)
else:
raise Exception('Invalid listener what: {}'.format(what))

8
disco/state.py

@ -185,6 +185,9 @@ class State(object):
for member in six.itervalues(event.guild.members):
self.users[member.user.id] = member.user
for voice_state in six.itervalues(event.guild.voice_states):
self.voice_states[voice_state.session_id] = voice_state
if self.config.sync_guild_members:
event.guild.sync()
@ -225,8 +228,13 @@ class State(object):
guild.voice_states[event.state.session_id].update(event.state)
else:
del guild.voice_states[event.state.session_id]
# Prevent a weird race where events come in before the guild_create (I think...)
if event.state.session_id in self.voice_states:
del self.voice_states[event.state.session_id]
elif event.state.channel_id:
guild.voice_states[event.state.session_id] = event.state
self.voice_states[event.state.session_id] = event.state
def on_guild_member_add(self, event):
if event.member.user.id not in self.users:

2
requirements.txt

@ -1,5 +1,5 @@
gevent==1.1.2
holster==1.0.6
holster==1.0.8
inflection==0.3.1
requests==2.11.1
six==1.10.0

Loading…
Cancel
Save