edts bot
authorCMDR furrycat <elite@furrycat.net>
Thu, 15 Sep 2016 08:45:40 +0000 (09:45 +0100)
committerCMDR furrycat <elite@furrycat.net>
Thu, 15 Sep 2016 08:45:40 +0000 (09:45 +0100)
bot.py [new file with mode: 0755]

diff --git a/bot.py b/bot.py
new file mode 100755 (executable)
index 0000000..509b9ae
--- /dev/null
+++ b/bot.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+
+import asyncio
+import discord
+import logging
+import pexpect
+import random
+import re
+
+fd = open('TOKEN', 'r')
+token = fd.readline().strip()
+fd.close()
+#logging.basicConfig(level = logging.DEBUG)
+log = logging.getLogger('discord')
+
+def wait_for_prompt(p):
+  p.expect('EDI> ')
+
+p = pexpect.spawnu('./edi.py')
+wait_for_prompt(p)
+stuff = { 'edi': p, 'commands': ['coords', 'close_to', 'distance', 'edts', 'fuel_usage', 'galmath', 'help', 'raikogram'] }
+
+client = discord.Client()
+
+@client.event
+@asyncio.coroutine
+def on_ready():
+  print('Logged in as {}#{}'.format(client.user.name, client.user.id))
+  #yield from client.edit_profile(avatar = open('DELTA-FURRYCAThires.png', 'rb').read())
+
+@client.event
+@asyncio.coroutine
+def on_message(message):
+  if message.author.id == client.user.id:
+    return
+  log.debug('Got message from {}: {}'.format(message.author, message.content))
+  if not client.user.mentioned_in(message) and not message.channel.is_private:
+    log.debug('Message is not for me!')
+    return
+  raw = re.sub('<@{}>'.format(client.user.id), '', message.content).strip()
+  m = re.match(r'^\s*(\S+)', raw)
+  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!')
+
+client.run(token)