Added digest command to developer plugin.
authorCMDR furrycat <elite@furrycat.net>
Fri, 11 Nov 2016 07:54:18 +0000 (07:54 +0000)
committerCMDR furrycat <elite@furrycat.net>
Fri, 11 Nov 2016 07:54:18 +0000 (07:54 +0000)
plugin/developer/developer.py

index 14b5c78..436358f 100644 (file)
@@ -32,7 +32,7 @@ class Developer(object):
     return 'Helpful Discord developer tools.'
 
   def valid_commands(self):
-    return ['id']
+    return ['digest', 'id']
 
   @asyncio.coroutine
   def handle_command(self, message, command, raw):
@@ -48,12 +48,21 @@ class Developer(object):
   @asyncio.coroutine
   def run_command(self, message, args):
     command = args[0]
-    if command == 'id':
+    if command == 'digest':
+      yield from self.handle_digest(message, args[1:])
+    elif command == 'id':
       yield from self.handle_id(message, args[1:])
 
   @asyncio.coroutine
   def help_command(self, message, command):
-    if command == 'id':
+    if command == 'digest':
+      lines = [
+        'Post the digest of a message.',
+        '```digest CHANNEL MESSAGE```',
+        '',
+        "You can retrieve the `CHANNEL` and `MESSAGE` ID from the web client's *Copy ID* menu when developer tools are enabled."
+      ]
+    elif command == 'id':
       lines = [
         'Print the Discord IDs of member, roles or channels known to me.',
         '```',
@@ -81,6 +90,40 @@ class Developer(object):
     yield from bot.say_many(message.channel, lines)
 
   @asyncio.coroutine
+  def get_message(self, message, channel_id, message_id):
+    channel = bot.parse_channel(channel_id, voice_ok = False)
+    if not channel:
+      yield from bot.say(message.channel, 'Channel?')
+      return None
+
+    permissions = channel.permissions_for(message.author)
+    if permissions is None:
+      log.info('User {} has no permissions in channel {}'.format(message.author, channel_id))
+      yield from bot.say(message.channel, 'Channel?')
+      return None
+    if not permissions.read_message_history:
+      log.info('User {} has no read message history permission in channel {}'.format(message.author, channel_id))
+      yield from bot.say(message.channel, 'Message?')
+      return None
+
+    m = re.match(r'^\d+$', message_id)
+    if m is not None:
+      result = yield from client.get_message(channel, message_id)
+      if result is not None:
+        return result
+      yield from bot.say(message.channel, 'Message?')
+      return None
+
+  @asyncio.coroutine
+  def handle_digest(self, message, args):
+    if len(args) > 1:
+      result = yield from self.get_message(message, *args)
+      if result is not None:
+        yield from bot.say(message.channel, bot.digest(result.content))
+        return
+    yield from cat.yelp(message.channel)
+
+  @asyncio.coroutine
   def handle_id(self, message, args):
     do = Identify.member.value | Identify.role.value | Identify.channel.value
     target = None