Skip to content

Commit

Permalink
Merge pull request #204 from T-101/feature/admin-only-commands
Browse files Browse the repository at this point in the history
Feature/admin only commands
  • Loading branch information
lepinkainen committed May 17, 2017
2 parents 119bbe7 + a2fe199 commit f3ec24a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pyfibot/botcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from types import FunctionType

import re
import string
import logging
from util import pyfiurl
Expand Down Expand Up @@ -161,10 +162,11 @@ def command_channels(self, user, channel, args):
def command_help(self, user, channel, cmnd):
"""Get help on all commands or a specific one. Usage: help [<command>]"""

commands = []
commands, admin_commands = [], []
for module, env in self.factory.ns.items():
myglobals, mylocals = env
commands += [(c.replace("command_", ""), ref) for c, ref in mylocals.items() if c.startswith("command_%s" % cmnd)]
admin_commands += [(c.replace("admin_", ""), ref) for c, ref in mylocals.items() if c.startswith("admin_%s" % cmnd)]
# Help for a specific command
if len(cmnd) > 0:
for cname, ref in commands:
Expand All @@ -175,7 +177,10 @@ def command_help(self, user, channel, cmnd):
# Generic help
else:
commandlist = ", ".join([c for c, ref in commands])
admin_commandlist = ", ".join([c for c, ref in admin_commands])
self.say(channel, "Available commands: %s" % commandlist)
if self.factory.isAdmin(user):
self.say(channel, "Available admin commands: %s" % admin_commandlist)


class PyFiBot(irc.IRCClient, CoreCommands):
Expand Down Expand Up @@ -397,9 +402,11 @@ def _command(self, user, channel, cmnd):
for module, env in self.factory.ns.items():
myglobals, mylocals = env
# find all matching command functions
commands = [(c, ref) for c, ref in mylocals.items() if c == "command_%s" % cmnd]
commands = [(c, ref) for c, ref in mylocals.items() if re.match(r'(command|admin)_%s' % cmnd, c)]

for cname, command in commands:
if not self.factory.isAdmin(user) and cname.startswith('admin'):
continue
log.info("module command %s called by %s (%s) on %s" % (cname, user, self.factory.isAdmin(user), channel))
# Defer commands to threads
d = threads.deferToThread(command, self, user, channel, self.factory.to_unicode(args.strip()))
Expand Down
10 changes: 10 additions & 0 deletions pyfibot/modules/module_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
Example of admin only commands
"""

from __future__ import unicode_literals, print_function, division


def admin_allow(bot, user, channel, args):
bot.say(channel, "Example of a command only allowed to an admin")

0 comments on commit f3ec24a

Please sign in to comment.