Create ops only commands (#50)
* Add support for ops commands. Fixes #48 * Update readme. remove printspull/53/head^2
parent
7c7d92948a
commit
bf984bacd0
11
README.md
11
README.md
|
@ -90,6 +90,17 @@ It also contains the following IRC functions:
|
||||||
* `action`: same as privmsg, but does a CTCP action. (i.e., `/me does a thing`)
|
* `action`: same as privmsg, but does a CTCP action. (i.e., `/me does a thing`)
|
||||||
* `notice`: send a notice
|
* `notice`: send a notice
|
||||||
|
|
||||||
|
You can optionally use the `@pinhook.plugin.ops` decorator to denote that a command should only be executable by a bot op.
|
||||||
|
* If you specify the optional second argument, it will be displayed when a non-op attempts to execute the command
|
||||||
|
|
||||||
|
The function will need to be structured as such:
|
||||||
|
```python
|
||||||
|
@pinhook.plugin.register('!test')
|
||||||
|
@pinhook.plugin.ops('!test', 'Only ops can run this command!')
|
||||||
|
def test_plugin(msg):
|
||||||
|
return pinhook.plugin.message('This was run by an op!')
|
||||||
|
```
|
||||||
|
|
||||||
**OR**
|
**OR**
|
||||||
|
|
||||||
The plugin function can return one of the following in order to give a response to the command:
|
The plugin function can return one of the following in order to give a response to the command:
|
||||||
|
|
|
@ -134,8 +134,8 @@ class Bot(irc.bot.SingleServerIRCBot):
|
||||||
def on_action(self, c, e):
|
def on_action(self, c, e):
|
||||||
self.process_event(c, e)
|
self.process_event(c, e)
|
||||||
|
|
||||||
def call_help(self):
|
def call_help(self, op):
|
||||||
helplist = sorted([i for i in pinhook.plugin.cmds])
|
helplist = sorted([i for i in pinhook.plugin.cmds if op or not ('ops' in pinhook.plugin.cmds[i] and pinhook.plugin.cmds[i]['ops'])])
|
||||||
msg = ', '.join(helplist)
|
msg = ', '.join(helplist)
|
||||||
return self.output_message('Available commands: {}'.format(msg))
|
return self.output_message('Available commands: {}'.format(msg))
|
||||||
|
|
||||||
|
@ -154,7 +154,7 @@ class Bot(irc.bot.SingleServerIRCBot):
|
||||||
c.quit("See y'all later!")
|
c.quit("See y'all later!")
|
||||||
quit()
|
quit()
|
||||||
elif cmd == '!help':
|
elif cmd == '!help':
|
||||||
output = self.call_help()
|
output = self.call_help(op)
|
||||||
elif cmd == '!reload' and op:
|
elif cmd == '!reload' and op:
|
||||||
self.logger.info('reloading plugins per request of {}'.format(nick))
|
self.logger.info('reloading plugins per request of {}'.format(nick))
|
||||||
self.load_plugins()
|
self.load_plugins()
|
||||||
|
@ -165,19 +165,23 @@ class Bot(irc.bot.SingleServerIRCBot):
|
||||||
output = None
|
output = None
|
||||||
if cmd in pinhook.plugin.cmds:
|
if cmd in pinhook.plugin.cmds:
|
||||||
try:
|
try:
|
||||||
output = pinhook.plugin.cmds[cmd]['run'](self.Message(
|
if 'ops' in pinhook.plugin.cmds[cmd] and nick not in self.ops:
|
||||||
channel=chan,
|
if pinhook.plugin.cmds[cmd]['ops_msg']:
|
||||||
cmd=cmd,
|
output = self.output_message(pinhook.plugin.cmds[cmd]['ops_msg'])
|
||||||
nick_list=nick_list,
|
else:
|
||||||
nick=nick,
|
output = pinhook.plugin.cmds[cmd]['run'](self.Message(
|
||||||
arg=arg,
|
channel=chan,
|
||||||
privmsg=privmsg,
|
cmd=cmd,
|
||||||
action=action,
|
nick_list=nick_list,
|
||||||
notice=notice,
|
nick=nick,
|
||||||
botnick=self.bot_nick,
|
arg=arg,
|
||||||
ops=self.ops,
|
privmsg=privmsg,
|
||||||
logger=self.logger
|
action=action,
|
||||||
))
|
notice=notice,
|
||||||
|
botnick=self.bot_nick,
|
||||||
|
ops=self.ops,
|
||||||
|
logger=self.logger
|
||||||
|
))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.exception('issue with command {}'.format(cmd))
|
self.logger.exception('issue with command {}'.format(cmd))
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
|
||||||
cmds = {}
|
cmds = {}
|
||||||
|
@ -31,10 +32,20 @@ def message(msg):
|
||||||
|
|
||||||
|
|
||||||
def _add_plugin(command, help_text, func):
|
def _add_plugin(command, help_text, func):
|
||||||
cmds[command] = {
|
if command not in cmds:
|
||||||
|
cmds[command] = {}
|
||||||
|
cmds[command].update({
|
||||||
'run': func,
|
'run': func,
|
||||||
'help': help_text
|
'help': help_text
|
||||||
}
|
})
|
||||||
|
|
||||||
|
def _ops_plugin(command, ops_msg, func):
|
||||||
|
if command not in cmds:
|
||||||
|
cmds[command] = {}
|
||||||
|
cmds[command].update({
|
||||||
|
'ops': True,
|
||||||
|
'ops_msg': ops_msg,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def _add_listener(name, func):
|
def _add_listener(name, func):
|
||||||
|
@ -47,6 +58,7 @@ def clear_plugins():
|
||||||
|
|
||||||
|
|
||||||
def register(command, help_text=None):
|
def register(command, help_text=None):
|
||||||
|
@wraps(command)
|
||||||
def register_for_command(func):
|
def register_for_command(func):
|
||||||
_add_plugin(command, help_text, func)
|
_add_plugin(command, help_text, func)
|
||||||
return func
|
return func
|
||||||
|
@ -58,3 +70,10 @@ def listener(name):
|
||||||
_add_listener(name, func)
|
_add_listener(name, func)
|
||||||
return func
|
return func
|
||||||
return register_as_listener
|
return register_as_listener
|
||||||
|
|
||||||
|
def ops(command, msg=None):
|
||||||
|
@wraps(command)
|
||||||
|
def register_ops_command(func):
|
||||||
|
_ops_plugin(command, msg, func)
|
||||||
|
return func
|
||||||
|
return register_ops_command
|
||||||
|
|
Loading…
Reference in New Issue