Improved Error Handling

doc_update
Joey Hines 2018-06-30 10:07:56 -05:00
parent b4459a529a
commit 77ded8497e
5 changed files with 152 additions and 60 deletions

View File

@ -16,3 +16,7 @@ class DeleteEntryError(DataBaseError) :
class UsernameLookupFailed(Exception):
'''Error in username lookup, is the player's nickname set correctly? *stares at aeskdar*'''
class PlayerNotFound(DataBaseError):
'''Player not found in database.'''

View File

@ -2,7 +2,7 @@ from sqlalchemy import Column, Integer, String, ForeignKey, Enum
import enum
from sqlalchemy.ext.declarative import declarative_base
from BotErrors import *
from sqlalchemy import create_engine, exists
from sqlalchemy import create_engine, exists, func
from sqlalchemy.orm import sessionmaker, relationship
import sqlalchemy
from MinecraftAccountInfoGrabber import *
@ -18,12 +18,11 @@ class GeoffreyDatabase:
self.session = Session()
SQL_Base.metadata.create_all(self.engine)
def add_base(self, player_name, name, x_pos, y_pos, z_pos, args):
def add_location(self, player_name, name, x_pos, y_pos, z_pos, args):
owner = self.add_player(player_name)
base = Location(name, x_pos, y_pos, z_pos, owner, args)
self.add_object(base)
return base
location = Location(name, x_pos, y_pos, z_pos, owner, args)
self.add_object(location)
return location
def add_shop(self, player_name, name, x_pos, y_pos, z_pos, args):
owner = self.add_player(player_name)
@ -32,29 +31,30 @@ class GeoffreyDatabase:
return shop
def add_item(self, player_name, shop_name, item_name, price):
try:
shop = self.find_location_by_name_and_owner(player_name, shop_name)
item = ItemListing(item_name, price, shop[0])
except IndexError:
raise LocationLookUpError
return item
def add_player(self, player_name):
expr = Player.name == player_name
player_list = self.query_by_filter(Player, expr)
if len(player_list) == 0:
try:
player = self.find_player(player_name)
except PlayerNotFound:
try:
uuid = grab_UUID(player_name)
expr = Player.id == uuid
player_list = self.query_by_filter(Player, expr)
if len(player_list) == 0:
player = self.find_player_by_uuid(uuid)
except PlayerNotFound:
player = Player(player_name)
self.add_object(player)
else:
player_list[0].name = player_name
else:
player = player_list[0]
finally:
player.name = player_name
self.session.commit()
return player
def add_object(self, obj):
@ -64,12 +64,12 @@ class GeoffreyDatabase:
self.session.commit()
def find_location_by_owner(self, owner_name):
player = self.add_player(owner_name)
player = self.find_player(owner_name)
expr = Location.owner == player
return self.query_by_filter(Location, expr)
def find_location_by_name_and_owner(self, owner_name, name):
player = self.add_player(owner_name)
player = self.find_player(owner_name)
expr = (Location.owner == player) & (Location.name == name)
return self.query_by_filter(Location, expr)
@ -92,12 +92,32 @@ class GeoffreyDatabase:
return shops
def find_player(self, player_name):
expr = func.lower(Player.name) == func.lower(player_name)
try:
player = self.query_by_filter(Player, expr)[0]
except IndexError:
raise PlayerNotFound
return player
def find_player_by_uuid(self, uuid):
expr = Player.id == uuid
try:
player = self.query_by_filter(Player, expr)[0]
except IndexError:
raise PlayerNotFound
return player
def query_by_filter(self, obj_type, * args):
filter_value = self.combine_filter(args)
return self.session.query(obj_type).filter(filter_value).all()
def delete_base(self, player_name, base_name):
player = self.add_player(player_name)
player = self.find_player(player_name)
expr = (Location.owner == player) & (Location.name == base_name)
self.delete_entry(Location, expr)
@ -152,9 +172,6 @@ class Player(SQL_Base):
locations = relationship("Location", back_populates="owner", lazy='dynamic')
def __init__(self, name):
if name == 'dootb.in ꙩ ⃤':
name = 'aeskdar'
self.id = grab_UUID(name)
self.name = name

View File

@ -13,7 +13,7 @@ Please respect Geoffrey, the bot is very sensitive.w
'''
bad_error_message = 'OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The admins at our ' \
'headquarters are working VEWY HAWD to fix this! (Error in command {})'
'headquarters are working VEWY HAWD to fix this! (Error in command {}: {})'
bot = commands.Bot(command_prefix=command_prefix, description=description, case_insensitive=True)
@ -40,8 +40,7 @@ async def on_command_error(error, ctx):
elif isinstance(error.original, UsernameLookupFailed):
error_str = error.original.__doc__
else:
error_str = bad_error_message.format(ctx.invoked_with)
print(error)
error_str = bad_error_message.format(ctx.invoked_with, error)
await bot.send_message(ctx.message.channel, error_str)
@ -65,7 +64,7 @@ async def addbase(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, * args):
player_name = get_nickname(ctx.message.author)
try:
base = database.add_base(player_name, name, x_pos, y_pos, z_pos, args)
base = database.add_location(player_name, name, x_pos, y_pos, z_pos, args)
except LocationInitError:
raise commands.UserInputError
@ -98,16 +97,14 @@ async def find(ctx, name: str):
?find [Player name]
'''
base_list = database.find_location_by_owner(name)
if len(base_list) != 0:
base_string = loc_list_to_string(base_list, '{} \n{}')
await bot.say('{}, {} has {} base(s): \n {}'.format(ctx.message.author.mention, name, len(base_list),
base_string))
else:
await bot.say('{}, the player {} is not in the database'.format(ctx.message.author.mention, name))
try:
loc_list = database.find_location_by_owner(name)
loc_string = loc_list_to_string(loc_list, '{} \n{}')
await bot.say('{}, **{}** has **{}** base(s): \n {}'.format(ctx.message.author.mention, name, len(loc_list),
loc_string))
except PlayerNotFound:
await bot.say('{}, the player **{}** is not in the database'.format(ctx.message.author.mention, name))
@bot.command(pass_context=True)
async def delete(ctx, name: str):
@ -119,9 +116,9 @@ async def delete(ctx, name: str):
player_name = get_nickname(ctx.message.author)
try:
database.delete_base(player_name, name)
await bot.say('{}, your base named "{}" has been deleted.'.format(ctx.message.author.mention, name))
except DeleteEntryError:
await bot.say('{}, you do not have a base named "{}".'.format(ctx.message.author.mention, name))
await bot.say('{}, your location named **{}** has been deleted.'.format(ctx.message.author.mention, name))
except (DeleteEntryError, PlayerNotFound):
await bot.say('{}, you do not have a location named **{}**.'.format(ctx.message.author.mention, name))
@bot.command(pass_context=True)
@ -157,11 +154,18 @@ async def additem(ctx, shop_name: str, item_name: str, diamond_price: int):
Adds an item to a shop's inventory
?additem [Shop name] [Item Name] [Price]
'''
try:
player_name = get_nickname(ctx.message.author)
database.add_item(player_name, shop_name, item_name, diamond_price)
await bot.say('{}, {} has been added to the inventory of {}.'.format(ctx.message.author.mention,
await bot.say('{}, **{}** has been added to the inventory of **{}**.'.format(ctx.message.author.mention,
item_name, shop_name))
except PlayerNotFound:
await bot.say('{}, you don\'t have any shops in the database.'.format(ctx.message.author.mention))
except LocationLookUpError:
await bot.say('{}, you don\'t have any shops named **{}** in the database.'.format(ctx.message.author.mention,
shop_name))
@bot.command(pass_context=True)
@ -175,15 +179,30 @@ async def selling(ctx, item_name: str):
shop_list_str = loc_list_to_string(shop_list)
await bot.say('The following shops sell {}: \n {}'.format(item_name, shop_list_str))
@bot.command(pass_context=True)
async def birb(ctx):
'''
Lists all the shops selling an item
?selling [item]
'''
r = redditBot()
birb_list = loc_list_to_string(r.getTopPosts())
await bot.say('Here some birbs: \n {}'.format(birb_list))
# Helper Functions ************************************************************
def get_nickname(discord_user) :
if discord_user.nick is None:
return discord_user.display_name
name = discord_user.display_name
else:
return discord_user.nick
name = discord_user.nick
if name == 'dootb.in ꙩ ⃤':
name = 'aeskdar'
return name
def loc_list_to_string(loc_list, str_format='{}\n{}'):

View File

@ -11,18 +11,6 @@ class TestGeoffreyDatabase(TestCase):
self.loc = Location('test', 1, 2, 3, self.owner, ['Green', 0])
#self.shop = Location('test', 1, 2, 3, self.owner, ['Green', 0])
def test_add_shop(self):
shop = self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0])
self.assertEqual(type(shop), Shop)
def test_add_item(self):
self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0])
self.database.add_item('ZeroHD', 'test', 'dirt', 1)
shops = self.database.find_shop_selling_item('dirt')
self.assertEqual(shops[0].name, 'test')
def test_add_object(self):
self.database.add_object(self.loc)
self.database.add_object(self.owner)
@ -55,6 +43,67 @@ class TestGeoffreyDatabase(TestCase):
self.assertRaises(DeleteEntryError, self.database.delete_entry, Location, expr)
def test_add_shop(self):
shop = self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0])
self.assertEqual(type(shop), Shop)
def test_add_two_shops(self):
shop1 = self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0])
shop2 = self.database.add_shop('ZeroHD', 'no u', 1, 2, 3, ['Green', 0])
loc_list = self.database.find_location_by_owner('ZeroHD')
self.assertEqual(loc_list[1].id, shop2.id)
def test_add_item(self):
self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0])
self.database.add_item('ZeroHD', 'test', 'dirt', 1)
shops = self.database.find_shop_selling_item('dirt')
self.assertEqual(shops[0].name, 'test')
def test_find_location_by_owner(self):
shop = self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0])
loc_list = self.database.find_location_by_owner('ZeroHD')
self.assertEqual(loc_list[0].id, shop.id)
def test_find_location_by_name_and_owner(self):
shop = self.database.add_shop('ZeroHD', 'test', 1, 2, 3, ['Green', 0])
loc_list = self.database.find_location_by_name_and_owner('ZeroHD','test')
self.assertEqual(loc_list[0].id, shop.id)
def test_delete_base(self):
self.database.add_location('ZeroHD', 'test', 1, 2, 3, ['Green', 0])
self.database.delete_base('ZeroHD', 'test')
loc_list = self.database.find_location_by_name_and_owner('ZeroHD', 'test')
self.assertEqual(len(loc_list), 0)
def test_find_location_around(self):
loc = self.database.add_location('ZeroHD', 'test', 0, 0, 0, ['Green', 0])
loc_list = self.database.find_location_around(100, 100, 200)
self.assertEqual(loc_list[0].name, loc.name)
loc_list = self.database.find_location_around(200, 200, 200)
self.assertEqual(len(loc_list), 0)
def test_wrong_case(self):
loc = self.database.add_location('ZeroHD', 'test', 0, 0, 0, ['Green', 0])
loc_list = self.database.find_location_by_owner('zerohd')
self.assertEqual(loc_list[0].id, loc.id)

View File

@ -9,3 +9,6 @@ class TestMinecraftInfoGrabber(TestCase):
def test_grab_playername(self):
self.assertEqual(grab_playername('fe7e84132570458892032b69ff188bc3'), 'ZeroHD')
def test_grab_playername_wrong_case(self):
self.assertEqual(grab_UUID('zerohd'), 'fe7e84132570458892032b69ff188bc3')