interval += '{}s'.format(seconds)
return interval
-def parse_server(text):
+def shared_with(user, instance):
+ if user is None:
+ return True
+ if user.id == client.user.id:
+ return True
+ if is_admin(user):
+ return True
+
+ if type(instance) == discord.server.Server:
+ servers = [instance]
+ elif type(instance) == discord.user.User:
+ for server in client.servers:
+ if server.get_member(instance.id):
+ servers.append(server)
+ elif type(instance) == discord.channel.Channel:
+ servers = [instance.server]
+ elif type(instance) == discord.role.Role:
+ servers = [instance.server]
+ else:
+ log.warning("Don't know how to check if a {} is known to {}!".format(type(instance), user))
+ return True
+ log.debug("Checking if {} {} is known to {}".format(type(instance), instance, user))
+
+ for server in servers:
+ if server.get_member(user.id):
+ log.debug('Member {} found on server {}'.format(user, server.name))
+ return True
+ else:
+ log.debug('Member {} not on server {}!'.format(user, server.name))
+ return False
+
+def parse_server(text, *, author = None):
log.debug("Parsing server {}".format(text))
+ if author is None:
+ log.warning('Called parse_server() with no author!')
# id.
m = re.match(r'^\d+$', text)
log.debug("Server ID {}".format(text))
server = client.get_server(text)
if server is not None:
- return server
+ if shared_with(author, server):
+ return server
+ if author is not None:
+ log.warning("User {} isn't on parsed server {}!".format(author, server.name))
for server in client.servers:
if server.name.lower() == text.lower():
- return server
+ if shared_with(author, server):
+ return server
+ if author is not None:
+ log.warning("User {} isn't on parsed server {}!".format(author, server.name))
log.warning("Can't find server with name or ID {}".format(text))
return None
-def parse_channel(text, *, text_ok = True, voice_ok = True):
+def parse_channel(text, *, author = None, 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))
+ if author is None:
+ log.warning('Called parse_channel() with no author!')
# Private message to user.
if text == 'private':
log.warning('Parsed private channel. Caller should check for private!')
+
if not text_ok:
log.warning("Requested voice channel but private channels are text!")
return None
if not voice_ok and channel.type == discord.ChannelType.voice:
log.warning('Parsed channel {} of wrong type!'.format(channel_id))
return None
- return channel
+ if shared_with(author, channel):
+ return channel
+ if author is not None:
+ log.warning("User {} isn't on channel {} server {}!".format(author, channel.name, server.name))
# #name or name.
if text[0] == '#':
if not voice_ok and channel.type == discord.ChannelType.voice:
log.warning('Parsed channel {} of wrong type!'.format(channel_id))
continue
- return channel
+ if shared_with(author, channel):
+ return channel
+ if author is not None:
+ log.warning("User {} isn't on channel {} server {}!".format(author, channel.name, server.name))
log.warning("Can't find {} channel {}!".format(channel_type, text))
return None
-def parse_mention(text, author = None):
+def parse_mention(text, *, author = None):
log.debug("Parsing mention {}".format(text))
+ if author is None:
+ log.warning('Called parse_mention() with no author!')
# Message author.
if text == 'me':
for role in server.roles:
if role.id == role_id:
log.info('Parsed role {} as {} on {}.'.format(role_id, role.name, server.name))
- return '&{}'.format(role_id)
+ if shared_with(author, role):
+ return '&{}'.format(role_id)
+ if author is not None:
+ log.warning("User {} isn't on role {} server {}!".format(author, role.name, server.name))
log.warning("Can't find role with ID {}!".format(role_id))
return None
for role in server.roles:
if role.name.lower() == name:
log.info('Parsed {} as {} on {}.'.format(name, role.name, server.name))
- return '&{}'.format(role.id)
+ if shared_with(author, role):
+ return '&{}'.format(role.id)
+ if author is not None:
+ log.warning("User {} isn't on role{} server {}!".format(author, role.name, server.name))
if m.group(1) != '&':
log.debug('Member {}.'.format(name))
parts = name.split('#')
parsed['mention'] = None
ok = True
elif param:
- mention = bot.parse_mention(param, message.author)
+ mention = bot.parse_mention(param, author = message.author)
if mention:
parsed['mention'] = mention
ok = True
parsed[k] = 'private'
ok = True
elif param:
- parse = {}
+ parse = { 'author': message.author }
if arg == 'in':
parse['voice_ok'] = False
channel = bot.parse_channel(param, **parse)
else:
if 'voice_id' in parsed and parsed['voice_id'] == parsed['channel_id']:
# Voice only.
- channel = bot.parse_channel(parsed['channel_id'], text_ok = False)
+ channel = bot.parse_channel(parsed['channel_id'], author = message.author, text_ok = False)
else:
# Text only.
- channel = bot.parse_channel(parsed['channel_id'], voice_ok = False)
+ channel = bot.parse_channel(parsed['channel_id'], author = message.author, voice_ok = False)
if not channel:
await bot.say(message.channel, 'Invalid channel!')
return None
server = channel.server
parsed['server_id'] = server.id
elif 'voice_id' in parsed:
- voice = bot.parse_channel(parsed['voice_id'], text_ok = False)
+ voice = bot.parse_channel(parsed['voice_id'], author = message.author, text_ok = False)
if not voice:
await bot.say(message.channel, 'Invalid voice channel!')
return None