Added parse_server() and permission(); made author optional for parse_mention().
authorCMDR furrycat <elite@furrycat.net>
Wed, 1 Mar 2017 11:54:44 +0000 (11:54 +0000)
committerCMDR furrycat <elite@furrycat.net>
Wed, 1 Mar 2017 11:54:44 +0000 (11:54 +0000)
bot.py

diff --git a/bot.py b/bot.py
index 6d0f176..70398f8 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -103,6 +103,24 @@ def unparse_seconds(seconds):
     interval += '{}s'.format(seconds)
   return interval
 
+def parse_server(text):
+  log.debug("Parsing server {}".format(text))
+
+  # id.
+  m = re.match(r'^\d+$', text)
+  if m is not None:
+    log.debug("Server ID {}".format(text))
+    server = client.get_server(text)
+    if server is not None:
+      return server
+
+  for server in client.servers:
+    if server.name.lower() == text.lower():
+      return server
+
+  log.warning("Can't find server with name or ID {}".format(text))
+  return None
+
 def parse_channel(text, *, text_ok = True, voice_ok = True):
   channel_type = '{}, {}'.format('text' if text_ok else 'non-text', 'voice' if voice_ok else 'non-voice')
   log.debug("Parsing {} channel {}".format(channel_type, text))
@@ -154,12 +172,15 @@ def parse_channel(text, *, text_ok = True, voice_ok = True):
   log.warning("Can't find {} channel {}!".format(channel_type, text))
   return None
 
-def parse_mention(text, author):
+def parse_mention(text, author = None):
   log.debug("Parsing mention {}".format(text))
 
   # Message author.
   if text == 'me':
-    return author.id
+    if author is not None:
+      return author.id
+    log.error("Can't parse_mention('{}') with no author!".format(text))
+    return None
 
   # @everyone or @here.
   m = re.match(r'^@?(everyone|here)$', text)
@@ -507,6 +528,15 @@ def equal_permissions(x, y, *, strict = True):
   # (allow, deny)
   return x[0].is_subset(y[0]) and x[1].is_superset(y[1])
 
+def permission(permission_name, allow = True):
+  overwrite = discord.PermissionOverwrite()
+  if permission_name not in overwrite.VALID_NAMES:
+    log.error("Invalid permission name {}!".format(permission_name))
+    return None
+  args = { permission_name: allow }
+  overwrite.update(**args)
+  return overwrite
+
 @asyncio.coroutine
 def overwrite_permissions(channel, whom, overwrite, *, strict = True):
   permissions = channel.overwrites_for(whom)