Skip to content

Commit

Permalink
Added get_user_by_username + more
Browse files Browse the repository at this point in the history
  • Loading branch information
jmkd3v committed Jan 4, 2021
1 parent 522f616 commit c372006
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
26 changes: 26 additions & 0 deletions examples/username.py
Original file line number Diff line number Diff line change
@@ -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()
33 changes: 33 additions & 0 deletions ro_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
19 changes: 12 additions & 7 deletions ro_py/robloxdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"]
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions ro_py/utilities/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

0 comments on commit c372006

Please sign in to comment.