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): elif isinstance(error.original, NoPermissionError):
error_str = 'You don\'t have permission for that cool command.' error_str = 'You don\'t have permission for that cool command.'
elif isinstance(error.original, UsernameLookupFailed): 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): elif isinstance(error.original, PlayerNotFound):
error_str = 'Make sure to use ?register first you ding dong.' error_str = 'Make sure to use ?register first you ding dong.'
elif isinstance(error.original, EntryNameNotUniqueError): elif isinstance(error.original, EntryNameNotUniqueError):
@ -64,7 +65,7 @@ def update_user_names(bot_commands):
session = bot_commands.interface.database.Session() session = bot_commands.interface.database.Session()
print("Updating MC usernames...") print("Updating MC usernames...")
player_list = session.query(Player).all() player_list = session.query(Player).all()
for player in player_list: for player in player_list:
player.name = grab_playername(player.mc_uuid) player.name = grab_playername(player.mc_uuid)
@ -89,8 +90,9 @@ if __name__ == '__main__':
for extension in extensions: for extension in extensions:
try: try:
bot.load_extension(extension) bot.load_extension(extension)
except Exception as e: except:
print('Failed to load extension {}'.format(extension)) print('Failed to load extension {}'.format(extension))
update_user_names(bot_commands) update_user_names(bot_commands)
bot.run(TOKEN) bot.run(TOKEN)

View File

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

View File

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