diff --git a/controller.py b/controller.py index eb3df71..2b4f3a9 100644 --- a/controller.py +++ b/controller.py @@ -122,108 +122,36 @@ def query_dragons(criteria): return Dragon.find_dragons(element, skill, ability, rarity, level) -async def process_adventurers(name, level=None, message=None): - try: - if name is None or name == "": - await view.show_invalid_name() +def process_adventurer(name, level=None): + if name is None or name == "": + raise KeyError("Name not specified") + return Adventurer(name, level) - adventurer = Adventurer(name, level) - if adventurer is None: - await view.show_adventurer_not_found(name) - else: - await view.show_adventurer(adventurer, message) - except Exception as e: - await view.show_exception( - "Failed to process adventurer with the following error:{0}" - .format(str(e)), traceback.format_exc()) +def process_wyrmprint(name, level=None): + if name is None or name == "": + raise KeyError("Name not specified") + return Wyrmprint(name, level or 3) -async def process_wyrmprint(name, level=None, message=None): - try: - if name is None or name == "": - await view.show_invalid_name() - wyrmprint = Wyrmprint(name, level or 3) - if wyrmprint is None: - await view.show_wyrmprint_not_found(name) - else: - await view.show_wyrmprint(wyrmprint, message) - except Exception as e: - await view.show_exception( - "Failed to process wyrmprint with the following error:{0}" - .format(str(e)), traceback.format_exc()) - - -async def process_dragon(name, level=None, message=None): - try: - if name is None or name == "": - await view.show_invalid_name() +def process_dragon(name, level=None): + if name is None or name == "": + raise KeyError("Name not specified") + return Dragon(name, level or 2) - dragon = Dragon(name, level or 2) - if dragon is None: - await view.show_dragon_not_found(name) - else: - await view.show_dragon(dragon, message) - except Exception as e: - await view.show_exception( - "Failed to process dragon with the following error:{0}" - .format(str(e)), traceback.format_exc()) -async def process_adventurers_reaction(emoji, adventurer, message): - if emoji == "\U0001F5BC": # Full picture - await view.show_adventurer_full(adventurer, message) - elif emoji == "\U0001F508": # 1 unbind - await process_adventurers(adventurer.name, 1, message) - elif emoji == "\U0001F509": # 2 unbinds - await process_adventurers(adventurer.name, 2, message) - elif emoji == "\U0001F50A": # 3 unbinds - await process_adventurers(adventurer.name, 3, message) - -async def process_wyrmprint_reaction(emoji, wyrmprint, message): - if emoji == "\U0001F5BC": # Full picture - await view.show_wyrmprint_full(wyrmprint, message) - elif emoji == "\U0001F508": # 1 unbind - await process_wyrmprint(wyrmprint.name, 1, message) - elif emoji == "\U0001F509": # 2 unbinds - await process_wyrmprint(wyrmprint.name, 2, message) - elif emoji == "\U0001F50A": # 3 unbinds - await process_wyrmprint(wyrmprint.name, 3, message) - elif emoji == "\U0001F3A8": # Full base picture - await view.show_wyrmprint_base_full(wyrmprint, message) - -async def process_dragon_reaction(emoji, dragon, message): - if emoji == "\U0001F5BC": # Full picture - await view.show_dragon_full(dragon, message) - elif emoji == "\U0001F508": # 1 unbind - await process_dragon(dragon.name, 1, message) - elif emoji == "\U0001F509": # 2 unbinds - await process_dragon(dragon.name, 2, message) - -async def handle_alias(message): - if ',' not in message or len(message.split(',')) < 1: - result = delete_alias(message) - await view.show_completed_alias(result) - return - alias = message.split(',')[0].strip() - name = message.split(',')[1].strip() +def handle_alias(alias_text, aliased_name): + if aliased_name is None: + return delete_alias(alias_text) alias_type = 0 - aliased_id = Adventurer.get_adventurer_id(name) - if aliased_id != 0: - result = create_update_alias(aliased_id, alias, alias_type) - await view.show_completed_alias(result) - return - alias_type += 1 - aliased_id = Wyrmprint.get_wyrmprint_id(name) - if aliased_id != 0: - result = create_update_alias(aliased_id, alias, alias_type) - await view.show_completed_alias(result) - return - alias_type += 1 - aliased_id = Dragon.get_dragon_id(name) - if aliased_id != 0: - result = create_update_alias(aliased_id, alias, alias_type) - await view.show_completed_alias(result) - return + lst = [Adventurer.get_adventurer_id, Wyrmprint.get_wyrmprint_id, + Dragon.get_dragon_id] + aliased_id = 0 + alias_type = -1 + while aliased_id == 0 and alias_type < len(lst): + alias_type += 1 + aliased_id = lst[alias_type](aliased_name) + return create_update_alias(aliased_id, alias_text, alias_type) def start(): diff --git a/master.db b/master.db index 7883e70..2e7c6a7 100644 Binary files a/master.db and b/master.db differ diff --git a/view.py b/view.py index 7362cfd..b7aff09 100644 --- a/view.py +++ b/view.py @@ -23,6 +23,7 @@ ''' import controller import discord +import traceback from discord.ext.commands import Bot from models.adventurer import Adventurer from models.wyrmprint import Wyrmprint @@ -31,10 +32,10 @@ from utils.parsing import convert_ISO_date_to_string, convert_args_to_dict channel = None -active_adventurer_messages = {} -active_wyrmprint_messages = {} -active_dragon_messages = {} -all_active_messages = [] +adv_msgs = {} +wyr_msgs = {} +dra_msgs = {} +all_msgs = [] config = Config("config.ini") client = Bot(command_prefix=config.command_start) @@ -58,40 +59,60 @@ async def exit(context): @client.command(name="get_adventurer", - description="Gets an adventurer with the given name", + description=''' + Searches for an adventurer using a name or alias. + The search is case insensitive, and in the case of multiple + results, the earliest released adventurer is returned. + ''', brief="Gets an adventurer using a case insensitive search", - aliases=["adv", "adventurer", "a"], - pass_context=True) -async def get_adventurer(context): - name = handle_context(context) - await client.send_typing(channel) - await controller.process_adventurers(name) + aliases=["adventurer", "adv", "a"]) +async def get_adventurer(name): + try: + adventurer = controller.process_adventurer(name) + await show_adventurer(adventurer) + except Exception as e: + await show_exception(e) @client.command(name="get_wyrmprint", - description="Gets a wyrmprint with the given name", + description=''' + Searches for a wyrmprint using a name or alias. + The search is case insensitive, and in the case of multiple + results, the earliest released wyrmprint is returned. + ''', brief="Gets a wyrmprint using a case insensitive search", - aliases=["wyr", "wyrmprint", "w"], - pass_context=True) -async def get_wyrmprint(context): - name = handle_context(context) - await client.send_typing(channel) - await controller.process_wyrmprint(name) + aliases=["wyrmprint", "wyr", "w"]) +async def get_wyrmprint(name): + try: + wyrmprint = controller.process_wyrmprint(name) + await show_wyrmprint(wyrmprint) + except Exception as e: + await show_exception(e) @client.command(name="get_dragon", - description="Gets a dragon with the given name", + description=''' + Searches for a dragon using a name or alias. + The search is case insensitive, and in the case of multiple + results, the earliest released dragon is returned. + ''', brief="Gets a dragon using a case insensitive search", - aliases=["dra", "dragon", "d"], - pass_context=True) -async def get_dragon(context): - name = handle_context(context) - await client.send_typing(channel) - await controller.process_dragon(name) + aliases=["dragon", "dra", "d"]) +async def get_dragon(name): + try: + dragon = controller.process_dragon(name) + await show_dragon(dragon) + except Exception as e: + await show_exception(e) @client.command(name="query", - description="Queries for anything", + description=''' + Queries the database for adventurers, dragons, or + wyrmprints with the given criteria. Criteria is set as + pairs. See example below: + {0}query type=adv ability="Burn Res +100%" + '''.format(config.command_start), brief="Queries for any adventurer, print, or dragon", aliases=["que", "q"], pass_context=True) @@ -102,25 +123,33 @@ async def query(context): @client.command(name="update", - description="Updates the bot's config", + description=''' + Resets the configuration and updates the bot using the + new values set in the config.ini file + ''', brief="Updates the bot's configurations", - aliases=["u"], - pass_context=True) -async def update(context): - message = handle_context(context) + aliases=["u"]) +async def update(): config = Config("config.ini") - await show_completed_update() + await client.say("Update completed") @client.command(name="alias", - description="Creates a new alias to search by", - brief="Creates a new alias to search by", - aliases=["al", "ali", "alia"], - pass_context=True) -async def alias(context): - message = handle_context(context) - await client.send_typing(channel) - await controller.handle_alias(message) + description=''' + Creates, updates, and deletes aliases. Aliases are shorthand + or alternative ways to search for items. If the second + parameter of the aliased_name is not specified, this will + delete the alias_text from the database. If this alias + already exists, it will update the alias. The same alias + may exist that refers to an adventurer, dragon, and print. + ''', + brief="Creates a new alias to search by") +async def alias(alias_text, aliased_name=None): + try: + alias_result = controller.handle_alias(alias_text, aliased_name) + await show_completed_alias(alias_result) + except Exception as e: + await show_exception(e) def handle_context(context): @@ -186,11 +215,6 @@ async def show_adventurer(adventurer, message=None): await show_or_edit_adventurer(e, adventurer, message) -@client.event -async def show_adventurer_not_found(name): - await client.send_message(channel, "Adventurer {0} not found".format(name)) - - @client.event async def show_wyrmprint(wyrmprint, message=None): url_name = "%20".join(wyrmprint.name.split()) @@ -216,11 +240,6 @@ async def show_wyrmprint(wyrmprint, message=None): await show_or_edit_wyrmprint(e, wyrmprint, message) -@client.event -async def show_wyrmprint_not_found(name): - await client.send_message(channel, "Wyrmprint {0} not found".format(name)) - - @client.event async def show_dragon(dragon, message=None): url_name = "%20".join(dragon.name.split()) @@ -251,16 +270,6 @@ async def show_dragon(dragon, message=None): await show_or_edit_dragon(e, dragon, message) -@client.event -async def show_dragon_not_found(name): - await client.send_message(channel, "Dragon {0} not found".format(name)) - - -@client.event -async def show_invalid_name(): - await client.send_message(channel, "Invalid/no name given") - - @client.event async def show_missing_criteria(missing_criteria): await client.send_message(channel, @@ -281,9 +290,9 @@ async def show_unknown_criteria(criteria_name, criteria): @client.event -async def show_exception(message, trace_message): - print(trace_message) - await client.send_message(channel, message) +async def show_exception(e): + print(traceback.format_exc()) + await client.say(str(e)) def get_emoji_element(elementtype): @@ -310,32 +319,70 @@ def get_emoji_limited(limited): @client.event async def on_reaction_add(reaction, user): - if (reaction.message.id in active_adventurer_messages and - reaction.emoji in config.adventurer_reactions and - user != client.user): + if user == client.user: + return + + emoji = reaction.emoji + message = reaction.message + if (message.id in adv_msgs and emoji in config.adventurer_reactions): await client.remove_reaction(reaction.message, reaction.emoji, user) - await controller.process_adventurers_reaction( - reaction.emoji, - active_adventurer_messages[reaction.message.id], - reaction.message) - - elif (reaction.message.id in active_wyrmprint_messages and - reaction.emoji in config.wyrmprint_reactions and - user != client.user): + adventurer = adv_msgs[message.id] + await process_adventurers_reaction(emoji, adventurer, message) + + elif (message.id in wyr_msgs and emoji in config.wyrmprint_reactions): await client.remove_reaction(reaction.message, reaction.emoji, user) - await controller.process_wyrmprint_reaction( - reaction.emoji, - active_wyrmprint_messages[reaction.message.id], - reaction.message) - - elif (reaction.message.id in active_dragon_messages and - reaction.emoji in config.dragon_reactions and - user != client.user): + wyrmprint = wyr_msgs[message.id] + await process_wyrmprint_reaction(emoji, wyrmprint, message) + + elif (message.id in dra_msgs and emoji in config.dragon_reactions): await client.remove_reaction(reaction.message, reaction.emoji, user) - await controller.process_dragon_reaction( - reaction.emoji, - active_dragon_messages[reaction.message.id], - reaction.message) + dragon = dra_msgs[message.id] + await process_dragon_reaction(emoji, dragon, message) + + +@client.event +async def process_adventurers_reaction(emoji, adventurer, message): + if emoji == "\U0001F5BC": # Full picture + await show_adventurer_full(adventurer, message) + return + + adventurer = controller.process_adventurer(adventurer.name, + get_level(emoji)) + await show_adventurer(adventurer, message) + + +@client.event +async def process_wyrmprint_reaction(emoji, wyrmprint, message): + if emoji == "\U0001F5BC": # Full picture + await show_wyrmprint_full(wyrmprint, message) + return + elif emoji == "\U0001F3A8": # Full base picture + await show_wyrmprint_base_full(wyrmprint, message) + return + + wyrmprint = controller.process_wyrmprint(wyrmprint.name, get_level(emoji)) + await show_wyrmprint(wyrmprint, message) + + +@client.event +async def process_dragon_reaction(emoji, dragon, message): + if emoji == "\U0001F5BC": # Full picture + await show_dragon_full(dragon, message) + return + + dragon = controller.process_dragon(dragon.name, get_level(emoji)) + await show_dragon(dragon, message) + + +def get_level(emoji): + level = 1 + if emoji == "\U0001F508": # 1 unbind + level = 1 + elif emoji == "\U0001F509": # 2 unbinds + level = 2 + elif emoji == "\U0001F50A": # 3 unbinds + level = 3 + return level @client.event @@ -382,11 +429,11 @@ async def show_dragon_full(dragon, message=None): @client.event async def show_or_edit_adventurer(e, adventurer, message=None): if message is None: - global active_adventurer_messages, all_active_messages + global adv_msgs, all_msgs await clear_active_messages() - msg = await client.send_message(channel, embed=e) - active_adventurer_messages[msg.id] = adventurer - all_active_messages.append(msg) + msg = await client.say(embed=e) + adv_msgs[msg.id] = adventurer + all_msgs.append(msg) for emoji in config.adventurer_reactions: await client.add_reaction(msg, emoji) else: @@ -396,11 +443,11 @@ async def show_or_edit_adventurer(e, adventurer, message=None): @client.event async def show_or_edit_wyrmprint(e, wyrmprint, message=None): if message is None: - global active_wyrmprint_messages, all_active_messages + global wyr_msgs, all_msgs await clear_active_messages() - msg = await client.send_message(channel, embed=e) - active_wyrmprint_messages[msg.id] = wyrmprint - all_active_messages.append(msg) + msg = await client.say(embed=e) + wyr_msgs[msg.id] = wyrmprint + all_msgs.append(msg) for emoji in config.wyrmprint_reactions: await client.add_reaction(msg, emoji) else: @@ -410,39 +457,34 @@ async def show_or_edit_wyrmprint(e, wyrmprint, message=None): @client.event async def show_or_edit_dragon(e, dragon, message=None): if message is None: - global active_dragon_messages + global dra_msgs await clear_active_messages() - msg = await client.send_message(channel, embed=e) - active_dragon_messages[msg.id] = dragon - all_active_messages.append(msg) + msg = await client.say(embed=e) + dra_msgs[msg.id] = dragon + all_msgs.append(msg) for emoji in config.dragon_reactions: await client.add_reaction(msg, emoji) else: msg = await client.edit_message(message, embed=e) -@client.event -async def show_completed_update(): - await client.send_message(channel, "Update complete") - - @client.event async def show_completed_alias(status): - await client.send_message(channel, "Alias {0}".format(status)) + await client.say("Alias {0}".format(status)) @client.event async def clear_active_messages(): - global all_active_messages, active_adventurer_messages, \ - active_dragon_messages, active_wyrmprint_messages + global all_msgs, adv_msgs, \ + dra_msgs, wyr_msgs - while len(all_active_messages) > max(0, config.message_limit - 1): - message = all_active_messages.pop(0) + while len(all_msgs) > max(0, config.message_limit - 1): + message = all_msgs.pop(0) await client.clear_reactions(message) - if message.id in active_adventurer_messages: - active_adventurer_messages.pop(message.id) - elif message.id in active_dragon_messages: - active_dragon_messages.pop(message.id) - elif message.id in active_wyrmprint_messages: - active_wyrmprint_messages.pop(message.id) + if message.id in adv_msgs: + adv_msgs.pop(message.id) + elif message.id in dra_msgs: + dra_msgs.pop(message.id) + elif message.id in wyr_msgs: + wyr_msgs.pop(message.id)