Improved error handling for MC username lookups

doc_update
Joey Hines 2018-08-05 17:51:55 -05:00
parent ccde543308
commit 311b94a1c8
3 changed files with 23 additions and 10 deletions

View File

@ -44,7 +44,8 @@ async def on_command_error(error, ctx):
elif isinstance(error.original, NoPermissionError):
error_str = 'You don\'t have permission for that cool command.'
elif isinstance(error.original, UsernameLookupFailed):
error_str = error.original.__doc__
error_str = 'Your user name was not found, either Mojang is having a fucky wucky ' \
'or your nickname is not set correctly. *stares at the Mods*'
elif isinstance(error.original, PlayerNotFound):
error_str = 'Make sure to use ?register first you ding dong.'
elif isinstance(error.original, EntryNameNotUniqueError):
@ -89,8 +90,9 @@ if __name__ == '__main__':
for extension in extensions:
try:
bot.load_extension(extension)
except Exception as e:
except:
print('Failed to load extension {}'.format(extension))
update_user_names(bot_commands)
bot.run(TOKEN)

View File

@ -8,20 +8,27 @@ username_lookup_url = 'https://api.mojang.com/user/profiles/{}/names'
def grab_json(url):
return requests.get(url).json()
def grab_UUID(username):
try:
player_data = grab_json(uuid_lookup_url.format(username))
return player_data['id']
json = requests.get(url).json()
if 'error' in json:
raise UsernameLookupFailed
except JSONDecodeError:
raise UsernameLookupFailed
return json
def grab_UUID(username):
player_data = grab_json(uuid_lookup_url.format(username))
return player_data['id']
def grab_playername(uuid):
player_data = grab_json(username_lookup_url.format(uuid))
last_index = len(player_data)-1
if len(player_data) == 0:
raise UsernameLookupFailed
else:
last_index = len(player_data)-1
return player_data[last_index]['name']

View File

@ -1,5 +1,6 @@
from unittest import TestCase
from MinecraftAccountInfoGrabber import *
from BotErrors import UsernameLookupFailed
class TestMinecraftInfoGrabber(TestCase):
@ -12,3 +13,6 @@ class TestMinecraftInfoGrabber(TestCase):
def test_grab_playername_wrong_case(self):
self.assertEqual(grab_UUID('zerohd'), 'fe7e84132570458892032b69ff188bc3')
def test_grab_invalid_player(self):
self.assertRaises(UsernameLookupFailed, grab_UUID, 'lsdlkjsljglfjgldkj')