77
88from bot .config import Config
99from bot .data import command
10- from bot .data import esc
1110from bot .data import format_msg
1211from bot .message import Message
1312
@@ -27,33 +26,61 @@ class PronounData(TypedDict):
2726
2827
2928async def _get_user_data (username : str ) -> UserData | None :
30- url = f' https://api.pronouns.alejo.io/v1/users/{ username } '
29+ url = f" https://api.pronouns.alejo.io/v1/users/{ username } "
3130
3231 async with aiohttp .ClientSession () as session :
3332 async with session .get (url ) as resp :
3433 if resp .status != 200 :
3534 return None
3635
37- return ( await resp .json () )
36+ return await resp .json ()
3837
3938
4039@async_lru .alru_cache (maxsize = 1 )
4140async def pronouns () -> dict [str , PronounData ]:
41+ """
42+ Database of all pronouns, with their various forms.
43+ """
44+
4245 url = 'https://api.pronouns.alejo.io/v1/pronouns/'
4346
4447 async with aiohttp .ClientSession () as session :
4548 async with session .get (url ) as resp :
46- return ( await resp .json () )
49+ return await resp .json ()
4750
4851
4952async def _get_user_pronouns (username : str ) -> tuple [str , str ] | None :
53+ """
54+ Get the pronouns of the user given their `username`.
55+
56+ The returned value is a pair `(main subject/alt subject)`
57+ if the user has alternative pronouns,
58+ `(main subject/main object)` if they don't, and `None` if
59+ their username is not known to the pronouns service.
60+
61+ Note: pronouns are in English, and put in lowercase.
62+ """
63+
5064 user_data = await _get_user_data (username )
5165
5266 if user_data is None :
5367 return None
5468
55- pronoun_data = (await pronouns ())[user_data ['pronoun_id' ]]
56- return (pronoun_data ['subject' ], pronoun_data ['object' ])
69+ all_pronouns = await pronouns ()
70+ main_pronoun_data = all_pronouns [user_data ['pronoun_id' ]]
71+ maybe_alt_pronoun_id = user_data ['alt_pronoun_id' ]
72+
73+ # first is always main subject
74+ first = main_pronoun_data ['subject' ].casefold ()
75+
76+ if maybe_alt_pronoun_id is not None :
77+ # second is alt subject if they have alt pronouns
78+ second = all_pronouns [maybe_alt_pronoun_id ]['subject' ].casefold ()
79+ else :
80+ # second is main object if they don't
81+ second = main_pronoun_data ['object' ].casefold ()
82+
83+ return (first , second )
5784
5885
5986@command ('!pronouns' )
@@ -63,7 +90,7 @@ async def cmd_pronouns(config: Config, msg: Message) -> str:
6390 pronouns = await _get_user_pronouns (username )
6491
6592 if pronouns is None :
66- return format_msg (msg , f' user not found { esc ( username ) } ' )
93+ return format_msg (msg , f" user not found: { username } " )
6794
6895 (subj , obj ) = pronouns
69- return format_msg (msg , f' { username } \ ' s pronouns are: { subj } /{ obj } ' )
96+ return format_msg (msg , f" { username } 's pronouns are: { subj } /{ obj } " )
0 commit comments