def remove_sound_tag(server_id, sound_tag_name, user_id): """Removes the given sound tag in the server.""" sound_tag_data = get_sound_tag_data(server_id, sound_tag_name) check_sound_tag_access(server_id, sound_tag_data, user_id, need_owner=True) servers_data = servermanager.servers_data del servers_data[server_id]['sound_tags'][sound_tag_name] write_data() return "Tag '{}' successfully removed!".format(sound_tag_name)
def set_nickname(server_id, user_id, nickname_text): """Sets the user nickname as the given text.""" if len(nickname_text) > 50: raise bot_exception(EXCEPT_TYPE, "Nickname cannot be more than 50 characters long") servers_data = servermanager.servers_data servers_data[server_id]['users'][user_id]['nickname'] = nickname_text write_data() return "Nickname successfully {}!".format("set" if nickname_text else "cleared")
def set_status(server_id, user_id, status_text): """Sets the user status as the given text.""" if len(status_text) > 1000: raise bot_exception(EXCEPT_TYPE, "Status cannot be more than 1000 characters long") servers_data = servermanager.servers_data servers_data[server_id]['users'][user_id]['status'] = status_text write_data() return "Status successfully {}!".format("set" if status_text else "cleared")
def update_server(server, update_all=False, force_write=False): servermanager.update_server(server_id=server.id, name=server.name, total_members=len(server.members), owner=server.owner.id, icon=server.icon_url, force_write=force_write) if update_all: for channel in server.channels: update_channel(server, channel, force_write) for user in server.members: update_user(server, user, force_write) servermanager.write_data()
def update_server(server, update_all=False, force_write=False): servermanager.update_server( server_id=server.id, name=server.name, total_members=len(server.members), owner=server.owner.id, icon=server.icon_url, force_write=force_write) if update_all: for channel in server.channels: update_channel(server, channel, force_write) for user in server.members: update_user(server, user, force_write) servermanager.write_data()
def update_tag(server_id, tag_name, increment_hits=False, **kwargs): """Updates or creates a tag in the server under the given author ID. Keyword arguments: increment_hits -- increments the hits counter of the tag user_id -- user trying to modify the tag author_id -- author of the tag tag_text -- text to go along with the tag private -- whether or not the tag can only be called by the author hits -- how many times the tag has been called full_name -- what the full name (with spaces) is (never passed in) """ to_return = '' full_name = tag_name tag_name = tag_name.replace(' ', '') servers_data = servermanager.servers_data if increment_hits: # Updating hit counter servers_data[server_id]['tags'][tag_name]['hits'] += 1 else: # Creating or modifying a tag try: # Modify a tag tag_data = servers_data[server_id]['tags'][tag_name] try: check_tag_access(server_id, tag_data, tag_name, kwargs['user_id'], need_owner=True) except KeyError: # user_id not found, meant to create but tag exists raise bot_exception(EXCEPT_TYPE, "Tag '{}' already exists".format(tag_name)) del kwargs['user_id'] # Don't write user_id to updated tag servers_data[server_id]['tags'][tag_name].update(kwargs) to_return += "Tag '{}' successfully modified!".format(full_name) except KeyError: # Tag doesn't exist. Create it. if configmanager.config['tags_per_server'] > 0 and len(servers_data[server_id]['tags']) >= configmanager.config['tags_per_server']: raise bot_exception(EXCEPT_TYPE, "This server has hit the tag limit of {}".format(configmanager.config['tags_per_server'])) if 'tag_text' not in kwargs: raise bot_exception(EXCEPT_TYPE, "Tag '{}' does not exist".format(tag_name)) if len(tag_name) > 50: raise bot_exception(EXCEPT_TYPE, "Tag names cannot be larger than 50 characters long") if len(kwargs['tag_text']) > 2000: # This shouldn't really happen, ever raise bot_exception(EXCEPT_TYPE, "Tag text cannot be larger than 2000 characters long") # Edit safety if 'user_id' in kwargs: kwargs['author_id'] = kwargs['user_id'] del kwargs['user_id'] kwargs['full_name'] = full_name servers_data[server_id]['tags'][tag_name] = {**kwargs, 'hits':0, 'date_created':time.strftime("%c")} to_return += "Tag '{}' successfully created!".format(full_name) write_data() return to_return
async def set_color(server_id, user_id, color): """Gives the user the given color as a role.""" if not botmanager.has_role_permissions(server_id): # Check bot permissions first raise bot_exception(EXCEPT_TYPE, "Bot must be able to manage permissions in order to change role colors.") if color is None: servermanager.servers_data[server_id]['users'][user_id]['color'] = '' write_data() await botmanager.update_color_role(server_id, user_id, None) return "Color successfully cleared!" if color.startswith('#'): # Just in case people just copy values over color = color[1:] try: # Make sure we're given a valid hex value here converted = int(color, 16) except ValueError: raise bot_exception(EXCEPT_TYPE, "'{}' does not appear to be a valid hex color.".format(color)) await botmanager.update_color_role(server_id, user_id, converted) servermanager.servers_data[server_id]['users'][user_id]['color'] = '#{}'.format(color.upper()) write_data() return "Color successfully set!"
def update_sound_tag(server_id, sound_tag_name, increment_hits=False, **kwargs): """Updates or creates a tag in the server under the given author ID. Keyword arguments: increment_hits -- increments the hits counter of the tag user_id -- user trying to modify the tag author_id -- author of the tag duration -- how long the sound should play for url -- url of the audio source private -- whether or not the tag can only be called by the author hits -- how many times the tag has been called type -- tag type (either YouTube or direct) length -- how long the sound tag is in seconds full_name -- what the full name (with spaces) is """ to_return = '' full_name = sound_tag_name sound_tag_name = sound_tag_name.replace(' ', '') servers_data = servermanager.servers_data if increment_hits: # Updating hit counter servers_data[server_id]['sound_tags'][sound_tag_name]['hits'] += 1 else: # Creating or modifying a tag if kwargs['url'].startswith('https://www.youtube.com/') or kwargs['url'].startswith('https://youtu.be/'): kwargs['type'] = 'YouTube' else: kwargs['type'] = 'direct' # Check tag length if kwargs['type'] == 'YouTube': # Find length of YouTube video try: length = pafy.new(kwargs['url']).length except: raise bot_exception(EXCEPT_TYPE, "Invalid YouTube video") else: # Download file and check length try: urllib.request.urlretrieve (kwargs['url'], '{}/tempsound'.format(configmanager.data_directory)) length = MP3('{}/tempsound'.format(configmanager.data_directory)).info.length except: raise bot_exception(EXCEPT_TYPE, "Invalid direct download file or URL") length_limit = int(configmanager.config['sound_tag_length_limit']) if length_limit > 0 and int(length) > length_limit: raise bot_exception(EXCEPT_TYPE, "Sound tags can be no longer than {} second{}".format( length_limit, 's' if length_limit > 1 else '')) kwargs['length'] = int(length) try: sound_tag_data = servers_data[server_id]['sound_tags'][sound_tag_name] try: check_sound_tag_access(server_id, sound_tag_data, kwargs['user_id'], need_owner=True) except KeyError: # user_id not found, meant to create but tag exists raise bot_exception(EXCEPT_TYPE, "Sound tag '{}' already exists".format(sound_tag_name)) del kwargs['user_id'] # Don't write user_id to updated tag servers_data[server_id]['sound_tags'][sound_tag_name].update(kwargs) to_return += "Sound tag '{}' successfully modified!".format(full_name) except KeyError: # Tag doesn't exist. Create it. if configmanager.config['sound_tags_per_server'] > 0 and len(servers_data[server_id]['sound_tags']) >= configmanager.config['sound_tags_per_server']: raise bot_exception(EXCEPT_TYPE, "This server has hit the sound tag limit of {}".format(configmanager.config['sound_tags_per_server'])) if 'url' not in kwargs: raise bot_exception(EXCEPT_TYPE, "Sound tag '{}' does not exist".format(sound_tag_name)) if len(sound_tag_name) > 50: raise bot_exception(EXCEPT_TYPE, "Sound tag names cannot be larger than 50 characters long") if len(kwargs['url']) > 2000: # This shouldn't really happen, ever raise bot_exception(EXCEPT_TYPE, "Sound tag url cannot be larger than 2000 characters long (how did you do this)") # Edit safety if 'user_id' in kwargs: kwargs['author_id'] = kwargs['user_id'] del kwargs['user_id'] kwargs['full_name'] = full_name servers_data[server_id]['sound_tags'][sound_tag_name] = {**kwargs, 'hits':0, 'date_created':time.strftime("%c")} to_return += "Sound tag '{}' successfully created!".format(full_name) write_data() return to_return
def update_sound_tag(server_id, sound_tag_name, increment_hits=False, **kwargs): """Updates or creates a tag in the server under the given author ID. Keyword arguments: increment_hits -- increments the hits counter of the tag user_id -- user trying to modify the tag author_id -- author of the tag duration -- how long the sound should play for url -- url of the audio source private -- whether or not the tag can only be called by the author hits -- how many times the tag has been called type -- tag type (either YouTube or direct) length -- how long the sound tag is in seconds full_name -- what the full name (with spaces) is """ to_return = '' full_name = sound_tag_name sound_tag_name = sound_tag_name.replace(' ', '') servers_data = servermanager.servers_data if increment_hits: # Updating hit counter servers_data[server_id]['sound_tags'][sound_tag_name]['hits'] += 1 else: # Creating or modifying a tag if kwargs['url'].startswith('https://www.youtube.com/') or kwargs[ 'url'].startswith('https://youtu.be/'): kwargs['type'] = 'YouTube' else: kwargs['type'] = 'direct' # Check tag length if kwargs['type'] == 'YouTube': # Find length of YouTube video try: length = pafy.new(kwargs['url']).length except: raise bot_exception(EXCEPT_TYPE, "Invalid YouTube video") else: # Download file and check length try: urllib.request.urlretrieve( kwargs['url'], '{}/tempsound'.format(configmanager.data_directory)) length = MP3('{}/tempsound'.format( configmanager.data_directory)).info.length except: raise bot_exception(EXCEPT_TYPE, "Invalid direct download file or URL") length_limit = int(configmanager.config['sound_tag_length_limit']) if length_limit > 0 and int(length) > length_limit: raise bot_exception( EXCEPT_TYPE, "Sound tags can be no longer than {} second{}".format( length_limit, 's' if length_limit > 1 else '')) kwargs['length'] = int(length) try: sound_tag_data = servers_data[server_id]['sound_tags'][ sound_tag_name] try: check_sound_tag_access(server_id, sound_tag_data, kwargs['user_id'], need_owner=True) except KeyError: # user_id not found, meant to create but tag exists raise bot_exception( EXCEPT_TYPE, "Sound tag '{}' already exists".format(sound_tag_name)) del kwargs['user_id'] # Don't write user_id to updated tag servers_data[server_id]['sound_tags'][sound_tag_name].update( kwargs) to_return += "Sound tag '{}' successfully modified!".format( full_name) except KeyError: # Tag doesn't exist. Create it. if configmanager.config['sound_tags_per_server'] > 0 and len( servers_data[server_id]['sound_tags'] ) >= configmanager.config['sound_tags_per_server']: raise bot_exception( EXCEPT_TYPE, "This server has hit the sound tag limit of {}".format( configmanager.config['sound_tags_per_server'])) if 'url' not in kwargs: raise bot_exception( EXCEPT_TYPE, "Sound tag '{}' does not exist".format(sound_tag_name)) if len(sound_tag_name) > 50: raise bot_exception( EXCEPT_TYPE, "Sound tag names cannot be larger than 50 characters long") if len(kwargs['url']) > 2000: # This shouldn't really happen, ever raise bot_exception( EXCEPT_TYPE, "Sound tag url cannot be larger than 2000 characters long (how did you do this)" ) # Edit safety if 'user_id' in kwargs: kwargs['author_id'] = kwargs['user_id'] del kwargs['user_id'] kwargs['full_name'] = full_name servers_data[server_id]['sound_tags'][sound_tag_name] = { **kwargs, 'hits': 0, 'date_created': time.strftime("%c") } to_return += "Sound tag '{}' successfully created!".format( full_name) write_data() return to_return
def update_tag(server_id, tag_name, increment_hits=False, **kwargs): """Updates or creates a tag in the server under the given author ID. Keyword arguments: increment_hits -- increments the hits counter of the tag user_id -- user trying to modify the tag author_id -- author of the tag tag_text -- text to go along with the tag private -- whether or not the tag can only be called by the author hits -- how many times the tag has been called full_name -- what the full name (with spaces) is (never passed in) """ to_return = '' full_name = tag_name tag_name = tag_name.replace(' ', '') servers_data = servermanager.servers_data if increment_hits: # Updating hit counter servers_data[server_id]['tags'][tag_name]['hits'] += 1 else: # Creating or modifying a tag try: # Modify a tag tag_data = servers_data[server_id]['tags'][tag_name] try: check_tag_access(server_id, tag_data, tag_name, kwargs['user_id'], need_owner=True) except KeyError: # user_id not found, meant to create but tag exists raise bot_exception(EXCEPT_TYPE, "Tag '{}' already exists".format(tag_name)) del kwargs['user_id'] # Don't write user_id to updated tag servers_data[server_id]['tags'][tag_name].update(kwargs) to_return += "Tag '{}' successfully modified!".format(full_name) except KeyError: # Tag doesn't exist. Create it. if configmanager.config['tags_per_server'] > 0 and len( servers_data[server_id] ['tags']) >= configmanager.config['tags_per_server']: raise bot_exception( EXCEPT_TYPE, "This server has hit the tag limit of {}".format( configmanager.config['tags_per_server'])) if 'tag_text' not in kwargs: raise bot_exception(EXCEPT_TYPE, "Tag '{}' does not exist".format(tag_name)) if len(tag_name) > 50: raise bot_exception( EXCEPT_TYPE, "Tag names cannot be larger than 50 characters long") if len(kwargs['tag_text'] ) > 2000: # This shouldn't really happen, ever raise bot_exception( EXCEPT_TYPE, "Tag text cannot be larger than 2000 characters long") # Edit safety if 'user_id' in kwargs: kwargs['author_id'] = kwargs['user_id'] del kwargs['user_id'] kwargs['full_name'] = full_name servers_data[server_id]['tags'][tag_name] = { **kwargs, 'hits': 0, 'date_created': time.strftime("%c") } to_return += "Tag '{}' successfully created!".format(full_name) write_data() return to_return