Merge cbcecabedd1c2ef99088ffc9f212911614e5de8c into 2d1472c73942abcff301d2a640c313f07e839f3b

This commit is contained in:
importantchoice 2017-11-30 19:59:31 +00:00 committed by GitHub
commit c8b0dbff86
3 changed files with 31 additions and 1 deletions

View File

@ -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

View File

@ -1,5 +1,6 @@
import imp import imp
import os import os
import re
import ssl import ssl
import time import time
import pinhook.plugin import pinhook.plugin
@ -66,11 +67,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:
@ -121,6 +126,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:

View File

@ -1,4 +1,5 @@
cmds = [] cmds = []
regexes = []
class Output: class Output:
def __init__(self, msg_type, msg): def __init__(self, msg_type, msg):
@ -21,6 +22,10 @@ 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 clear_plugins(): def clear_plugins():
cmds.clear() cmds.clear()
@ -31,3 +36,9 @@ def register(command):
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