From 3026b240f4037fdd886824982e140fa76e34f1bf Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Fri, 21 Sep 2018 07:27:32 -0500 Subject: [PATCH] Added better error checking for find_player and add_player --- geoffrey/Commands.py | 6 +++--- geoffrey/cogs/Admin_Commands.py | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/geoffrey/Commands.py b/geoffrey/Commands.py index 4c35440..8f678e6 100644 --- a/geoffrey/Commands.py +++ b/geoffrey/Commands.py @@ -377,18 +377,18 @@ class Commands: try: self.interface.find_player_by_discord_uuid(session, discord_uuid) - out = "is already in database." + raise PlayerInDBError except PlayerNotFound: player = Player(mc_name, discord_id=discord_uuid) self.interface.database.add_object(session, player) player = self.interface.find_player_by_discord_uuid(session, discord_uuid) - out = "has been added to the database with id {}".format(player.id) + id = player.id finally: session.close() - return out + return id def find_player(self, discord_uuid): session = self.interface.database.Session() diff --git a/geoffrey/cogs/Admin_Commands.py b/geoffrey/cogs/Admin_Commands.py index 5dcaae4..0b90c82 100644 --- a/geoffrey/cogs/Admin_Commands.py +++ b/geoffrey/cogs/Admin_Commands.py @@ -134,9 +134,12 @@ class Admin_Commands: """ Manually add a player to the database """ - str = self.bot.bot_commands.add_player(discord_uuid, mc_name) - await ctx.send('{}, user **{}** {}.' - .format(ctx.message.author.mention, mc_name, str)) + try: + db_id = self.bot.bot_commands.add_player(discord_uuid, mc_name) + await ctx.send('{}, user **{}** been added to the data base with id {}.'.format(ctx.message.author.mention, + mc_name, db_id)) + except PlayerInDBError: + await ctx.send('{}, user **{}** is already in the database.'.format(ctx.message.author.mention, mc_name)) @add_player.error async def add_player_error(self, ctx, error): @@ -147,9 +150,12 @@ class Admin_Commands: """ Finds a player in the database """ - id, username, discord_uuid, minecraft_uuid = self.bot.bot_commands.find_player(discord_uuid) - await ctx.send('Username: {}, id: {}, Discord UUID: {}, Minecraft UUID: {}' - .format(username, id, discord_uuid, minecraft_uuid)) + try: + db_id, username, discord_uuid, minecraft_uuid = self.bot.bot_commands.find_player(discord_uuid) + await ctx.send('Username: {}, id: {}, Discord UUID: {}, Minecraft UUID: {}' + .format(username, db_id, discord_uuid, minecraft_uuid)) + except PlayerNotFound: + await ctx.send('That player is not in the database...') @find_player.error async def find_player_error(self, ctx, error):