async def i_pocket(client, message, db_acc): channel = message.channel author = message.author tokens = message.content.split(' ') if len(tokens) < 2: await channel.send( '8!ipocket usage: 8!ipocket [add|remove] <character>') return remove_fighter = False # Figure out where the fighter name starts # to determine between 8!ipocket pit and 8!ipocket remove/add pit fighter_name_start_idx = 1 if (tokens[1].lower() == "add"): fighter_name_start_idx = 2 elif (tokens[1].lower() == "remove"): fighter_name_start_idx = 2 remove_fighter = True if not remove_fighter: # First add character, if necessary await i_play(client, message, db_acc, False) # Assume everything after is the fighter name test_fighter_string = ' '.join(tokens[fighter_name_start_idx:]) fighter_name, confidence = await find_fighter(db_acc, channel, test_fighter_string) # Might want to fine tune this later, but 80 seems good if (confidence < 80): await channel.send( 'I\'m really not sure who {} is. Remember: 8!ipocket usage: 8!ipocket [add/remove] <character>' .format(test_fighter_string)) return if (not await is_registered(db_acc, author.id, channel)): await channel.send('Please register with 8!register first!') return # Adding or removing that you pocket this character else: try: db_acc.execute_update( ''' UPDATE player.player_fighter SET is_main = CASE WHEN %(set_pocket)s THEN false ELSE is_main END, is_pocket = %(set_pocket)s, is_true_main = CASE WHEN %(set_pocket)s THEN false ELSE is_true_main END WHERE player_discord_id = %(discord_id)s AND fighter_id = (SELECT id FROM fighter.fighter WHERE name=%(name)s)''', { "discord_id": author.id, "name": fighter_name, "set_pocket": not remove_fighter }) except dberr.Error as e: print(e) await channel.send(DB_ERROR_MSG.format(author.mention)) raise if remove_fighter: await channel.send('{0} no longer pockets {1}. If you want ' \ 'to remove this character entirely, use 8!iplay remove {1}'.format(author.mention, fighter_name)) else: await channel.send('I see, so {} pockets {}. {}'.format( author.mention, fighter_name, random_snarky_comment()))
async def i_play(client, message, db_acc, send_message=True): channel = message.channel author = message.author tokens = message.content.split(' ') if len(tokens) < 2: if send_message: await channel.send( '8!iplay usage: 8!iplay [add|remove] <character>') return remove_fighter = False # Figure out where the fighter name starts # to determine between 8!iplay pit and 8!iplay remove/add pit fighter_name_start_idx = 1 if (tokens[1].lower() == "add"): fighter_name_start_idx = 2 elif (tokens[1].lower() == "remove"): fighter_name_start_idx = 2 remove_fighter = True # Assume everything after is the fighter name test_fighter_string = ' '.join(tokens[fighter_name_start_idx:]) fighter_name, confidence = await find_fighter(db_acc, channel, test_fighter_string) # Might want to fine tune this later, but 80 seems good if (confidence < 80): if send_message: await channel.send('I\'m really not sure who {} is. Remember: 8!iplay usage:' \ ' Check 8!help for command usage.'.format(test_fighter_string)) return if (not await is_registered(db_acc, author.id, channel)): if send_message: await channel.send('Please register with 8!register first!') return # Removing that you play this character if (remove_fighter): try: db_acc.execute_update( ''' DELETE FROM player.player_fighter WHERE player_discord_id = %(discord_id)s AND fighter_id = (SELECT id FROM fighter.fighter WHERE name=%(name)s)''', { "discord_id": author.id, "name": fighter_name }) except dberr.Error as e: print(e) if send_message: await channel.send(DB_ERROR_MSG.format(author.mention)) raise if send_message: await channel.send('{} does not play {}, okay.'.format( author.mention, fighter_name)) # Adding that you play this character else: try: db_acc.execute_update( ''' INSERT INTO player.player_fighter (player_discord_id, fighter_id, is_main, is_true_main) SELECT %(discord_id)s, id as fighter_id, false, false FROM fighter.fighter WHERE name=%(name)s''', { "discord_id": author.id, "name": fighter_name }) except dberr.UniqueViolation as e: if send_message: await channel.send('I already know you play {}, {}!'.format( fighter_name, author.mention)) return except dberr.Error as e: print(e) if send_message: await channel.send(DB_ERROR_MSG.format(author.mention)) raise if send_message: await channel.send('Okay, noted that {} plays {}. {}'.format( author.mention, fighter_name, random_snarky_comment()))