From c37200645a5643b763a74d44116e1f69a7aca776 Mon Sep 17 00:00:00 2001 From: jmkdev Date: Sun, 3 Jan 2021 21:30:15 -0500 Subject: [PATCH] Added get_user_by_username + more --- examples/username.py | 26 ++++++++++++++++++++++++++ ro_py/client.py | 33 +++++++++++++++++++++++++++++++++ ro_py/robloxdocs.py | 19 ++++++++++++------- ro_py/utilities/errors.py | 4 ++++ 4 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 examples/username.py diff --git a/examples/username.py b/examples/username.py new file mode 100644 index 00000000..131c2fd5 --- /dev/null +++ b/examples/username.py @@ -0,0 +1,26 @@ +from ro_py.client import Client +import asyncio + +client = Client() + +user_name = "JMK_RBXDev" + + +async def grab_info(): + print(f"Loading user {user_name}...") + user = await client.get_user_by_username(user_name) + print("Loaded user.") + + print(f"Username: {user.name}") + print(f"Display Name: {user.display_name}") + print(f"Description: {user.description}") + print(f"Status: {await user.get_status() or 'None.'}") + + +def main(): + loop = asyncio.get_event_loop() + loop.run_until_complete(grab_info()) + + +if __name__ == '__main__': + main() diff --git a/ro_py/client.py b/ro_py/client.py index 0c454e35..162b25db 100644 --- a/ro_py/client.py +++ b/ro_py/client.py @@ -15,6 +15,7 @@ from ro_py.utilities.requests import Requests from ro_py.accountsettings import AccountSettings from ro_py.accountinformation import AccountInformation +from ro_py.utilities.errors import UserDoesNotExistError import logging @@ -79,6 +80,38 @@ async def get_user(self, user_id): await user.update() return user + async def get_user_by_username(self, user_name: str, exclude_banned_users: bool = False): + """ + Gets a Roblox user by their username.. + + Parameters + ---------- + user_name : str + Name of the user to generate the object from. + exclude_banned_users : bool + Whether to exclude banned users in the request. + """ + username_req = await self.requests.post( + url="https://users.roblox.com/v1/usernames/users", + data={ + "usernames": [ + user_name + ], + "excludeBannedUsers": exclude_banned_users + } + ) + username_data = username_req.json() + if len(username_data["data"]) > 0: + user_id = username_req.json()["data"][0]["id"] # TODO: make this a partialuser + user = self.requests.cache.get(CacheType.Users, user_id) + if not user: + user = User(self.requests, user_id) + self.requests.cache.set(CacheType.Users, user_id, user) + await user.update() + return user + else: + raise UserDoesNotExistError + async def get_group(self, group_id): """ Gets a Roblox group. diff --git a/ro_py/robloxdocs.py b/ro_py/robloxdocs.py index bec80513..249a7815 100644 --- a/ro_py/robloxdocs.py +++ b/ro_py/robloxdocs.py @@ -37,7 +37,10 @@ def __init__(self, data): self.description = None self.required = data["required"] - self.type = data["type"] # TODO: actually convert this to python types + self.type = None + + if "type" in data: + self.type = data["type"] if "format" in data: self.format = data["format"] @@ -48,12 +51,14 @@ def __init__(self, data): class EndpointDocsPathRequestType: def __init__(self, data): self.tags = data["tags"] - self.summary = data["summary"] + self.description = None + self.summary = None + + if "summary" in data: + self.summary = data["summary"] if "description" in data: self.description = data["description"] - else: - self.description = None self.consumes = data["consumes"] self.produces = data["produces"] @@ -68,9 +73,9 @@ def __init__(self, data): class EndpointDocsPath: def __init__(self, data): - self.data = data - for type_k, type_v in self.data.items(): - setattr(self, type_k, EndpointDocsPathRequestType(type_v)) + self.data = {} + for type_k, type_v in data.items(): + self.data[type_k] = EndpointDocsPathRequestType(type_v) class EndpointDocsDataInfo: diff --git a/ro_py/utilities/errors.py b/ro_py/utilities/errors.py index 91ad4d3a..9229bf56 100644 --- a/ro_py/utilities/errors.py +++ b/ro_py/utilities/errors.py @@ -33,3 +33,7 @@ class ChatError(Exception): class InvalidPageError(Exception): """Called when an invalid page is requested.""" + + +class UserDoesNotExistError(Exception): + """Called when a user does not exist."""