Queries are now limited and all commands have a cooldown.

doc_update
Joey Hines 2018-07-29 09:29:27 -05:00
parent 04ccf94d1a
commit c25b2dad33
3 changed files with 25 additions and 6 deletions

View File

@ -148,12 +148,15 @@ class DatabaseInterface:
def search_all_fields(self, session, search): def search_all_fields(self, session, search):
loc_string = '\n**Locations:**' loc_string = '\n**Locations:**'
count = 0 count = 0
limit = 10
expr = Location.owner.has(Player.name.ilike('%{}%'.format(search))) | Location.name.ilike('%{}%'.format(search)) expr = Location.owner.has(Player.name.ilike('%{}%'.format(search))) | Location.name.ilike('%{}%'.format(search))
for loc in self.database.query_by_filter(session, Location, expr): for loc in self.database.query_by_filter(session, Location, expr, limit=limit):
loc_string = "{}\n{}".format(loc_string, loc) loc_string = "{}\n{}".format(loc_string, loc)
count += 1 count += 1
if count == limit:
loc_string = loc_string + '\n**. . .**'
expr = Tunnel.owner.has(Player.name.ilike('%{}%'.format(search))) & Tunnel.location == None expr = Tunnel.owner.has(Player.name.ilike('%{}%'.format(search))) & Tunnel.location == None
tunnels = self.database.query_by_filter(session, Tunnel, expr) tunnels = self.database.query_by_filter(session, Tunnel, expr)

View File

@ -51,9 +51,9 @@ class GeoffreyDatabase:
except: except:
session.rollback() session.rollback()
def query_by_filter(self, session, obj_type, * args): def query_by_filter(self, session, obj_type, *args, limit=10):
filter_value = self.combine_filter(args) filter_value = self.combine_filter(args)
return session.query(obj_type).filter(filter_value).all() return session.query(obj_type).filter(filter_value).limit(limit).all()
def delete_entry(self, session, obj_type, * args): def delete_entry(self, session, obj_type, * args):

View File

@ -35,6 +35,8 @@ async def on_ready():
async def on_command_error(error, ctx): async def on_command_error(error, ctx):
if isinstance(error, commands.CommandNotFound): if isinstance(error, commands.CommandNotFound):
error_str = 'Command not found, ding dongs like you can use ?help to see all the commands this bot can do.' error_str = 'Command not found, ding dongs like you can use ?help to see all the commands this bot can do.'
elif isinstance(error, commands.CommandOnCooldown):
return
elif isinstance(error, commands.UserInputError): elif isinstance(error, commands.UserInputError):
error_str = 'Invalid syntax for **{}** you ding dong, please read ?help {}.'\ error_str = 'Invalid syntax for **{}** you ding dong, please read ?help {}.'\
.format(ctx.invoked_with, ctx.invoked_with) .format(ctx.invoked_with, ctx.invoked_with)
@ -53,6 +55,7 @@ async def on_command_error(error, ctx):
error_str)) error_str))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command() @bot.command()
async def test(): async def test():
''' '''
@ -61,6 +64,7 @@ async def test():
await bot.say('I\'m here you ding dong') await bot.say('I\'m here you ding dong')
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def register(ctx): async def register(ctx):
''' '''
@ -80,7 +84,7 @@ async def register(ctx):
await bot.say('{}, you are already in the database. Ding dong.'.format(ctx.message.author.mention)) await bot.say('{}, you are already in the database. Ding dong.'.format(ctx.message.author.mention))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def addbase(ctx, x_pos: int, z_pos: int, * args): async def addbase(ctx, x_pos: int, z_pos: int, * args):
''' '''
@ -105,6 +109,7 @@ async def addbase(ctx, x_pos: int, z_pos: int, * args):
ctx.message.author.mention, name)) ctx.message.author.mention, name))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def addshop(ctx, x_pos: int, z_pos: int, *args): async def addshop(ctx, x_pos: int, z_pos: int, *args):
''' '''
@ -128,6 +133,8 @@ async def addshop(ctx, x_pos: int, z_pos: int, *args):
await bot.say('{}, a shop called **{}** already exists. You need to specify a different name.'.format( await bot.say('{}, a shop called **{}** already exists. You need to specify a different name.'.format(
ctx.message.author.mention, name)) ctx.message.author.mention, name))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def addtunnel(ctx, tunnel_color: str, tunnel_number: int, *args): async def addtunnel(ctx, tunnel_color: str, tunnel_number: int, *args):
''' '''
@ -155,6 +162,7 @@ async def addtunnel(ctx, tunnel_color: str, tunnel_number: int, *args):
await bot.say('{}, {} is an invalid tunnel color.'.format(ctx.message.author.mention, tunnel_color)) await bot.say('{}, {} is an invalid tunnel color.'.format(ctx.message.author.mention, tunnel_color))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def find(ctx, * args): async def find(ctx, * args):
''' '''
@ -173,6 +181,8 @@ async def find(ctx, * args):
except LocationLookUpError: except LocationLookUpError:
await bot.say('{}, no matches to **{}** were found in the database'.format(ctx.message.author.mention, search)) await bot.say('{}, no matches to **{}** were found in the database'.format(ctx.message.author.mention, search))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def tunnel(ctx, player: str): async def tunnel(ctx, player: str):
''' '''
@ -187,6 +197,8 @@ async def tunnel(ctx, player: str):
await bot.say('{}, no tunnels for the player **{}** were found in the database' await bot.say('{}, no tunnels for the player **{}** were found in the database'
.format(ctx.message.author.mention, player)) .format(ctx.message.author.mention, player))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def delete(ctx, * args): async def delete(ctx, * args):
''' '''
@ -206,6 +218,7 @@ async def delete(ctx, * args):
@bot.command(pass_context=True) @bot.command(pass_context=True)
@commands.cooldown(5, 60, commands.BucketType.user)
async def findaround(ctx, x_pos: int, z_pos: int, * args): async def findaround(ctx, x_pos: int, z_pos: int, * args):
''' '''
Finds all the locations around a certain point. Finds all the locations around a certain point.
@ -245,6 +258,7 @@ async def findaround(ctx, x_pos: int, z_pos: int, * args):
@bot.command(pass_context=True) @bot.command(pass_context=True)
@commands.cooldown(5, 60, commands.BucketType.user)
async def additem(ctx, item_name: str, quantity: int, diamond_price: int, * args): async def additem(ctx, item_name: str, quantity: int, diamond_price: int, * args):
''' '''
Adds an item to a shop's inventory. Adds an item to a shop's inventory.
@ -269,6 +283,7 @@ async def additem(ctx, item_name: str, quantity: int, diamond_price: int, * args
shop_name)) shop_name))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def selling(ctx, item_name: str): async def selling(ctx, item_name: str):
''' '''
@ -280,9 +295,10 @@ async def selling(ctx, item_name: str):
result = bot_commands.selling(item_name) result = bot_commands.selling(item_name)
await bot.say('{}, the following shops sell **{}**: \n{}'.format(ctx.message.author.mention, item_name, result)) await bot.say('{}, the following shops sell **{}**: \n{}'.format(ctx.message.author.mention, item_name, result))
except ItemNotFound: except ItemNotFound:
await bot.say('{}, no shops sell **{}**'.format(ctx.message.author.mention, item_name)) await bot.say('{}, no shop sells **{}**.'.format(ctx.message.author.mention, item_name))
@commands.cooldown(5, 60, commands.BucketType.user)
@bot.command(pass_context=True) @bot.command(pass_context=True)
async def info(ctx, * args): async def info(ctx, * args):
''' '''