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
parent
d4f0edda00
commit
172c7b439c
|
@ -31,6 +31,8 @@ Optional arguments are:
|
||||||
### Creating plugins
|
### 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.
|
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:
|
The function will need to be structured as such:
|
||||||
```python
|
```python
|
||||||
import pinhook.plugin
|
import pinhook.plugin
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import imp
|
import imp
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import ssl
|
import ssl
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
@ -63,11 +64,15 @@ class Bot(irc.bot.SingleServerIRCBot):
|
||||||
plugins.append(p)
|
plugins.append(p)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
# gather all commands
|
# gather all commands and regexes
|
||||||
self.cmds = {}
|
self.cmds = {}
|
||||||
|
self.regexes = []
|
||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
for cmd in plugin.pinhook.plugin.cmds:
|
for cmd in plugin.pinhook.plugin.cmds:
|
||||||
self.cmds[cmd['cmd']] = cmd['func']
|
self.cmds[cmd['cmd']] = cmd['func']
|
||||||
|
for regex in plugin.pinhook.plugin.regexes:
|
||||||
|
self.regexes.append(regex)
|
||||||
|
|
||||||
|
|
||||||
def on_welcome(self, c, e):
|
def on_welcome(self, c, e):
|
||||||
if self.ns_pass:
|
if self.ns_pass:
|
||||||
|
@ -118,6 +123,18 @@ class Bot(irc.bot.SingleServerIRCBot):
|
||||||
))
|
))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(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:
|
if output:
|
||||||
for msg in output.msg:
|
for msg in output.msg:
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
cmds = []
|
cmds = []
|
||||||
|
regexes = []
|
||||||
|
|
||||||
class Output:
|
class Output:
|
||||||
def __init__(self, msg_type, msg):
|
def __init__(self, msg_type, msg):
|
||||||
|
@ -21,9 +22,19 @@ def add_plugin(command, func):
|
||||||
cmds.append({'cmd': command, 'func': func})
|
cmds.append({'cmd': command, 'func': func})
|
||||||
|
|
||||||
|
|
||||||
|
def add_regex(regex, func):
|
||||||
|
regexes.append({'regex': regex, 'func': func})
|
||||||
|
|
||||||
|
|
||||||
def register(command):
|
def register(command):
|
||||||
def register_for_command(func):
|
def register_for_command(func):
|
||||||
add_plugin(command, func)
|
add_plugin(command, func)
|
||||||
return func
|
return func
|
||||||
return register_for_command
|
return register_for_command
|
||||||
|
|
||||||
|
|
||||||
|
def register_regex(regex):
|
||||||
|
def register_for_regex(func):
|
||||||
|
add_regex(regex, func)
|
||||||
|
return func
|
||||||
|
return register_for_regex
|
||||||
|
|
Loading…
Reference in New Issue