Migrate bling plugin to Python 3.5.
authorCMDR furrycat <elite@furrycat.net>
Thu, 2 Mar 2017 10:05:21 +0000 (10:05 +0000)
committerCMDR furrycat <elite@furrycat.net>
Thu, 2 Mar 2017 10:05:21 +0000 (10:05 +0000)
plugin/bling/bling.py

index d2aa8b5..469e8da 100644 (file)
@@ -24,15 +24,13 @@ class Bling(object):
   def description(self):
     return 'Converts image for use as Discord avatars.'
 
-  @asyncio.coroutine
-  def handle_command(self, message, command, raw):
+  async def handle_command(self, message, command, raw):
     if command not in self.valid_commands():
       return PluginCommand.ignored
-    yield from self.bling(message, shlex.split(raw)[1:])
+    await self.bling(message, shlex.split(raw)[1:])
     return PluginCommand.exclusive
 
-  @asyncio.coroutine
-  def handle_help(self, message, *args):
+  async def handle_help(self, message, *args):
     lines = [
       'Convert an image into an avatar suitable for use on Discord.',
       '```',
@@ -60,10 +58,9 @@ class Bling(object):
       'If no `PROFILE` is specified with `--profile` I will use the `default` profile.  All `PROFILE`s, including `default`, are installed by my administrator under a subdirectory of `{0}`.  The background image should be installed as `{1}` and the foreground image should be installed as `{2}`, for example `{0}{3}default{3}{1} and {0}{3}default{3}{2}.'.format(self.path, self.bgname, self.fgname, os.sep),
       'Your image will be overlaid *above* {} and *beneath* {}.'.format(self.bgname, self.fgname)
     ]
-    yield from bot.say_many(message.channel, lines)
+    await bot.say_many(message.channel, lines)
 
-  @asyncio.coroutine
-  def bling(self, message, args):
+  async def bling(self, message, args):
     ap = ThrowingArgumentParser()
     ap.add_argument('--profiles', action='store_true')
     ap.add_argument('--profile', nargs='?')
@@ -71,18 +68,18 @@ class Bling(object):
     try:
       parsed = ap.parse_args(args)
     except ArgumentParserError:
-      yield from cat.yelp(message.channel)
+      await cat.yelp(message.channel)
       return
 
     if parsed.profiles:
-      yield from self.list_profiles(message)
+      await self.list_profiles(message)
     else:
       create = {}
       if parsed.name:
         create['name'] = parsed.name
       if parsed.profile:
         create['profile'] = parsed.profile
-      yield from self.bling_message(message, **create)
+      await self.bling_message(message, **create)
 
   def profiles(self):
     # Subdirectories with a foreground or background image.
@@ -91,13 +88,12 @@ class Bling(object):
     fgs = glob.glob(os.sep.join([self.path, '*', self.fgname]))
     return list(filter(lambda dir: dir != 'default', set([path.split(os.sep)[-2] for path in bgs + fgs])))
 
-  @asyncio.coroutine
-  def list_profiles(self, message):
+  async def list_profiles(self, message):
     profiles = self.profiles()
     if len(profiles):
-      yield from bot.say_many(message.channel, profiles)
+      await bot.say_many(message.channel, profiles)
     else:
-      yield from cat.shrug(message.channel)
+      await cat.shrug(message.channel)
 
   def parse_message(self, message, name):
     parsed = {
@@ -134,28 +130,27 @@ class Bling(object):
       parsed['url'] = message.author.avatar_url
     return parsed
 
-  @asyncio.coroutine
-  def bling_message(self, message, *, name = None, profile = 'default'):
+  async def bling_message(self, message, *, name = None, profile = 'default'):
     if profile != 'default':
       if profile not in self.profiles():
-        yield from bot.say(message.channel, '{}?'.format(profile))
+        await bot.say(message.channel, '{}?'.format(profile))
         return
 
     parsed = self.parse_message(message, name)
     if parsed['url'] is None:
       log.debug('No URL for avatar to bling.')
-      yield from cat.zzz(message.channel)
+      await cat.zzz(message.channel)
       return
 
-    yield from client.send_typing(message.channel)
+    await client.send_typing(message.channel)
 
     data = self.create_avatar(parsed['url'], os.sep.join([self.path, profile, self.bgname]), os.sep.join([self.path, profile, self.fgname]))
     if data is None:
-      yield from cat.yelp(message.channel)
+      await cat.yelp(message.channel)
       return
 
     text = ' '.join(parsed['mentions']) if parsed['mentions'] is not None else 'purr'
-    yield from bot.say(message.channel, text, attachment = bot.parse_attachment(data, parsed['filename'] + '.png'))
+    await bot.say(message.channel, text, attachment = bot.parse_attachment(data, parsed['filename'] + '.png'))
 
   def create_avatar(self, url, bgfile, fgfile):
     # Get Delta images.