Added register_regex decorator

The register_regex decorator lets the user define a custom regular
expression. If this regex matches against any incoming message, the plugin
function is called.
pull/13/head
importantchoice 2017-11-30 10:08:31 +01:00
parent d4f0edda00
commit 172c7b439c
3 changed files with 31 additions and 1 deletions

View File

@ -31,6 +31,8 @@ Optional arguments are:
### Creating plugins
In your chosen plugins directory ("plugins" by default) make a python file with a function. You can use the `@pinhook.plugin.register` decorator to tell the bot the command to activate the function.
Use the `@pinhook.plugin.register_regex` to activate your functions for all messages, that match a regular expression, you define.
The function will need to be structured as such:
```python
import pinhook.plugin

View File

@ -1,5 +1,6 @@
import imp
import os
import re
import ssl
import time
@ -63,11 +64,15 @@ class Bot(irc.bot.SingleServerIRCBot):
plugins.append(p)
except Exception as e:
print(e)
# gather all commands
# gather all commands and regexes
self.cmds = {}
self.regexes = []
for plugin in plugins:
for cmd in plugin.pinhook.plugin.cmds:
self.cmds[cmd['cmd']] = cmd['func']
for regex in plugin.pinhook.plugin.regexes:
self.regexes.append(regex)
def on_welcome(self, c, e):
if self.ns_pass:
@ -118,6 +123,18 @@ class Bot(irc.bot.SingleServerIRCBot):
))
except Exception as e:
print(e)
# check if text matches agains any defined regex
for regex in self.regexes:
match = re.match(regex['regex'], text)
if match:
output = regex['func'](Message(
channel=chan,
cmd=regex['regex'],
nick=nick,
arg=text,
botnick=self.bot_nick,
ops=self.ops
))
if output:
for msg in output.msg:

View File

@ -1,4 +1,5 @@
cmds = []
regexes = []
class Output:
def __init__(self, msg_type, msg):
@ -21,9 +22,19 @@ def add_plugin(command, func):
cmds.append({'cmd': command, 'func': func})
def add_regex(regex, func):
regexes.append({'regex': regex, 'func': func})
def register(command):
def register_for_command(func):
add_plugin(command, func)
return func
return register_for_command
def register_regex(regex):
def register_for_regex(func):
add_regex(regex, func)
return func
return register_for_regex