fixed issue with ?register function. Also cleaned up code a bit

doc_update
Joey Hines 2018-08-30 11:46:20 -05:00
parent 6748f01208
commit 2111bf8c8e
7 changed files with 21 additions and 19 deletions

View File

@ -78,4 +78,3 @@ class Config:
def get_config(config_path): def get_config(config_path):
return Config(config_path) return Config(config_path)

View File

@ -4,7 +4,7 @@ from geoffrey.DatabaseModels import *
class DatabaseInterface: class DatabaseInterface:
def __init__(self, bot_config, debug=False): def __init__(self, bot_config, debug=False):
self.database = GeoffreyDatabase(bot_config, debug) self.database = GeoffreyDatabase(bot_config, debug)
def add_base(self, session, owner, name, x_pos, z_pos, dimension=None): def add_base(self, session, owner, name, x_pos, z_pos, dimension=None):
base = Base(name, x_pos, z_pos, owner, dimension) base = Base(name, x_pos, z_pos, owner, dimension)
@ -93,7 +93,8 @@ class DatabaseInterface:
def find_location_around(self, session, x_pos, z_pos, radius, dimension): def find_location_around(self, session, x_pos, z_pos, radius, dimension):
dimension_obj = Dimension.str_to_dimension(dimension) dimension_obj = Dimension.str_to_dimension(dimension)
expr = (Location.x < x_pos + radius + 1) & (Location.x > x_pos - radius - 1) & (Location.z < z_pos + radius + 1) \ expr = (Location.x < x_pos + radius + 1) & (Location.x > x_pos - radius - 1) & \
(Location.z < z_pos + radius + 1) \
& (Location.z > z_pos - radius - 1) & (Location.dimension == dimension_obj) & (Location.z > z_pos - radius - 1) & (Location.dimension == dimension_obj)
return list_to_string(self.database.query_by_filter(session, Location, expr)) return list_to_string(self.database.query_by_filter(session, Location, expr))

View File

@ -59,7 +59,7 @@ class GeoffreyDatabase:
filter_value = self.combine_filter(args) filter_value = self.combine_filter(args)
return session.query(obj_type).filter(filter_value).limit(limit).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):
filter_value = self.combine_filter(args) filter_value = self.combine_filter(args)
entry = session.query(obj_type).filter(filter_value) entry = session.query(obj_type).filter(filter_value)
@ -77,7 +77,7 @@ class GeoffreyDatabase:
s = '' s = ''
for obj in obj_list: for obj in obj_list:
s = s + '\n' + obj.id s = s + '\n' + obj.id
return s return s
def combine_filter(self, filter_value): def combine_filter(self, filter_value):
@ -90,7 +90,6 @@ class TunnelDirection(enum.Enum):
South = "south" South = "south"
West = "west" West = "west"
def str_to_tunnel_dir(arg): def str_to_tunnel_dir(arg):
arg = arg.lower() arg = arg.lower()
@ -181,7 +180,7 @@ class Location(SQL_Base):
x = Column(Integer) x = Column(Integer)
z = Column(Integer) z = Column(Integer)
tunnel = relationship("Tunnel", uselist=False, cascade="all, delete-orphan") tunnel = relationship("Tunnel", uselist=False, cascade="all, delete-orphan")
dimension = Column(Enum(Dimension)) dimension = Column(Enum(Dimension))
owner_id = Column(Integer, ForeignKey('geoffrey_players.id', ondelete='CASCADE')) owner_id = Column(Integer, ForeignKey('geoffrey_players.id', ondelete='CASCADE'))
@ -210,7 +209,7 @@ class Location(SQL_Base):
raise LocationInitError raise LocationInitError
def dynmap_link(self, bot_config): def dynmap_link(self, bot_config):
return '<{}/?worldname={}&mapname=surface&zoom=4&x={}&y=65&z={}>'.\ return '<{}/?worldname={}&mapname=surface&zoom=4&x={}&y=65&z={}>'. \
format(bot_config.dynmap_url, bot_config.world_name, self.x, self.z) format(bot_config.dynmap_url, bot_config.world_name, self.x, self.z)
def pos_to_str(self): def pos_to_str(self):
@ -222,7 +221,7 @@ class Location(SQL_Base):
def info_str(self): def info_str(self):
return "**{}** @ {}, Owner: **{}**, Type: **{}**".format(self.name, self.pos_to_str(), self.owner.name, return "**{}** @ {}, Owner: **{}**, Type: **{}**".format(self.name, self.pos_to_str(), self.owner.name,
self.type) self.type)
def full_str(self, bot_config): def full_str(self, bot_config):
return self.__str__() + '\n' + self.dynmap_link(bot_config) return self.__str__() + '\n' + self.dynmap_link(bot_config)
@ -294,4 +293,4 @@ class ItemListing(SQL_Base):
return '**{}** **{}** for **{}D**'.format(self.amount, self.name, self.price) return '**{}** **{}** for **{}D**'.format(self.amount, self.name, self.price)
def __str__(self): def __str__(self):
return '**{}**, selling {}'.format(self.shop.name, self.listing_str()) return '**{}**, selling {}'.format(self.shop.name, self.listing_str())

View File

@ -10,14 +10,14 @@ def get_name(args):
return name return name
def get_nickname(discord_user, bot_config): def get_nickname(discord_user, special_users):
if discord_user.nick is None: if discord_user.nick is None:
name = discord_user.display_name name = discord_user.display_name
else: else:
name = discord_user.nick name = discord_user.nick
if name in bot_config.special_name_list: if name in special_users:
return bot_config.special_name_list[name] return special_users[name]
else: else:
return name return name

View File

@ -43,6 +43,7 @@ class GeoffreyBot(commands.Bot):
super().__init__(command_prefix=config.prefix, description=description, pm_help=True, case_insensitive=True) super().__init__(command_prefix=config.prefix, description=description, pm_help=True, case_insensitive=True)
self.error_users = config.error_users self.error_users = config.error_users
self.admin_users = config.bot_mod self.admin_users = config.bot_mod
self.special_users = config.special_name_list
self.bot_commands = Commands(config) self.bot_commands = Commands(config)
for extension in extensions: for extension in extensions:
@ -109,9 +110,8 @@ class GeoffreyBot(commands.Bot):
logger.error("Geoffrey encountered unhandled exception: %s", error) logger.error("Geoffrey encountered unhandled exception: %s", error)
error_str = bad_error_message.format(ctx.invoked_with) error_str = bad_error_message.format(ctx.invoked_with)
await self.send_message(ctx.message.channel, await self.send_message(ctx.message.channel, '{} **Error Running Command:** {}'.format(
'{} **Error Running Command:** {}'.format(ctx.message.author.mention, ctx.message.author.mention, error_str))
error_str))
async def send_error_message(self, msg): async def send_error_message(self, msg):
for user_id in self.error_users: for user_id in self.error_users:
@ -170,6 +170,7 @@ def setup_logging(config):
def start_bot(config_path="{}/GeoffreyConfig.ini".format(path.dirname(path.abspath(__file__)))): def start_bot(config_path="{}/GeoffreyConfig.ini".format(path.dirname(path.abspath(__file__)))):
bot = None
try: try:
bot_config = get_config(config_path) bot_config = get_config(config_path)
@ -185,5 +186,6 @@ def start_bot(config_path="{}/GeoffreyConfig.ini".format(path.dirname(path.abspa
except Exception as e: except Exception as e:
logger.info('Bot encountered the following unhandled exception %s', e) logger.info('Bot encountered the following unhandled exception %s', e)
finally: finally:
bot.loop.stop() if bot is not None:
bot.loop.stop()
logger.info("Bot shutting down...") logger.info("Bot shutting down...")

View File

@ -23,7 +23,7 @@ class Add_Commands:
""" """
try: try:
player_name = get_nickname(ctx.message.author, bot_config) player_name = get_nickname(ctx.message.author, self.bot.special_users)
self.bot.bot_commands.register(player_name, ctx.message.author.id) self.bot.bot_commands.register(player_name, ctx.message.author.id)
await self.bot.say('{}, you have been added to the database.'.format(ctx.message.author.mention)) await self.bot.say('{}, you have been added to the database.'.format(ctx.message.author.mention))
except AttributeError: except AttributeError:

View File

@ -37,7 +37,8 @@ class Edit_Commands:
""" """
loc = get_name(args) loc = get_name(args)
try: try:
loc_str = self.bot.bot_commands.edit_tunnel(tunnel_color, tunnel_number, loc, discord_uuid=ctx.message.author.id) loc_str = self.bot.bot_commands.edit_tunnel(tunnel_color, tunnel_number, loc,
discord_uuid=ctx.message.author.id)
await self.bot.say( await self.bot.say(
'{}, the following location has been updated: \n\n{}'.format(ctx.message.author.mention, loc_str)) '{}, the following location has been updated: \n\n{}'.format(ctx.message.author.mention, loc_str))