diff options
author | 2019-11-30 14:42:16 -0500 | |
---|---|---|
committer | 2019-11-30 14:42:50 -0500 | |
commit | 5c7e2385a71a1560376ce6d8e1774a8ac7652670 (patch) | |
tree | 6df0faa295168b41c474cecd4668f0aa4de271da | |
parent | Readd cog init & rename *.py file (diff) |
Refactor random generator to use a local instance rather than system
-rw-r--r-- | bot/seasons/halloween/monsterbio.py | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/bot/seasons/halloween/monsterbio.py b/bot/seasons/halloween/monsterbio.py index d84498a8..bfa8a026 100644 --- a/bot/seasons/halloween/monsterbio.py +++ b/bot/seasons/halloween/monsterbio.py @@ -20,29 +20,33 @@ class MonsterBio(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - def generate_name(self, length: int) -> str: + def generate_name(self, seeded_random: random.Random) -> str: """Generates a name (for either monster species or monster name).""" - return "".join([random.choice(TEXT_OPTIONS["monster_type"][i]) for i in range(length)]) + n_candidate_strings = seeded_random.randint(2, len(TEXT_OPTIONS["monster_type"])) + return "".join(seeded_random.choice(TEXT_OPTIONS["monster_type"][i]) for i in range(n_candidate_strings)) @commands.command(brief="Sends your monster bio!") async def monsterbio(self, ctx: commands.Context) -> None: """Sends a description of a monster.""" - random.seed(ctx.message.author.id) - name = self.generate_name(random.randint(2, len(TEXT_OPTIONS["monster_type"]))) - species = self.generate_name(random.randint(2, len(TEXT_OPTIONS["monster_type"]))) - biography_text = random.choice(TEXT_OPTIONS["biography_text"]) + seeded_random = random.Random(ctx.message.author.id) # Seed a local Random instance rather than the system one + + name = self.generate_name(seeded_random) + species = self.generate_name(seeded_random) + biography_text = seeded_random.choice(TEXT_OPTIONS["biography_text"]) words = {"monster_name": name, "monster_species": species} for key, value in biography_text.items(): if key == "text": continue - if value > 1: - words[key] = random.sample(TEXT_OPTIONS[key], value) - else: - words[key] = random.choice(TEXT_OPTIONS[key]) + + options = seeded_random.sample(TEXT_OPTIONS[key], value) + words[key] = ' '.join(options) + embed = discord.Embed( title=f"{name}'s Biography", - color=random.choice([Colours.orange, Colours.purple]), description=biography_text["text"].format(**words) + color=seeded_random.choice([Colours.orange, Colours.purple]), + description=biography_text["text"].format_map(words), ) + await ctx.send(embed=embed) |