11import re
22import requests
3+ import uuid
34
45from cloudbot import hook
56
@@ -12,46 +13,47 @@ def get_name(uuid):
1213 # submit the profile request
1314 request = requests .get (UUID_API .format (uuid ))
1415 data = request .json ()
15- return data [uuid ] or None
16+ return data [uuid ]
1617
1718
1819@hook .command ("mcuser" , "mcpaid" , "haspaid" )
1920def mcuser (text , bot ):
2021 """<username> - gets information about the Minecraft user <account>"""
2122 headers = {'User-Agent' : bot .user_agent }
22- user = text .strip ()
23+ text = text .strip ()
2324
24- cleaned = user .replace ('-' , '' )
25- if re .search (r'[0-9a-f]{32}\Z' , cleaned , re .I ):
25+ # check if we are looking up a UUID
26+ cleaned = text .replace ('-' , '' )
27+ if re .search (r'^[0-9a-f]{32}\Z$' , cleaned , re .I ):
28+ # we are looking up a UUID, get a name.
2629 try :
2730 name = get_name (cleaned )
28- except (requests .exceptions .HTTPError , requests .exceptions .ConnectionError ) as e :
31+ except (requests .exceptions .HTTPError , requests .exceptions .ConnectionError , KeyError ) as e :
2932 return "Could not get username from UUID: {}" .format (e )
3033 else :
31- name = user
34+ name = text
3235
33- if not name :
34- return "The account \x02 {}\x02 does not exist." .format (user )
35-
36- # submit the profile request
36+ # get user data from fishbans
3737 try :
3838 request = requests .get (HIST_API .format (requests .utils .quote (name )), headers = headers )
3939 except (requests .exceptions .HTTPError , requests .exceptions .ConnectionError ) as e :
4040 return "Could not get profile status: {}" .format (e )
4141
42- # get the fishbans data
42+ # read the fishbans data
4343 try :
4444 results = request .json ()
4545 except ValueError :
4646 return "Could not parse profile status"
4747
48- # handle errors
48+ # check for errors from fishbans and handle them
4949 if not results ['success' ]:
5050 if results ['error' ] == "User is not premium." :
51- return "The account \x02 {}\x02 is not premium or does not exist." .format (user )
51+ return "The account \x02 {}\x02 is not premium or does not exist." .format (text )
5252 else :
5353 return results ['error' ]
54- user = results ["data" ]
5554
56- return 'The account \x02 {username}\x02 ({uuid}) exists. It is a \x02 paid\x02 ' \
57- ' account.' .format (** user )
55+ username = results ['data' ]['username' ]
56+ id = uuid .UUID (results ['data' ]['uuid' ])
57+
58+ return 'The account \x02 {}\x02 ({}) exists. It is a \x02 paid\x02 ' \
59+ ' account.' .format (username , id )
0 commit comments