Added (sub/main)command, removed abot error check
@command is untested.
This commit is contained in:
1
abot.py
1
abot.py
@@ -67,7 +67,6 @@ def on_any_cmd(event):
|
|||||||
words = event.getMessage().lower().split(" ")
|
words = event.getMessage().lower().split(" ")
|
||||||
cmd = words[0][1:]
|
cmd = words[0][1:]
|
||||||
if cmd in ["msg", "m", "t", "pm", "mail", "r", "reply"]:
|
if cmd in ["msg", "m", "t", "pm", "mail", "r", "reply"]:
|
||||||
info(" ".join(words[1:]))
|
|
||||||
check_msg(event, " ".join(words[1:]))
|
check_msg(event, " ".join(words[1:]))
|
||||||
|
|
||||||
load_answers()
|
load_answers()
|
||||||
244
basecommands.py
244
basecommands.py
@@ -1,8 +1,178 @@
|
|||||||
from helpers import *
|
from helpers import *
|
||||||
import inspect, new
|
import inspect, new
|
||||||
|
|
||||||
|
current_subs = []
|
||||||
|
current_main = None
|
||||||
|
|
||||||
to_see_permission = "utils.showpermission" # See cmd permission in help
|
to_see_permission = "utils.showpermission" # See cmd permission in help
|
||||||
|
|
||||||
|
def maincommand(function):
|
||||||
|
global current_main
|
||||||
|
current_main = function
|
||||||
|
return function
|
||||||
|
|
||||||
|
class subcommand():
|
||||||
|
|
||||||
|
def __init__(self, cmd,
|
||||||
|
aliases = [],
|
||||||
|
amin = 0,
|
||||||
|
amax = -1,
|
||||||
|
description = None,
|
||||||
|
usage = "[args...]",
|
||||||
|
senderLimit = -1,
|
||||||
|
helpNoargs = False,
|
||||||
|
helpSubcmd = False):
|
||||||
|
cmd = cmd.lower()
|
||||||
|
self.description = description
|
||||||
|
self.cmd = cmd
|
||||||
|
self.usage = usage
|
||||||
|
self.aliases = aliases
|
||||||
|
self.amin = amin
|
||||||
|
self.amax = amax
|
||||||
|
self.senderLimit = senderLimit
|
||||||
|
self.helpNoargs = helpNoargs
|
||||||
|
self.helpSubcmd = helpSubcmd
|
||||||
|
self.permission = None
|
||||||
|
self.parent = None
|
||||||
|
self.call = None
|
||||||
|
|
||||||
|
def __call__(self, f):
|
||||||
|
global current_subs
|
||||||
|
def call_sub(sender, command, label, args):
|
||||||
|
isPlayer = is_player(sender)
|
||||||
|
if not isSenderValid(senderLimit, isPlayer):
|
||||||
|
return invalidSenderMsg(isPlayer)
|
||||||
|
if not sender.hasPermission(permission):
|
||||||
|
return "&cYou do not have permission to use that command"
|
||||||
|
if ((not args) and helpNoargs) or (helpSubcmd and args and args[0].lower() == "help"):
|
||||||
|
return getHelp(sender)
|
||||||
|
if not checkargs(sender, args, amin, amax):
|
||||||
|
return None
|
||||||
|
return f(sender, command, label, args)
|
||||||
|
|
||||||
|
self.call = call_sub
|
||||||
|
current_subs.append(self)
|
||||||
|
return call_sub
|
||||||
|
|
||||||
|
def getHelp(sender):
|
||||||
|
return helpMsg(sender, "%s %s" % (parent, cmd), description, usage, aliases, permission)
|
||||||
|
|
||||||
|
def setParent(self, parent):
|
||||||
|
self.parent = parent
|
||||||
|
self.permission = "utils.%s.%s" % (parent, self.cmd)
|
||||||
|
self.description = self.description if self.description else "Handles /" + parent
|
||||||
|
|
||||||
|
def isCalled(self, subcmd):
|
||||||
|
alias = self.cmd
|
||||||
|
i = 0
|
||||||
|
while i <= len(self.aliases):
|
||||||
|
if alias == subcmd:
|
||||||
|
return True
|
||||||
|
alias = self.aliases[i]
|
||||||
|
i += 1
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def command(cmd,
|
||||||
|
aliases = [],
|
||||||
|
usage = "[args...]",
|
||||||
|
description = None,
|
||||||
|
senderLimit = -1,
|
||||||
|
amin = 0,
|
||||||
|
amax = -1,
|
||||||
|
helpNoargs = False,
|
||||||
|
helpSubcmd = False):
|
||||||
|
|
||||||
|
cmd = cmd.lower()
|
||||||
|
|
||||||
|
global curent_subs
|
||||||
|
subcommands = []
|
||||||
|
for subcmd in current_subs:
|
||||||
|
subcmd.setParent(cmd)
|
||||||
|
subcommands.append(subcmd)
|
||||||
|
info(subcommands)
|
||||||
|
current_subs = []
|
||||||
|
|
||||||
|
global current_main
|
||||||
|
main_function = current_main
|
||||||
|
current_main = None
|
||||||
|
if main_function == None and len(subcommands) != 0:
|
||||||
|
error("No main function found for " + cmd)
|
||||||
|
|
||||||
|
permission = "utils." + cmd
|
||||||
|
if not description:
|
||||||
|
description = "Handles " + cmd
|
||||||
|
if not usage:
|
||||||
|
usage = "/%s <subcmd>" % cmd
|
||||||
|
|
||||||
|
def getHelp(sender):
|
||||||
|
return helpMsg(sender, cmd, description, usage, aliases, permission)
|
||||||
|
|
||||||
|
def getCalledCmd(cmdName):
|
||||||
|
for subcmd in subcommands:
|
||||||
|
if subcmd.isCalled(cmdName):
|
||||||
|
return subcmd
|
||||||
|
return None
|
||||||
|
|
||||||
|
def decorator(function):
|
||||||
|
|
||||||
|
if len(subcommands) == 0:
|
||||||
|
main_function = function
|
||||||
|
|
||||||
|
@hook.command(cmd, aliases = aliases)
|
||||||
|
def call(sender, command, label, args):
|
||||||
|
message = run(sender, command, label, args)
|
||||||
|
if message:
|
||||||
|
if message == "HELP":
|
||||||
|
message = getHelp(sender)
|
||||||
|
msg(sender, message)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def run(sender, command, label, args):
|
||||||
|
isPlayer = is_player(sender)
|
||||||
|
if not isSenderValid(senderLimit, isPlayer):
|
||||||
|
return invalidSenderMsg(isPlayer)
|
||||||
|
|
||||||
|
if args:
|
||||||
|
arg1 = args[0].lower()
|
||||||
|
if (not args) and helpNoargs:
|
||||||
|
return getHelp(sender)
|
||||||
|
if helpSubcmd and arg1 == "help":
|
||||||
|
if len(args) > 1:
|
||||||
|
sub = getCalledCmd(args[1].lower())
|
||||||
|
return sub.getHelp(sender) if sub and sub.helpSubcmd else "&c%s is not a command!" % args[1]
|
||||||
|
return getHelp(sender)
|
||||||
|
sub = getCalledCmd(args[0].lower())
|
||||||
|
if sub:
|
||||||
|
return sub.call(sender, command, label, args[1:])
|
||||||
|
if not sender.hasPermission(permission):
|
||||||
|
return "&cYou do not have permission to use that command"
|
||||||
|
if not checkargs(sender, args, amin, amax):
|
||||||
|
return None
|
||||||
|
return main_function(sender, command, label, args)
|
||||||
|
return call
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def isSenderValid(senderLimit, isPlayer):
|
||||||
|
return True if senderLimit == -1 else senderLimit != isPlayer
|
||||||
|
|
||||||
|
def invalidSenderMsg(isPlayer):
|
||||||
|
return "&cThat command can only be run from the console" if isPlayer else "&cThat command can only be run by players"
|
||||||
|
|
||||||
|
def helpMsg(sender, cmd, description, usage, aliases, permission):
|
||||||
|
help_msg = "&aInformation about command /%s:\n &9%s" % (cmd, description.replace("\n", "\n "))
|
||||||
|
help_msg += "\n \n&aSyntax: /%s %s" % (cmd, usage)
|
||||||
|
if aliases:
|
||||||
|
help_msg += ("\n&6Aliases: " + "".join([(alias + ", ") for alias in aliases]))[:-2]
|
||||||
|
if sender.hasPermission(to_see_permission):
|
||||||
|
help_msg += "\n&6Required permission: " + permission
|
||||||
|
return help_msg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def simplecommand(cmd,
|
def simplecommand(cmd,
|
||||||
aliases = [],
|
aliases = [],
|
||||||
usage = "[args...]",
|
usage = "[args...]",
|
||||||
@@ -115,64 +285,8 @@ def advancedcommand(cmd,
|
|||||||
error("No function found for /%s %s" % (cmd, sub.cmd))
|
error("No function found for /%s %s" % (cmd, sub.cmd))
|
||||||
return call
|
return call
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
class subcommand():
|
|
||||||
|
|
||||||
def __init__(self, cmd,
|
|
||||||
aliases = [],
|
|
||||||
amin = 0,
|
|
||||||
amax = -1,
|
|
||||||
description = None,
|
|
||||||
usage = "[args...]",
|
|
||||||
senderLimit = -1):
|
|
||||||
cmd = cmd.lower()
|
|
||||||
self.description = description
|
|
||||||
self.cmd = cmd
|
|
||||||
self.usage = usage
|
|
||||||
self.aliases = aliases
|
|
||||||
self.amin = amin
|
|
||||||
self.amax = amax
|
|
||||||
self.senderLimit = senderLimit
|
|
||||||
self.call = None
|
|
||||||
|
|
||||||
def getHelp(sender):
|
|
||||||
return helpMsg(sender, "%s %s" % (parent, cmd), description, usage, aliases, permission)
|
|
||||||
|
|
||||||
def setParent(self, parent):
|
|
||||||
self.parent = parent
|
|
||||||
self.permission = "utils.%s.%s" % (parent, self.cmd)
|
|
||||||
self.description = self.description if self.description else "Handles /" + parent
|
|
||||||
|
|
||||||
def setCalledFunction(self, function):
|
|
||||||
self.call = function
|
|
||||||
|
|
||||||
def isCalled(self, subcmd):
|
|
||||||
alias = self.cmd
|
|
||||||
i = 0
|
|
||||||
while i <= len(self.aliases):
|
|
||||||
if alias == subcmd:
|
|
||||||
return True
|
|
||||||
alias = self.aliases[i]
|
|
||||||
i += 1
|
|
||||||
return False
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def isSenderValid(senderLimit, isPlayer):
|
|
||||||
return True if senderLimit == -1 else senderLimit != isPlayer
|
|
||||||
|
|
||||||
def invalidSenderMsg(isPlayer):
|
|
||||||
return "&cThat command can only be run from the console" if isPlayer else "&cThat command can only be run by players"
|
|
||||||
|
|
||||||
def helpMsg(sender, cmd, description, usage, aliases, permission):
|
|
||||||
help_msg = "&aInformation about command /%s:\n &9%s" % (cmd, description.replace("\n", "\n "))
|
|
||||||
help_msg += "\n \n&aSyntax: /%s %s" % (cmd, usage)
|
|
||||||
if aliases:
|
|
||||||
help_msg += ("\n&6Aliases: " + "".join([(alias + ", ") for alias in aliases]))[:-2]
|
|
||||||
if sender.hasPermission(to_see_permission):
|
|
||||||
help_msg += "\n&6Required permission: " + permission
|
|
||||||
return help_msg
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Use this instead of @hook.command for your command methods. (@simplecommand(things...))
|
Use this instead of @hook.command for your command methods. (@simplecommand(things...))
|
||||||
@@ -215,3 +329,21 @@ def on_test(sender, command, label, args):
|
|||||||
return None
|
return None
|
||||||
return "&cThat player could not be found"
|
return "&cThat player could not be found"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
Example @command:
|
||||||
|
|
||||||
|
@command("forcefield")
|
||||||
|
def forcefield_command():
|
||||||
|
|
||||||
|
@subcommand("add")
|
||||||
|
def func(sender, command, label, args):
|
||||||
|
...add to whitelist
|
||||||
|
|
||||||
|
@maincommand
|
||||||
|
def other(sender, command, label, args):
|
||||||
|
...Execute command if no sub commands were found
|
||||||
|
|
||||||
|
@command("simplestuff")
|
||||||
|
def simplestuff_command(sender, command, label, args):
|
||||||
|
msg(sender, "hi, I do not use subcommands!")
|
||||||
|
|||||||
Reference in New Issue
Block a user