diff --git a/botcmd.py b/botcmd.py index 1b9d52d..66da556 100644 --- a/botcmd.py +++ b/botcmd.py @@ -64,14 +64,176 @@ def send_event(event): game_channel.send(event) -def parse_command(message, nick): +def parse_command(message, nick, irc): + def send(m): + global irc_chan + irc.bot_response(irc_chan, m) + + def args(num, index = 1): + nonlocal message + if type(num) == int: + num = [num] + + if len(message) - index not in num: + command = ' '.join(message[:index]) + + if len(num) == 1: + if num[0] == 1: + send('%s needs 1 argument' % command) + else: + send('%s needs %i arguments' % (command, num[0])) + else: + send('%s needs either %s arguments' % (command, ' or '.join(num))) + return None + + return message[index:] + events = gameloop.events - if message == '!start': - send_event((events.start, nick)) + message = message.split() + if len(message) == 0: return + c = message[0] - elif message == '!kill': - send_event((events.kill,)) + if c == '!status': + if args(0) is not None: + send_event((events.status,)) + + elif c == '!start': + if args(0) is not None: + send_event((events.start, nick)) + + elif c == '!ready': + if args(0) is not None: + send_event((events.ready, nick)) + + elif c == '!unready': + if args(0) is not None: + send_event((events.unready, nick)) + + elif c == '!kill': + if args(0) is not None: + send_event((events.kill,)) + + elif c == '!join': + if args(0) is not None: + send_event((events.join, nick)) + + elif c == '!leave': + if args(0) is not None: + send_event((events.leave, nick)) + + elif c == '!players': + if args(0) is not None: + send_event((events.players,)) + + elif c == '!deck': + if len(message) < 2: + send('Subcommands: !deck add | remove | list') + return + + subc = message[1] + if subc == 'add': + arg = args(1, 2) + if arg is not None: + code, = arg + if code == 'random': + send_event((events.deck_add_random,)) + else: + send_event((events.deck_add, code)) + + elif subc == 'remove': + arg = args(1, 2) + if arg is not None: + code, = arg + send_event((events.deck_remove, code)) + + elif subc == 'list': + if args(0, 2) is not None: + send_event((events.deck_list,)) + + else: + send('Subcommands: !deck add | remove | list') + + elif c == '!limit': + arg = args([0, 1, 2]) + if arg is None: return + + if len(arg) == 0: + send_event((events.limit,)) + + else: + num = arg[0] + if not num.isdecimal(): + send('Usage: !limit [ []]') + return + num = int(num) + + if len(arg) == 2: + limit_type = arg[1] + + if limit_type == 'p' or limit_type == 'points': + send_event((events.limit, gameloop.limit_types.points, num)) + elif limit_type == 'r' or limit_type == 'rounds': + send_event((events.limit, gameloop.limit_types.rounds, num)) + else: + send('Allowed limit types: p(oints), r(ounds)') + + else: + send_event((events.limit, gameloop.limit_types.points, num)) + + elif c == '!card' or all(i.isdecimal() for i in message): + if c == '!card': + arg = message[1:] + else: + arg = message + + if not all(i.isdecimal() for i in arg): + send('Usage: [!card] ...') + return + + choices = [int(i) for i in arg] + send_event((events.card, nick, choices)) + + elif c == '!cards': + if args(0) is not None: + send_event((events.cards, nick)) + + elif c == '!help': + arg = args([0, 1, 2]) + if arg is not None: + if len(arg) > 0: + if arg[0][0] == '!': + arg[0] = arg[0][1:] + + if len(arg) == 0: + send('!status !start !ready !unready !kill !join !leave !players !deck !limit !card !cards') + + elif len(arg) == 1: + if arg[0] in ('status', 'start', 'ready', 'unready', 'kill', 'join', 'leave', 'players', 'cards'): + send('Usage: !%s' % (arg[0])) + elif arg[0] == 'card': + send('Usage: [!card] ...') + elif arg[0] == 'deck': + send('Subcommands: !deck add | remove | list') + elif arg[0] == 'limit': + send('Usage: !limit [ []]') + else: + send('No such command !%s' % (arg[0])) + elif len(arg) == 2: + if arg[0] == 'deck': + if arg[1] == 'add': + send('Usage: !deck add | random') + elif arg[1] == 'remove': + send('Usage: !deck remove ') + elif arg[1] == 'list': + send('Usage: !deck list') + else: + send('No such subcommand !%s %s' % (arg[0], arg[1])) + else: + send('No such subcommand !%s %s' % (arg[0], arg[1])) + + else: + send('Uh, how did we get %i args?' % len(arg)) # initialize(*, config) # Called to initialize the IRC bot @@ -117,7 +279,7 @@ def handle_message(*, prefix, message, nick, channel, irc): global irc_chan if channel == irc_chan: - parse_command(message.decode(), nick.decode()) + parse_command(message.decode(), nick.decode(), irc) # handle_nonmessage(*, prefix, command, arguments, irc) # Called for all other commands than PINGs and PRIVMSGs. @@ -127,4 +289,15 @@ def handle_message(*, prefix, message, nick, channel, irc): # irc is the IRC API object # All strings are bytestrings def handle_nonmessage(*, prefix, command, arguments, irc): - ... + if command == b'NICK': + old = prefix.split(b'!')[0].decode() + new = arguments[0].decode() + send_event((gameloop.events.nick_change, old, new)) + + elif command == b'PART' or command == b'QUIT': + nick = prefix.split(b'!')[0].decode() + send_event((gameloop.events.leave, nick)) + + elif command == b'KICK': + nick = arguments[1].decode() + send_event((gameloop.events.leave, nick))