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

Commit

Permalink
Add plugin schedules (repeating functions) and Message.member prop
Browse files Browse the repository at this point in the history
  • Loading branch information
b1naryth1ef committed Oct 7, 2016
1 parent c11c563 commit f5aa3f5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions disco/bot/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import functools
import gevent

from holster.emitter import Priority

Expand Down Expand Up @@ -83,6 +84,16 @@ def post_listener(cls):
'type': 'post_listener',
})

@classmethod
def schedule(cls, interval=60):
"""
Runs a function repeatedly, waiting for a specified interval
"""
return cls.add_meta_deco({
'type': 'schedule',
'interval': interval,
})


class Plugin(LoggingClass, PluginDeco):
"""
Expand Down Expand Up @@ -115,6 +126,7 @@ def __init__(self, bot, config):

self.listeners = []
self.commands = {}
self.schedules = {}

self._pre = {'command': [], 'listener': []}
self._post = {'command': [], 'listener': []}
Expand All @@ -126,6 +138,8 @@ def __init__(self, bot, config):
self.register_listener(member, meta['event_name'], meta['priority'])
elif meta['type'] == 'command':
self.register_command(member, *meta['args'], **meta['kwargs'])
elif meta['type'] == 'schedule':
self.register_schedule(member, meta['interval'])
elif meta['type'].startswith('pre_') or meta['type'].startswith('post_'):
when, typ = meta['type'].split('_', 1)
self.register_trigger(typ, when, member)
Expand Down Expand Up @@ -193,6 +207,25 @@ def register_command(self, func, *args, **kwargs):
wrapped = functools.partial(self._dispatch, 'command', func)
self.commands[func.__name__] = Command(self, wrapped, *args, **kwargs)

def register_schedule(self, func, interval):
"""
Registers a function to be called repeatedly, waiting for an interval
duration.
Args
----
func : function
The function to be registered.
interval : int
Interval (in seconds) to repeat the function on.
"""
def repeat():
while True:
func()
gevent.sleep(interval)

self.schedules[func.__name__] = gevent.spawn(repeat)

def destroy(self):
"""
Destroys the plugin (removing all listeners)
Expand Down
10 changes: 10 additions & 0 deletions disco/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ def guild(self):
"""
return self.channel.guild

@cached_property
def member(self):
"""
Returns
-------
:class:`disco.types.guild.GuildMember`
The guild member (if applicable) that sent this message.
"""
return self.channel.guild.get_member(self.author)

@cached_property
def channel(self):
"""
Expand Down

0 comments on commit f5aa3f5

Please sign in to comment.