def on_cmd(full_message, user, value): if (not hasattr(user, 'menu')) or user.menu is None: return False # Let cmd pass through menu_text, options, exit_callback, timeout = user.menu if value == 'again': tell_menu(user, user.menu) return True elif value == 'exit': callback = exit_callback elif len(options) and options[0] == 'prompt': callback = options[1] else: try: n = int(value) except ValueError: tell(user, "Not a number: %s" % value) return True if not 0 < n <= len(options): tell(user, "%d not a valid option" % n) return True callback = options[n-1][1] new_menu = callback(user, value) user.menu = None schedule.clear(('menu', user)) if new_menu is not None: display(user, *new_menu) return True
def uncaught_command(message, user, command): if command in MC_COMMANDS or user.username in ops(): return False # Do not drop packet if user.username in ops() and command in ('ban','pardon'): tell(user, 'Please use the whitetree tools for bans: /whitetree ops') return True tell(user, "Bad command: %s" % command) return True # Drop packet
def uncaught_command(message, user, command): for mc_cmd in MC_COMMANDS: if command.startswith(mc_cmd): return False # Do not drop packet if user.username in ops(): tell(user, "Warning: I don't recognise %s" % message) return False tell(user, "Bad command: %s" % command) return True # Drop packet
def on_cmd(message, user): now = time.time() user_time = users.get(user.username, 0) time_left = INTERVAL - (now - user_time) if time_left <= 0: tell(user, "slimeballs: Dispensing product.") server_cmd('/give %s 341 2') # give 2 slimeballs users[user.username] = now else: hours = int(time_left % 3600) mins = int(time_left % 60 - 60*hours) secs = int(time_left - 3600*hours - 60*mins) tell(user, "slimeballs: You cannot slimeballs again for another %02d:%02d:%02d" % (hours, mins, secs))
def tell(user, message, delay=0, lock=None, prefix=''): """Send a server message to user. Note: Splits multiline messages. See prefix, below. Optional args: delay: Wait delay seconds before sending message. Useful mainly with lock. See below. lock: Until message sent (see delay) allow no new messages with the same user and lock value. Generally speaking, expected to be a string. But it doesn't really matter. prefix: Add given prefix to every line sent, eg '<server>: '. Returns bool of whether message was sent (see lock). Note: Apart from delay and lock args, this function acts like helpers.tell() """ message = unicode(message) tell_fn = lambda: helpers.tell(user, message, prefix=prefix) if delay: try: schedule.check(lock) except KeyError: schedule.register(delay, tell_fn, key=lock) else: return False else: tell_fn() return True
def tell_menu(user, menu): menu_text, options, exit_callback, timeout = menu tell(user, menu_text) n = 0 if len(options) and options[0] == 'prompt': tell(user, "Please enter :your response") else: for option, callback in options: n += 1 tell(user, ":%d %s" % (n, option)) tell(user, "")
def on_command(message, user, command): parts = command.split(' ', 1) if len(parts) == 2: verb, value = parts else: verb = parts[0] if verb == 'help': tell(user, HELP) elif verb == 'colors': for c in colors: prefs = users.get(user.username, defaults) tell(user, 'color: %s"%s"%s' % (color(c), c, prefs['all'])) elif verb in defaults: if value in colors: prefs = users.get(user.username, defaults.copy()) prefs[verb] = color(value) users[user.username] = prefs else: tell(user, 'color: That is not a valid color!') else: tell(user, 'color: Bad command. Try "/color help"')
def on_packet(packet, user, to_server): if packet.name() == 'Login request' and to_server: tell(user, WELCOME) return packet
def on_start(): cmd.register('/help', lambda msg, user: tell(user, HELP)) cmd.register('/firsttime', lambda msg, user: tell(user, FIRSTTIME)) cmd.register('/rules', lambda msg, user: tell(user, RULES)) cmd.register('/info', lambda msg, user: tell(user, INFO)) cmd.register('/tech', lambda msg, user: tell(user, TECH))
def on_start(): cmds.register(":(.*)", on_cmd) cmds.register("/menus", lambda msg, user: tell(user, MENU_HELP))
def no_command(message, user): tell(user, 'Modify user colors! Type "/color help".')
def exit_menu(user, value): tell(user, "Test menu closed")
def start_menu(user): try: menus.display(user, *ROOT_MENU) except menus.MenuLockError: tell(user, "Menu already open")