pull/13/merge
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
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
import pinhook.plugin
@ -66,11 +67,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:
@ -121,6 +126,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,6 +22,10 @@ def add_plugin(command, func):
cmds.append({'cmd': command, 'func': func})
def add_regex(regex, func):
regexes.append({'regex': regex, 'func': func})
def clear_plugins():
cmds.clear()
@ -31,3 +36,9 @@ def register(command):
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