diff options
author | 2019-05-03 21:30:48 +0100 | |
---|---|---|
committer | 2019-05-03 21:30:48 +0100 | |
commit | a0adcd7956f5cbf1734e39b37930f8c30f6292a0 (patch) | |
tree | a1b42a1824015d6b3c2e09bed4e94d5b019589d3 /bot | |
parent | Fixed linting errors. (diff) |
Combined filter_name and find_spaces into one method. Renamed variables to be more Pythonic, and improved the comments.
Diffstat (limited to 'bot')
-rw-r--r-- | bot/seasons/easter/bunny_name_generator.py | 133 |
1 files changed, 60 insertions, 73 deletions
diff --git a/bot/seasons/easter/bunny_name_generator.py b/bot/seasons/easter/bunny_name_generator.py index 7d40e219..f8d0eea3 100644 --- a/bot/seasons/easter/bunny_name_generator.py +++ b/bot/seasons/easter/bunny_name_generator.py @@ -18,6 +18,54 @@ class BunnyNameGenerator(commands.Cog): def __init__(self, bot): self.bot = bot + def find_separators(self, displayname): + """ + Check if Discord name contains spaces so we can bunnify an individual word in the name. + """ + + new_name = re.split(r'[_.\s]', str(displayname)) + if displayname not in new_name: + return new_name + + def find_vowels(self, displayname): + """ + Finds vowels in the user's display name. + + If the Discord name contains a vowel and the letter y, + it will match one or more of these patterns. + Only the most recently matched pattern will apply the changes. + """ + + new_name = None + + option1 = re.sub(r'a.+y', 'patchy', displayname) + option2 = re.sub(r'e.+y', 'ears', displayname) + option3 = re.sub(r'i.+y', 'ditsy', displayname) + option4 = re.sub(r'o.+y', 'oofy', displayname) + option5 = re.sub(r'u.+y', 'uffy', displayname) + + if option1 != displayname: + new_name = option1 + if option2 != displayname: + new_name = option2 + if option3 != displayname: + new_name = option3 + if option4 != displayname: + new_name = option4 + if option5 != displayname: + new_name = option5 + + return new_name + + def append_name(self, displayname): + """Adds a suffix to the end of the Discord name""" + + extensions = ['foot', 'ear', 'nose', 'tail'] + suffix = random.choice(extensions) + appended_name = displayname + suffix + + return appended_name + @commands.command() async def bunnyname(self, ctx): """Picks a random bunny name from a JSON file""" @@ -28,86 +76,25 @@ class BunnyNameGenerator(commands.Cog): async def bunnifyme(self, ctx): """Gets your Discord username and bunnifies it""" - def filter_name(displayname): - """Turns separators into whitespace""" - - if displayname.find('_') != -1: - displayname = displayname.replace('_', ' ') - if displayname.find('.') != -1: - displayname = displayname.replace('.', ' ') - - return displayname - - def find_spaces(displayname): - """ - Check if Discord name contains spaces so we can bunnify an individual word in the name. - - Spaces should not be bunnified so we remove them from - the list that is returned from the pattern matching. - """ - - contains_spaces = re.findall(r'^\w+|\s+|\w+$', displayname) - if len(contains_spaces) > 1: - contains_spaces.remove(' ') - displayname = contains_spaces - return displayname - - def find_vowels(displayname): - """ - Finds vowels in the user's display name. - - If the Discord name contains a vowel and the letter y, - it will match one or more of these patterns. - Only the most recently matched pattern will apply the changes. - """ - - new_name = None - - option1 = re.sub(r'a.+y', 'patchy', displayname) - option2 = re.sub(r'e.+y', 'ears', displayname) - option3 = re.sub(r'i.+y', 'ditsy', displayname) - option4 = re.sub(r'o.+y', 'oofy', displayname) - option5 = re.sub(r'u.+y', 'uffy', displayname) - - if option1 != displayname: - new_name = option1 - if option2 != displayname: - new_name = option2 - if option3 != displayname: - new_name = option3 - if option4 != displayname: - new_name = option4 - if option5 != displayname: - new_name = option5 - - return new_name - - def append_name(displayname): - """Adds a suffix to the end of the Discord name""" - - extensions = ['foot', 'ear', 'nose', 'tail'] - suffix = random.choice(extensions) - appended_name = displayname + suffix - - return appended_name - username = ctx.message.author.display_name - username_filtered = filter_name(username) # Filter username before pattern matching - spaces_pattern = find_spaces(username_filtered) # Does the name contain spaces? + # If name contains spaces or other separators, get the individual words to randomly bunnify + spaces_in_name = self.find_separators(username) - vowels_pattern = find_vowels(username_filtered) # Does the name contain vowels? - # If so, does it match any of the patterns in this function? + # If name contains vowels, see if it matches any of the patterns in this function + # If there are matches, the bunnified name is returned. + vowels_in_name = self.find_vowels(username) - unmatched_name = append_name(username_filtered) # Default if name doesn't match the above patterns + # Default if the checks above return None + unmatched_name = self.append_name(username) - if spaces_pattern is not None: + if spaces_in_name is not None: replacements = ['Cotton', 'Fluff', 'Floof' 'Bounce', 'Snuffle', 'Nibble', 'Cuddle', 'Velvetpaw', 'Carrot'] - word_to_replace = random.choice(spaces_pattern) + word_to_replace = random.choice(spaces_in_name) substitute = random.choice(replacements) - bunnified_name = username_filtered.replace(word_to_replace, substitute) - elif vowels_pattern is not None: - bunnified_name = vowels_pattern + bunnified_name = username.replace(word_to_replace, substitute) + elif vowels_in_name is not None: + bunnified_name = vowels_in_name elif unmatched_name: bunnified_name = unmatched_name |