Check admins.
authorCMDR furrycat <elite@furrycat.net>
Thu, 15 Sep 2016 09:24:21 +0000 (10:24 +0100)
committerCMDR furrycat <elite@furrycat.net>
Thu, 15 Sep 2016 09:24:21 +0000 (10:24 +0100)
bot.py

diff --git a/bot.py b/bot.py
index 121323a..163b914 100755 (executable)
--- a/bot.py
+++ b/bot.py
@@ -7,16 +7,60 @@ import pexpect
 import random
 import re
 
+admins = []
+fd = open('ADMINS', 'r')
+for line in fd.readlines():
+  admin = int(line.strip())
+  admins.append(admin)
+fd.close()
+
 fd = open('TOKEN', 'r')
 token = fd.readline().strip()
 fd.close()
-#logging.basicConfig(level = logging.DEBUG)
+logging.basicConfig(level = logging.WARN)
 log = logging.getLogger('discord')
 
 def wait_for_prompt(p):
   p.expect('EDI> ')
 
 @asyncio.coroutine
+def maybe_say(channel, text, probability = 0.2):
+  if random.random() <= probability:
+    yield from client.send_typing(channel)
+    yield from client.send_message(channel, text)
+  else:
+    log.debug("Didn't bother to say {}".format(text))
+
+@asyncio.coroutine
+def say(channel, text):
+  yield from maybe_say(channel, text, 1.0)
+
+@asyncio.coroutine
+def not_admin(message):
+  log.warning('Not admin: {} id {}'.format(message.author, message.author.id))
+  yield from maybe_say(message.channel, 'hiss!')
+
+@asyncio.coroutine
+def do_admin(message, command, raw):
+  if not int(message.author.id) in admins:
+    yield from not_admin(message)
+    return
+
+  # debug
+  log.debug('Admin command: {}'.format(raw))
+  if command == 'debug':
+    m = re.match(r'\bdebug\s+(o(?:ff|n))\b', raw.lower())
+    if m is not None:
+      arg = m.group(1)
+      if arg == 'on':
+        log.setLevel(logging.DEBUG)
+      elif arg == 'off':
+        log.setLevel(logging.WARN)
+      yield from maybe_say(message.channel, 'purr')
+      return
+    yield from say(message.channel, 'Sorry!')
+
+@asyncio.coroutine
 def do_edi(message, command, raw, p):
   if command == 'close_to' and not '-m' in raw:
     raw = 'close_to'
@@ -29,12 +73,23 @@ def do_edi(message, command, raw, p):
     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 say(message.channel, 'Sorry!')
 
 p = pexpect.spawnu('./edi.py')
 wait_for_prompt(p)
 
-modules = { 'edi': { 'fn': do_edi, 'args': [p], 'commands': ['coords', 'close_to', 'distance', 'edts', 'fuel_usage', 'galmath', 'help', 'raikogram'] } }
+modules = {
+  'admin': {
+    'fn': do_admin,
+    'args': [],
+    'commands': 'debug'
+  },
+  'edi': {
+    'fn': do_edi,
+    'args': [p],
+    'commands': ['coords', 'close_to', 'distance', 'edts', 'fuel_usage', 'galmath', 'help', 'raikogram']
+  }
+}
 
 client = discord.Client()
 
@@ -48,13 +103,13 @@ def process_module(message, command, raw):
       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!')
+  yield from maybe_say(message.channel, 'meow!')
 
 @client.event
 @asyncio.coroutine
 def on_ready():
   print('Logged in as {}#{}'.format(client.user.name, client.user.id))
+  print('Admins are: {}'.format(admins))
   #yield from client.edit_profile(avatar = open('DELTA-FURRYCAThires.png', 'rb').read())
 
 @client.event