summaryrefslogtreecommitdiff
path: root/basecommands.py
blob: 89e08aa3d8eddba0e3e93d542c52515772cf87fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from helpers import *

to_see_permission = "utils.showpermission" # See cmd permission in help


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,
                aliases     = [],
                usage       = "[args...]",
                description = None,
                senderLimit = -1,
                amin        = 0,
                amax        = -1,
                helpNoargs  = False,
                helpSubcmd  = False):
    cmd = cmd.lower()
    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 decorator(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 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

            try:
                return function(sender, command, label, args)
            except CommandException, e:
                return e.message
            except Exception, e:
                error(e.message, trace())
                return "&cAn internal error occurred while attempting to perform this command"

        return call
    return decorator


class CommandException(Exception):
    pass


class Validate():
    @staticmethod
    def notNone(obj, msg):
        if obj == None:
            raise CommandException(msg)

    @staticmethod
    def isPlayer(sender):
        if not is_player(sender):
            raise CommandException("&cThat command can only be run by players")

    @staticmethod
    def isConsole(sender):
        if is_player(sender):
            raise CommandException("&cThat command can only be run from the console")

    @staticmethod
    def isAuthorized(sender, permission, msg = "that command"):
        if not sender.hasPermission(permission):
            raise CommandException("&cYou do not have permission to use " + msg)

    @staticmethod
    def isTrue(obj, msg):
        if obj != True:
            raise CommandException(msg)

    @staticmethod
    def checkArgs(sender, args, amin, amax):
        if not checkargs(sender, args, amin, amax):
            raise CommandException("")