Skip to content

Commit

Permalink
feat(misc): add random name generator /name_gen (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
natelandau authored Oct 2, 2023
1 parent 4e38ec4 commit 1191003
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/valentina/cogs/developer.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ async def create_test_characters(
else:
vampire_clan = None

first_name, last_name = await fetch_random_name()
name = await fetch_random_name()

# Create the character
data: dict[str, str | int | bool] = {
"first_name": first_name,
"last_name": last_name,
"first_name": name[0][0],
"last_name": name[0][1],
"nickname": char_class.name,
"developer_character": True,
"player_character": True,
Expand Down
44 changes: 43 additions & 1 deletion src/valentina/cogs/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from valentina.models.db_tables import Character, Macro
from valentina.utils import errors
from valentina.utils.changelog_parser import ChangelogParser
from valentina.utils.options import select_changelog_version_1, select_changelog_version_2
from valentina.utils.helpers import fetch_random_name
from valentina.utils.options import (
select_changelog_version_1,
select_changelog_version_2,
select_country,
)

p = inflect.engine()

Expand Down Expand Up @@ -269,6 +274,43 @@ async def coinflip(self, ctx: discord.ApplicationContext) -> None:
f"**{ctx.author.name}** flipped a coin and got **{random.choice(coin_sides)}**!"
)

@commands.slash_command(name="name_generator", help="Generate a random name")
async def name_gen(
self,
ctx: discord.ApplicationContext,
gender: Option(
str,
name="gender",
description="The character's gender",
choices=["male", "female"],
required=True,
),
country: Option(
str,
name="country",
description="The country for the character's name (default 'US')",
autocomplete=select_country,
default="us",
),
number: Option(
int, name="number", description="The number of names to generate (default 5)", default=5
),
) -> None:
"""Generate a random name."""
name_list = [
f"- {name[0].title()} {name[1].title()}\n"
for name in await fetch_random_name(gender=gender, country=country, results=number)
]

await ctx.respond(
embed=discord.Embed(
title="Random Name Generator",
description=f"Here are some random names for you, {ctx.author.mention}!\n{''.join(name_list)}",
color=EmbedColor.INFO.value,
),
ephemeral=True,
)


def setup(bot: Valentina) -> None:
"""Add the cog to the bot."""
Expand Down
6 changes: 3 additions & 3 deletions src/valentina/cogs/storyteller.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,14 @@ async def create_rng_char(
),
) -> None:
"""Create a new storyteller character."""
first_name, last_name = await fetch_random_name(gender=gender, country=name_type)
name = await fetch_random_name(gender=gender, country=name_type)

if char_class.name.lower() == "vampire" and not vampire_clan:
vampire_clan = VampireClan.select().order_by(fn.Random()).limit(1)[0]

data = {
"first_name": first_name,
"last_name": last_name,
"first_name": name[0][0],
"last_name": name[0][1],
"nickname": char_class.name,
"storyteller_character": True,
"player_character": False,
Expand Down
20 changes: 15 additions & 5 deletions src/valentina/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,29 @@ def fetch_clan_disciplines(clan: str) -> list[str]:
return CLAN_DISCIPLINES[clan.title()]


async def fetch_random_name(gender: str | None = None, country: str = "us") -> tuple[str, str]:
"""Fetch a random name from the randomuser.me API."""
async def fetch_random_name(
gender: str | None = None, country: str = "us", results: int = 1
) -> list[tuple[str, str]]:
"""Fetch a random name from the randomuser.me API.
Args:
country (str, optional): The country to fetch the name from. Defaults to "us".
results (int, optional): The number of results to fetch. Defaults to 1.
gender (str, optional): The gender of the name to fetch. Defaults to None
"""
if not gender:
gender = random.choice(["male", "female"])

params = {"gender": gender, "nat": country, "inc": "name"}
params = {"gender": gender, "nat": country, "inc": "name", "results": results}
url = f"https://randomuser.me/api/?{urlencode(params)}"

async with ClientSession() as session, session.get(url) as res:
if 300 > res.status >= 200: # noqa: PLR2004
data = await res.json()
return (data["results"][0]["name"]["first"], data["results"][0]["name"]["last"])
return [(result["name"]["first"], result["name"]["last"]) for result in data["results"]]

return ("John", "Doe")
return [("John", "Doe")]


def get_max_trait_value(trait: str, category: str, is_custom_trait: bool = False) -> int | None:
Expand Down

0 comments on commit 1191003

Please sign in to comment.