Command to set avatar.
authorCMDR furrycat <elite@furrycat.net>
Thu, 15 Sep 2016 09:36:33 +0000 (10:36 +0100)
committerCMDR furrycat <elite@furrycat.net>
Thu, 15 Sep 2016 09:36:33 +0000 (10:36 +0100)
bot.py

diff --git a/bot.py b/bot.py
index 163b914..3b1032f 100755 (executable)
--- a/bot.py
+++ b/bot.py
@@ -6,6 +6,7 @@ import logging
 import pexpect
 import random
 import re
+import util
 
 admins = []
 fd = open('ADMINS', 'r')
@@ -46,19 +47,42 @@ def do_admin(message, command, raw):
     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 command == 'avatar':
+    # avatar
+    url = None
+    m = re.match(r'\bavatar\s+(\S+)', 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')
+      url = m.group(1)
+    elif len(message.attachments):
+      url = message.attachments[0]['url']
+    if url is None:
+      log.debug('No URL for avatar')
+      yield from say(message.channel, 'zzz')
       return
-    yield from say(message.channel, 'Sorry!')
+    fd = util.open_url(url)
+    if fd is None:
+      log.debug('Failed to open URL {} for avatar'.format(url))
+      yield from say(message.channel, 'yelp!')
+      return
+    yield from client.edit_profile(avatar = fd.read())
+    fd.close()
+
+  elif command == 'debug':
+    # debug
+    m = re.match(r'\bdebug\s+(o(?:ff|n))\b', raw.lower())
+    if m is None:
+      yield from say(message.channel, 'Sorry!')
+      return
+
+    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')
 
 @asyncio.coroutine
 def do_edi(message, command, raw, p):
@@ -82,7 +106,7 @@ modules = {
   'admin': {
     'fn': do_admin,
     'args': [],
-    'commands': 'debug'
+    'commands': ['avatar', 'debug']
   },
   'edi': {
     'fn': do_edi,
@@ -110,7 +134,6 @@ def process_module(message, command, raw):
 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
 @asyncio.coroutine