Improved error handling for MC username lookups
parent
ccde543308
commit
311b94a1c8
|
@ -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):
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -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))
|
||||||
|
|
||||||
|
if len(player_data) == 0:
|
||||||
|
raise UsernameLookupFailed
|
||||||
|
else:
|
||||||
last_index = len(player_data)-1
|
last_index = len(player_data)-1
|
||||||
|
|
||||||
return player_data[last_index]['name']
|
return player_data[last_index]['name']
|
||||||
|
|
||||||
|
|
|
@ -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')
|
||||||
|
|
Loading…
Reference in New Issue