From d611f5472134a9882396901f4e0e69102390de79 Mon Sep 17 00:00:00 2001 From: glowingrunes Date: Sun, 28 Apr 2019 16:27:53 +0100 Subject: Created a JSON file to store the bunny names. --- bot/resources/easter/bunny_names.json | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 bot/resources/easter/bunny_names.json (limited to 'bot') diff --git a/bot/resources/easter/bunny_names.json b/bot/resources/easter/bunny_names.json new file mode 100644 index 00000000..8c97169c --- /dev/null +++ b/bot/resources/easter/bunny_names.json @@ -0,0 +1,29 @@ +{ + "names": [ + "Flopsy", + "Hopsalot", + "Thumper", + "Nibbles", + "Daisy", + "Fuzzy", + "Cottontail", + "Carrot Top", + "Marshmallow", + "Lucky", + "Clover", + "Daffodil", + "Buttercup", + "Goldie", + "Dizzy", + "Trixie", + "Snuffles", + "Hopscotch", + "Skipper", + "Thunderfoot", + "Bigwig", + "Dandelion", + "Pipkin", + "Buckthorn", + "Skipper" + ] +} -- cgit v1.2.3 From e76c79d68829d3ff7401e0b02acdd80f9616abfd Mon Sep 17 00:00:00 2001 From: glowingrunes Date: Sun, 28 Apr 2019 16:32:42 +0100 Subject: Finished the random bunny name generator command. --- bot/seasons/easter/bunny_name_generator.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 bot/seasons/easter/bunny_name_generator.py (limited to 'bot') diff --git a/bot/seasons/easter/bunny_name_generator.py b/bot/seasons/easter/bunny_name_generator.py new file mode 100644 index 00000000..b2bfb893 --- /dev/null +++ b/bot/seasons/easter/bunny_name_generator.py @@ -0,0 +1,25 @@ +import random +import json +import logging + +from discord.ext import commands +from pathlib import Path + + +log = logging.getLogger(__name__) + +with open(Path("bot", "resources", "easter", "bunny_names.json"), "r", encoding="utf8") as f: + BUNNY_NAMES = json.load(f) + + +class BunnyNameGenerator(commands.Cog): + """Generate a random bunny name, or bunnify your Discord username!""" + + def __init__(self, bot): + self.bot = bot + + @commands.command() + async def bunnyname(self, ctx): + """Picks a random bunny name from a JSON file""" + + await ctx.send(random.choice(BUNNY_NAMES["names"])) -- cgit v1.2.3 From e71ef5adf6769a79577b2ba472b423a7a8224694 Mon Sep 17 00:00:00 2001 From: glowingrunes Date: Sun, 28 Apr 2019 16:34:55 +0100 Subject: Completed the 'bunnifyme' command. --- bot/seasons/easter/bunny_name_generator.py | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'bot') diff --git a/bot/seasons/easter/bunny_name_generator.py b/bot/seasons/easter/bunny_name_generator.py index b2bfb893..918e11d4 100644 --- a/bot/seasons/easter/bunny_name_generator.py +++ b/bot/seasons/easter/bunny_name_generator.py @@ -1,6 +1,7 @@ import random import json import logging +import re from discord.ext import commands from pathlib import Path @@ -23,3 +24,99 @@ class BunnyNameGenerator(commands.Cog): """Picks a random bunny name from a JSON file""" await ctx.send(random.choice(BUNNY_NAMES["names"])) + + @commands.command() + 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): + """ + 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 + + print(new_name) + + 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? + + vowels_pattern = find_vowels(username_filtered) # Does the name contain vowels? + # If so, does it match any of the patterns in this function? + + unmatched_name = append_name(username_filtered) # Default if name doesn't match the above patterns + + if spaces_pattern is not None: + replacements = ['Cotton', 'Fluff', 'Floof' 'Bounce', 'Snuffle', 'Nibble', 'Cuddle', 'Velvetpaw', 'Carrot'] + word_to_replace = random.choice(spaces_pattern) + substitute = random.choice(replacements) + bunnified_name = username_filtered.replace(word_to_replace, substitute) + elif vowels_pattern is not None: + bunnified_name = vowels_pattern + elif unmatched_name: + bunnified_name = unmatched_name + + await ctx.send(bunnified_name) + + +def setup(bot): + """Cog load.""" + + bot.add_cog(BunnyNameGenerator(bot)) + log.info("BunnyNameGenerator cog loaded.") -- cgit v1.2.3 From 9d2fb1b1fdd85e22aaf88f8306524b02d5676803 Mon Sep 17 00:00:00 2001 From: glowingrunes Date: Sun, 28 Apr 2019 16:37:21 +0100 Subject: Removed code that was added for debugging purposes. --- bot/seasons/easter/bunny_name_generator.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'bot') diff --git a/bot/seasons/easter/bunny_name_generator.py b/bot/seasons/easter/bunny_name_generator.py index 918e11d4..df1f0e28 100644 --- a/bot/seasons/easter/bunny_name_generator.py +++ b/bot/seasons/easter/bunny_name_generator.py @@ -79,8 +79,6 @@ class BunnyNameGenerator(commands.Cog): if option5 != displayname: new_name = option5 - print(new_name) - return new_name def append_name(displayname): -- cgit v1.2.3 From c537f29473a2ae8e1b8da0f3e1ffa6929d22f7be Mon Sep 17 00:00:00 2001 From: glowingrunes Date: Sun, 28 Apr 2019 17:33:22 +0100 Subject: Fixed linting errors. --- bot/seasons/easter/bunny_name_generator.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'bot') diff --git a/bot/seasons/easter/bunny_name_generator.py b/bot/seasons/easter/bunny_name_generator.py index df1f0e28..7d40e219 100644 --- a/bot/seasons/easter/bunny_name_generator.py +++ b/bot/seasons/easter/bunny_name_generator.py @@ -1,11 +1,10 @@ -import random import json import logging +import random import re - -from discord.ext import commands from pathlib import Path +from discord.ext import commands log = logging.getLogger(__name__) @@ -41,8 +40,8 @@ class BunnyNameGenerator(commands.Cog): def find_spaces(displayname): """ - Check if Discord name contains spaces so we can bunnify - an individual word in the name. + 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. """ @@ -55,6 +54,8 @@ class BunnyNameGenerator(commands.Cog): 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. -- cgit v1.2.3 From a0adcd7956f5cbf1734e39b37930f8c30f6292a0 Mon Sep 17 00:00:00 2001 From: glowingrunes Date: Fri, 3 May 2019 21:30:48 +0100 Subject: Combined filter_name and find_spaces into one method. Renamed variables to be more Pythonic, and improved the comments. --- bot/seasons/easter/bunny_name_generator.py | 133 +++++++++++++---------------- 1 file changed, 60 insertions(+), 73 deletions(-) (limited to 'bot') 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 -- cgit v1.2.3 From eece8c93fcfdd2b5c82ca87a2d4f15eec9de99ba Mon Sep 17 00:00:00 2001 From: glowingrunes Date: Fri, 3 May 2019 21:35:28 +0100 Subject: Fixed linting error. --- bot/seasons/easter/bunny_name_generator.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/seasons/easter/bunny_name_generator.py b/bot/seasons/easter/bunny_name_generator.py index f8d0eea3..5dcbe62f 100644 --- a/bot/seasons/easter/bunny_name_generator.py +++ b/bot/seasons/easter/bunny_name_generator.py @@ -19,9 +19,7 @@ class BunnyNameGenerator(commands.Cog): self.bot = bot def find_separators(self, displayname): - """ - Check if Discord name contains spaces so we can bunnify an individual word in the name. - """ + """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: -- cgit v1.2.3