Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to not specify a matcher in command() and privmsg() #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def repeat(self, target, sender, **kwargs):
else:
self.message(sender, kwargs["msg"])

@hooks.privmsg()
def spamconsole(self, target, sender):
"will say 'i am watching you' when someone says something"
if target.startswith("#"):
self.message(target, "%s: i am watching you" % sender)
else:
self.message(sender, "i am watching you")

@hooks.privmsg("(lol|lmao|rofl(mao)?)")
def stopword(self, target, sender, *args):
"""
Expand Down
24 changes: 8 additions & 16 deletions pyrc/utils/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,30 @@

class command(object):
def __init__(self, matcher=None):
self._matcher = matcher

def __call__(self, func):
# Default the command's name to an exact match of the function's name.
# ^func_name$
matcher = self._matcher
if matcher is None:
matcher = r'^%s$' % func.func_name

# convert matcher to regular expression
matcher = re.compile(matcher)
matcher = r'^%s$' % func.func_name # default to function name
self._matcher = re.compile(matcher)

def __call__(self, func):
@functools.wraps(func)
def wrapped_command(*args, **kwargs):
return func(*args, **kwargs)
wrapped_command._type = "COMMAND"
wrapped_command._matcher = matcher
wrapped_command._matcher = self._matcher
return wrapped_command

class privmsg(object):
def __init__(self, matcher=None):
self._matcher = matcher
if matcher is None:
matcher = r'.+' # default to accepting all input
self._matcher = re.compile(matcher)

def __call__(self, func):
# convert matcher to regular expression
matcher = re.compile(self._matcher)

@functools.wraps(func)
def wrapped_command(*args, **kwargs):
return func(*args, **kwargs)
wrapped_command._type = "PRIVMSG"
wrapped_command._matcher = matcher
wrapped_command._matcher = self._matcher
return wrapped_command

def interval(milliseconds):
Expand Down