From df2648f7a652e234ea0c25a22d4ac4c0e82f9d7b Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Thu, 15 Sep 2016 09:57:08 +0100 Subject: [PATCH] Command modules. --- bot.py | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/bot.py b/bot.py index 509b9ae..121323a 100755 --- a/bot.py +++ b/bot.py @@ -16,12 +16,41 @@ log = logging.getLogger('discord') def wait_for_prompt(p): p.expect('EDI> ') +@asyncio.coroutine +def do_edi(message, command, raw, p): + if command == 'close_to' and not '-m' in raw: + raw = 'close_to' + log.info(raw) + yield from client.send_typing(message.channel) + p.sendline(raw) + wait_for_prompt(p) + stdout = p.before + if stdout: + log.info(stdout) + yield from client.send_message(message.channel, '{}```{}```'.format('{} '.format(message.author.mention) if not message.channel.is_private else '', stdout)) + else: + yield from client.send_message(message.channel, 'Sorry!') + p = pexpect.spawnu('./edi.py') wait_for_prompt(p) -stuff = { 'edi': p, 'commands': ['coords', 'close_to', 'distance', 'edts', 'fuel_usage', 'galmath', 'help', 'raikogram'] } + +modules = { 'edi': { 'fn': do_edi, 'args': [p], 'commands': ['coords', 'close_to', 'distance', 'edts', 'fuel_usage', 'galmath', 'help', 'raikogram'] } } client = discord.Client() +@asyncio.coroutine +def process_module(message, command, raw): + for module, params in modules.items(): + if command in params['commands']: + log.info('Recognised command "{}" from "{}" module'.format(command, module)) + fn = params['fn'] + args = params['args'] + yield from fn(message, command, raw, *args) + return + log.debug('Unrecognised command!') + if random.random() > 0.8: + yield from client.send_message(message.channel, 'meow!') + @client.event @asyncio.coroutine def on_ready(): @@ -42,24 +71,6 @@ def on_message(message): if m is None: return command = m.group(1) - if command not in stuff['commands']: - log.debug('Unrecognised command!') - if random.random() > 0.8: - yield from client.send_message(message.channel, 'meow!') - return - - if command == 'close_to' and not '-m' in raw: - raw = 'close_to' - p = stuff['edi'] - log.info(raw) - yield from client.send_typing(message.channel) - p.sendline(raw) - wait_for_prompt(p) - stdout = p.before - if stdout: - log.info(stdout) - yield from client.send_message(message.channel, '{}```{}```'.format('{} '.format(message.author.mention) if not message.channel.is_private else '', stdout)) - else: - yield from client.send_message(message.channel, 'Sorry!') + yield from process_module(message, command, raw) client.run(token) -- 2.7.4