def get_info(server_id, user_id): """Builds a string listing known user information of given user.""" user_data = servermanager.servers_data[server_id]['users'][user_id] config = configmanager.config permissions_text = '' if servermanager.is_owner(user_id): # Order important here permissions_text = "Bot owner" elif servermanager.is_bot(user_id): permissions_text = "Literally the bot" elif servermanager.is_admin(server_id, user_id): permissions_text = "Bot admin" elif servermanager.is_banned(server_id, user_id): permissions_text = "Banned from interaction" else: permissions_text = "Standard user" to_return = """User information for {user_data[name]}: ``` ID: {user_id} Name: {user_data[name]} Nickname: {user_data[nickname]} Discriminator: {user_data[discriminator]} Aliases: {user_data[aliases]} Permissions: {permissions_text} Joined: {user_data[joined]} Last seen: {user_data[last_seen]} Last played game: {user_data[last_game]} Color: {user_data[color]} Status: {user_data[status]} Avatar: {user_data[avatar]} ```""".format(user_id=user_id, user_data=user_data, permissions_text=permissions_text) return to_return # Placeholder if this will be modified later
def check_tag_access(server_id, tag_data, tag_name, user_id, need_owner=False): """Ensures that the given user is author of the tag (or a bot admin) if the tag is private, otherwise throw an exception.""" if (tag_data['private'] or need_owner) and ( tag_data['author_id'] != user_id and not servermanager.is_admin(server_id, user_id)): tag_author = usermanager.get_name(server_id, tag_data['author_id']) if need_owner: raise bot_exception( EXCEPT_TYPE, "User is not the author of tag '{tag_name}', created by {tag_author}" .format(tag_name=tag_name, tag_author=tag_author)) else: raise bot_exception( EXCEPT_TYPE, "The tag '{tag_name}' was made private by the author, {tag_author}" .format(tag_name=tag_name, tag_author=tag_author))
async def get_response(message, send_typing=True): """Gets a response. Split up so messages can be edited.""" # Check if the message is a valid command and the server or channel is not muted if (message.content and message.content[0] in configmanager.config['command_invokers'] and message.author.id != client.user.id): # Valid invoker from not the bot is_private = (type(message.channel) is discord.PrivateChannel) if parser.is_command(message.content.partition(' ')[0][1:].lower(), private=is_private): # Valid command if is_private or ( not is_private and # Private - skip checks. Not private - do server checks. not servermanager.is_banned(server_id=message.server.id, user_id=message.author.id) and # Author is not banned ( not servermanager.is_muted(server_id=message.server.id, channel_id=message.channel.id) or # Server or channel is not muted or (servermanager.is_admin(message.server.id, message.author.id) and message.content[1:].startswith('admin'))) ): # Admin is unmuting bot try: global last_responses_dictionary if send_typing: await client.send_typing(message.channel) return await parser.parse( message.content, message.server.id if not is_private else '0', message.channel.id if not is_private else '0', message.author.id if not is_private else '0', message.author.voice_channel.id if (not is_private and message.author.voice_channel) else 0, # Previously type None is_private) except bot_exception as e: # Something bad happened return [str(e), False] return ['', False]
async def get_response(message, send_typing=True): """Gets a response. Split up so messages can be edited.""" # Check if the message is a valid command and the server or channel is not muted if (message.content and message.content[0] in configmanager.config['command_invokers'] and message.author.id != client.user.id): # Valid invoker from not the bot is_private = (type(message.channel) is discord.PrivateChannel) if parser.is_command(message.content.partition(' ')[0][1:].lower(), private=is_private): # Valid command if is_private or (not is_private and # Private - skip checks. Not private - do server checks. not servermanager.is_banned(server_id=message.server.id, user_id=message.author.id) and # Author is not banned (not servermanager.is_muted(server_id=message.server.id, channel_id=message.channel.id) or # Server or channel is not muted or (servermanager.is_admin(message.server.id, message.author.id) and message.content[1:].startswith('admin')))): # Admin is unmuting bot try: global last_responses_dictionary if send_typing: await client.send_typing(message.channel) return await parser.parse( message.content, message.server.id if not is_private else '0', message.channel.id if not is_private else '0', message.author.id if not is_private else '0', message.author.voice_channel.id if (not is_private and message.author.voice_channel) else 0, # Previously type None is_private) except bot_exception as e: # Something bad happened return [str(e), False] return ['', False]
def check_sound_tag_access(server_id, sound_tag_data, user_id, need_owner=False): """Ensures that the given user is author of the tag (or a bot admin) if the tag is private, otherwise throw an exception.""" if (sound_tag_data['private'] or need_owner) and (sound_tag_data['author_id'] != user_id and not servermanager.is_admin(server_id, user_id)): tag_author = usermanager.get_name(server_id, sound_tag_data['author_id']) if sound_tag_data['private']: raise bot_exception(EXCEPT_TYPE, "This sound tag was made private by the author, {tag_author}".format(tag_author=tag_author)) else: raise bot_exception(EXCEPT_TYPE, "User is not the author of sound tag '{sound_tag_name}', created by {tag_author}".format( sound_tag_name=sound_tag_data['name'], tag_author=tag_author))
async def parse(text, server_id, channel_id, author_id, voice_channel_id, is_private=False): """Parses the text to separate the command, options, and arguments, and passes them into the apropriate modules.""" is_admin = servermanager.is_admin(server_id, author_id) if not is_private else False text_partition, _, text = text[1:].partition(' ') # Remove invoker character text = text.strip() raw_parameters = text # Used for some functions (mostly shortcuts) command = text_partition.lower() option_invoker = configmanager.config['option_invoker'] # Look for options options = [] while text.startswith(option_invoker): option_partition, _, text = text[1:].partition(' ') text = text.strip() if option_partition: options.append(option_partition) # Look for arguments arguments = [] arguments_blocks = [] while text: if len(arguments_blocks) < 4: arguments_blocks.append(text) if text.startswith('"'): # Search for end quote try: text = text[1:] closed_quote_index = 0 next_quote_index = text[closed_quote_index:].index('"') # Maybe use do while? while text[closed_quote_index + next_quote_index - 1] == '\\': # Run until proper closed quote found closed_quote_index += next_quote_index text = text[:closed_quote_index - 1] + text[closed_quote_index:] next_quote_index = text[closed_quote_index:].index('"') except ValueError: raise bot_exception(EXCEPT_TYPE, "An argument has an unclosed quote") closed_quote_index += next_quote_index if closed_quote_index > 0: # No null argument to_append = text[:closed_quote_index].strip() if to_append: # No empty string arguments.append(to_append) text = text[closed_quote_index + 1:].strip() else: # If there is a null argument, remove it text = text[2:].strip() else: # Search for next space argument_partition, _, text = text.partition(' ') arguments.append(argument_partition) text = text.strip() # Debug printing print("DEBUG:\n{}\n{}\n{}\n{}\n------".format(command, options, arguments, arguments_blocks)) # Get proper response to_return = ['', False] # Text response, TTS get_response_function = help # Function placeholder # TODO: See if there is an object representation of modules if command in tagmanager.commands: # Tag manager get_response_function = getattr(tagmanager, 'get_response') elif command in soundtagmanager.commands: # Sound tag manager get_response_function = getattr(soundtagmanager, 'get_response') elif command in usermanager.commands: # User manager get_response_function = getattr(usermanager, 'get_response') elif command in decider.commands: # Decider get_response_function = getattr(decider, 'get_response') elif command in utilities.commands: # Utilities get_response_function = getattr(utilities, 'get_response') elif command in base_commands: # Base commands get_response_function = get_response if get_response_function != help: # Found a response function to_return[0] = await get_response_function( command, options, arguments, arguments_blocks, raw_parameters, server_id, channel_id, voice_channel_id, author_id, is_admin, is_private) else: to_return[0] += "Sorry, I can't seem to do that right now. You should never see this, by the way. What is most likely going on is that you are hallucinating this error." if to_return[0] is not None and len(to_return[0]) > 1900: # Split responses up if it is too long to_return[0] = "```\n***Looks like the response is very large. It might look a little messed up because it's gettin' chopped up.***```\n" + to_return[0][:1900] return to_return