diff options
author | 2021-05-17 12:50:04 -0500 | |
---|---|---|
committer | 2021-05-17 12:50:04 -0500 | |
commit | a174b78114d4204e74e11a820ddf36f00d293112 (patch) | |
tree | 9404dbf50e3f11c7c83b782733aa30d7d15fcae2 /bot/exts/valentines/myvalenstate.py | |
parent | Remove comments and update docstrings (diff) | |
parent | Merge pull request #726 from Objectivitix/main (diff) |
Merge branch 'main' into http_status_command_randomness
Diffstat (limited to 'bot/exts/valentines/myvalenstate.py')
-rw-r--r-- | bot/exts/valentines/myvalenstate.py | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/bot/exts/valentines/myvalenstate.py b/bot/exts/valentines/myvalenstate.py index 01801847..52a61011 100644 --- a/bot/exts/valentines/myvalenstate.py +++ b/bot/exts/valentines/myvalenstate.py @@ -7,20 +7,17 @@ from random import choice import discord from discord.ext import commands +from bot.bot import Bot from bot.constants import Colours log = logging.getLogger(__name__) -with open(Path("bot/resources/valentines/valenstates.json"), "r", encoding="utf8") as file: - STATES = json.load(file) +STATES = json.loads(Path("bot/resources/valentines/valenstates.json").read_text("utf8")) class MyValenstate(commands.Cog): """A Cog to find your most likely Valentine's vacation destination.""" - def __init__(self, bot: commands.Bot): - self.bot = bot - def levenshtein(self, source: str, goal: str) -> int: """Calculates the Levenshtein Distance between source and goal.""" if len(source) < len(goal): @@ -46,12 +43,12 @@ class MyValenstate(commands.Cog): """Find the vacation spot(s) with the most matching characters to the invoking user.""" eq_chars = collections.defaultdict(int) if name is None: - author = ctx.message.author.name.lower().replace(' ', '') + author = ctx.author.name.lower().replace(" ", "") else: - author = name.lower().replace(' ', '') + author = name.lower().replace(" ", "") for state in STATES.keys(): - lower_state = state.lower().replace(' ', '') + lower_state = state.lower().replace(" ", "") eq_chars[state] = self.levenshtein(author, lower_state) matches = [x for x, y in eq_chars.items() if y == min(eq_chars.values())] @@ -60,27 +57,26 @@ class MyValenstate(commands.Cog): embed_title = "But there are more!" if len(matches) > 1: - leftovers = f"{', '.join(matches[:len(matches)-2])}, and {matches[len(matches)-1]}" + leftovers = f"{', '.join(matches[:-2])}, and {matches[-1]}" embed_text = f"You have {len(matches)} more matches, these being {leftovers}." elif len(matches) == 1: embed_title = "But there's another one!" - leftovers = str(matches) - embed_text = f"You have another match, this being {leftovers}." + embed_text = f"You have another match, this being {matches[0]}." else: embed_title = "You have a true match!" embed_text = "This state is your true Valenstate! There are no states that would suit" \ " you better" embed = discord.Embed( - title=f'Your Valenstate is {valenstate} \u2764', - description=f'{STATES[valenstate]["text"]}', + title=f"Your Valenstate is {valenstate} \u2764", + description=STATES[valenstate]["text"], colour=Colours.pink ) embed.add_field(name=embed_title, value=embed_text) embed.set_image(url=STATES[valenstate]["flag"]) - await ctx.channel.send(embed=embed) + await ctx.send(embed=embed) -def setup(bot: commands.Bot) -> None: - """Valenstate Cog load.""" - bot.add_cog(MyValenstate(bot)) +def setup(bot: Bot) -> None: + """Load the Valenstate Cog.""" + bot.add_cog(MyValenstate()) |