Beispiel #1
0
async def player_remove_server(db_dis, Server, rs_name):
    """Try to remove a server id from a player's all_servers list"""
    try:
        db_dis[f'player:{rs_name}#all_servers'].remove(Server.id)
        logger.info(f'Removed server ID {Server.id} from {rs_name} in DB')
    except KeyError:
        raise ex.DataHandlerError(
            f'OSRS account *{rs_name}* is not present in any Activity Log!')
    except ValueError:
        raise ex.DataHandlerError(
            f"OSRS account *{rs_name}* is not present in this server's Activity Log!"
        )
Beispiel #2
0
async def get_member_entry(Server, Member, entry):
    """Get and return member entry's value"""
    logger.info('------------------------------')
    logger.info(
        f'Initialized GET MEMBER ENTRY - Member: {Member.name} | ID: {Member.id} | Entry: {entry}'
    )
    db = await h.db_open(h.DB_DISCORD_PATH)
    try:
        return db[f'member:{Member.id}#server:{Server.id}#{entry}']
    except KeyError:
        raise ex.DataHandlerError(f'Could not find {entry} for {Member.name}')
Beispiel #3
0
async def check_player_member_link(db_dis, Server, Member, rs_name):
    """Check if a player already has a link to a server"""
    try:
        # This member already has this player
        if db_dis[f'player:{rs_name}#server:{Server.id}#member'] == Member.id:
            raise ex.DataHandlerError(
                f'**{Member.name}** is already linked to OSRS account: *{rs_name}*!'
            )
        # This player had a member on this server but is now open (deprecated)
        elif db_dis[f'player:{rs_name}#server:{Server.id}#member'] == None:
            logger.debug(
                f'{rs_name} was used in this server before. {Member.name} will now try to take it'
            )
            return True
        # Another member is using this player
        else:
            raise ex.DataHandlerError(
                f'OSRS account *{rs_name}* is already linked to another member on this server!'
            )
    # This is an open player for this server
    except KeyError:
        return True
Beispiel #4
0
async def player_in_server_member(db_dis, Server, member_id, rs_name):
    """Check if a player is in a member for this server\n
    Returns True or False"""
    try:
        # This member is using this player in this server
        if db_dis[f'player:{rs_name}#server:{Server.id}#member'] == member_id:
            return True
        # Another member is using this player
        else:
            return False
    # This is an open player for this server
    except KeyError:
        raise ex.DataHandlerError(
            f"OSRS account *{rs_name}* is not present in this server's Activity Log!"
        )
Beispiel #5
0
async def toggle_player_entry(Server, Member, rs_name, entry):
    """Toggles a player entry's value between True and False"""
    logger.info('------------------------------')
    logger.info(
        f"Initialized TOGGLE PLAYER ENTRY - Player: {rs_name} | Member: {Member.name} | ID: {Member.id} | Server: {Server.name} | ID: {Server.id}"
    )
    db = await h.db_open(h.DB_DISCORD_PATH)
    try:
        if not await h.player_in_server_member(db, Server, Member, rs_name):
            raise ex.DataHandlerError(
                f'**{Member.name}** does not use OSRS account: *{rs_name}*')
    except Exception as e:
        raise e
    new_toggle = not db[f'player:{rs_name}#server:{Server.id}#{entry}']
    db[f'player:{rs_name}#server:{Server.id}#{entry}'] = new_toggle
    await h.db_write(h.DB_DISCORD_PATH, db)
    logger.info(
        f"FINISHED TOGGLE PLAYER ENTRY - Player: {rs_name} | Member: {Member.name} | ID: {Member.id} | Server: {Server.name} | ID: {Server.id}"
    )
    return new_toggle
Beispiel #6
0
async def update_max_players(new_val):
    """Updates max OSRS accounts per Discord member"""
    config_path = h.DIR_PATH + "/bot_config.json"
    try:
        config_all = await h.db_open(config_path)
        logger.debug(
            f'Old max players: {config_all["MAX_PLAYERS_PER_MEMBER"]}')
        config_all['MAX_PLAYERS_PER_MEMBER'] = new_val
        await h.db_write(config_path, config_all)
        h.MAX_PLAYERS_PER_MEMBER = new_val
    except Exception as e:
        raise ex.DataHandlerError(f'COULD NOT LOAD BOT CONFIG!')


# ---------------------------------- TESTING --------------------------------- #

# def verify_files(file_name):
#     """Verify if a file is present in a path"""
#     path_check = DATA_PATH + file_name
#     if os.path.exists(path_check):
#         logger.debug(f'Found {path_check}')
#         TEMP CODE FOR TESTING
#         if file_name == 'db_discord.json':
#             db = {'active_servers': [],'removed_servers': []}
#             with open(DB_DISCORD_PATH, 'w') as outfile:
#                 json.dump(db, outfile, indent=4, sort_keys=False)
#         else:
#             db = {}
#             with open(DB_RUNESCAPE_PATH, 'w') as outfile:
#                 json.dump(db, outfile, indent=4, sort_keys=False)
#         pass
#     else:
#         if file_name == 'db_discord.json':
#             db = {'active_servers': [],'removed_servers': []}
#         else:
#             db = {}
#         with open(path_check, 'w') as outfile:
#             json.dump(db, outfile)
#         logger.info(f'CREATED NEW FILE: {path_check}')
Beispiel #7
0
async def remove_player(Server, Member, rs_name, force_rm):
    """Remove a player from a specific server\n
    Remove player from runescape.json if there are no more servers for player\n
    Returns False if player could not be removed"""

    member_key_only = False
    if isinstance(Member, int):
        member_key_only = True
        member_name = Member
        member_id = Member
        logger.debug(
            f'Removing player with member key only: {member_name}, {rs_name}')
    else:
        member_name = Member.name
        member_id = Member.id

    logger.info('------------------------------')
    logger.info(
        f'Initialized REMOVE PLAYER: {rs_name} | Removed by: {member_name}')
    db_dis = await h.db_open(h.DB_DISCORD_PATH)  # open Discord DB

    # Check if Member passed is tied to this player on this server (non-admin remove)
    if not force_rm:
        try:
            if not await h.player_in_server_member(db_dis, Server, member_id,
                                                   rs_name):
                raise ex.DataHandlerError(
                    f'**{member_name}** does not use OSRS account: *{rs_name}*'
                )
        except Exception as e:
            raise e

    # Check if this player is in this server, try removing server from player
    try:
        await h.player_remove_server(db_dis, Server, rs_name)
    except Exception as e:
        raise e

    # Get ID of member in this server with this player (admin could be removing)
    player_path = f'player:{rs_name}#server:{Server.id}'
    linked_member = db_dis[f'{player_path}#member']
    # Update all DB entries for this player
    db_dis[f'member:{linked_member}#server:{Server.id}#players'].remove(
        rs_name)
    # Check if member has any more players in this server
    if not db_dis[f'member:{linked_member}#server:{Server.id}#players']:
        del db_dis[f'member:{linked_member}#server:{Server.id}#players']
    db_dis[f'server:{Server.id}#all_players'].remove(rs_name)
    del db_dis[f'{player_path}#member']
    del db_dis[f'{player_path}#mention']
    del db_dis[f'{player_path}#sotw_opt']

    # some users dont have this field
    try:
        del db_dis[f'{player_path}#botw_opt']
    except:
        logger.debug(
            f"  -{rs_name} botw_opt not found for this player. Continuing...")

    # Check if there are no more instances of this player in any server
    if len(db_dis[f'player:{rs_name}#all_servers']) == 0:
        # Delete entries that apply to all instances of this player
        del db_dis[f'player:{rs_name}#all_servers']
        del db_dis[f'player:{rs_name}#sotw_xp']
        del db_dis[f'player:{rs_name}#botw_kills']
        # Remove player from Runescape DB
        db_rs = await h.db_open(h.DB_RUNESCAPE_PATH)  # open Runescape DB
        try:
            del db_rs[rs_name]
        except:
            logger.debug(f"  -{rs_name} was not found in RS DB. Continuing...")
        await h.db_write(h.DB_RUNESCAPE_PATH, db_rs)
        logger.info(
            f"Completely removed player from all DBs | RS name: {rs_name}")
    await h.db_write(h.DB_DISCORD_PATH, db_dis)
    logger.info(
        f"REMOVED PLAYER - RS name : {rs_name} | Linked Member ID : {linked_member} | Remover ID: {member_id} | Server: {Server.name} | ID: {Server.id}"
    )
    return True
Beispiel #8
0
async def rename_player(Server, Member, old_rs_name, new_rs_name, stats_dict):
    """Rename a player in a specific server\n
    Move all info from old player to new player\n
    Returns False if player could not be renamed"""
    logger.info('------------------------------')
    logger.info(
        f'Initialized RENAME PLAYER: Old: {old_rs_name} | New: {new_rs_name} | Updated by: {Member.name} | ID: {Member.id} | Server: {Server.name} | ID: {Server.id}'
    )
    if old_rs_name == new_rs_name:
        raise ex.DataHandlerError('These are the same names!')
    db_dis = await h.db_open(h.DB_DISCORD_PATH)  # open Discord DB

    # Check if new player is already in this server linked with a member
    try:
        await h.check_player_member_link(db_dis, Server, Member, new_rs_name)
    except Exception as e:
        raise e

    # Check if this server is in this player, try removing server from player
    try:
        await h.player_remove_server(db_dis, Server, old_rs_name)
    except Exception as e:
        raise e

    # Get list of existing players for this member in this server
    old_player_path = f'player:{old_rs_name}#server:{Server.id}'
    new_player_path = f'player:{new_rs_name}#server:{Server.id}'
    member_path = f'member:{Member.id}#server:{Server.id}'

    # Add server to player (my method)
    await h.player_add_server(db_dis, Server, new_rs_name)
    # Replace player in member's player list for this server
    db_dis[f'{member_path}#players'].remove(old_rs_name)
    db_dis[f'{member_path}#players'].append(new_rs_name)
    # Replace player in server's player list
    db_dis[f'server:{Server.id}#all_players'].remove(old_rs_name)
    db_dis[f'server:{Server.id}#all_players'].append(new_rs_name)
    # Replace old entries with new, updated entries
    db_dis[f'{new_player_path}#member'] = db_dis[f'{old_player_path}#member']
    db_dis[f'{new_player_path}#mention'] = db_dis[f'{old_player_path}#mention']
    db_dis[f'{new_player_path}#sotw_opt'] = db_dis[
        f'{old_player_path}#sotw_opt']
    db_dis[f'player:{new_rs_name}#sotw_xp'] = db_dis[
        f'player:{old_rs_name}#sotw_xp']
    db_dis[f'player:{new_rs_name}#botw_kills'] = db_dis[
        f'player:{old_rs_name}#botw_kills']

    # some users dont have this field
    try:
        db_dis[f'{new_player_path}#botw_opt'] = db_dis[
            f'{old_player_path}#botw_opt']
        del db_dis[f'{old_player_path}#botw_opt']
    except:
        logger.debug(
            f"  -{old_rs_name} botw_opt not found for this player. Continuing..."
        )

    # Remove old entries (MAY CHANGE LATER)
    del db_dis[f'{old_player_path}#member']
    del db_dis[f'{old_player_path}#mention']
    del db_dis[f'{old_player_path}#sotw_opt']

    # Edit Runescape DB
    # Check if there are no more instances of old player in any server
    db_rs = await h.db_open(h.DB_RUNESCAPE_PATH)  # open Runescape DB
    if len(db_dis[f'player:{old_rs_name}#all_servers']) == 0:
        # Delete entries that apply to all instances of this player
        del db_dis[f'player:{old_rs_name}#all_servers']
        del db_dis[f'player:{old_rs_name}#sotw_xp']
        del db_dis[f'player:{old_rs_name}#botw_kills']
        # Remove old player from Runescape DB
        del db_rs[old_rs_name]
        await h.db_write(h.DB_RUNESCAPE_PATH, db_rs)
        logger.info(
            f"Completely removed player from all DBs | RS name : {old_rs_name}"
        )
    await h.db_write(h.DB_DISCORD_PATH, db_dis)
    db_rs[new_rs_name] = stats_dict
    await h.db_write(h.DB_RUNESCAPE_PATH, db_rs)
    logger.info(
        f"RENAMED PLAYER: Old: {old_rs_name} | New: {new_rs_name} | Updated by: {Member.name} | ID: {Member.id} | Server: {Server.name} | ID: {Server.id}"
    )
    return True
Beispiel #9
0
async def add_player(Server, Member, rs_name, stats_dict):
    """Add a player to a specific server\n
    Create new member if this member's id doesnt exist\n
    Returns False if player could not be added"""
    logger.info('------------------------------')
    logger.info(
        f'Initialized ADD PLAYER: {rs_name} | Added by: {Member.name} | ID: {Member.id} | Server: {Server.name} | ID: {Server.id}'
    )

    # Open Discord DB
    db_dis = await h.db_open(h.DB_DISCORD_PATH)

    # Check if player is already in this server linked with a member
    try:
        await h.check_player_member_link(db_dis, Server, Member, rs_name)
    except Exception as e:
        raise e

    # Get list of existing players for this member in this server
    player_path = f'player:{rs_name}#server:{Server.id}'
    member_path = f'member:{Member.id}#server:{Server.id}'
    try:
        # Member already has a player in this server
        player_list = db_dis[f'{member_path}#players']
        logger.debug(
            f'Found player list for member: {Member.id} in server: {Server.id}...'
        )
    except KeyError:
        # Member doesn't have a player in this server
        player_list = []
        logger.info(
            f"ADDED NEW MEMBER - Name: {Member.name} | ID: {Member.id} | Server ID {Server.id}"
        )
    if len(player_list) >= h.MAX_PLAYERS_PER_MEMBER:
        # Member has too many players for them in this server
        raise ex.DataHandlerError(
            f'You can only have up to **{h.MAX_PLAYERS_PER_MEMBER}** OSRS accounts connected to a Discord member per server.\n'
            f'Please remove one to add another. Current accounts: *{", ".join(player_list)}*\n'
            'If you are changing an OSRS name, use *;transfer {old-name}>>{new-name}* to retain your Activity Log records'
        )
    player_list.append(rs_name)

    # Add server to player (my method)
    await h.player_add_server(db_dis, Server, rs_name)
    # Add updated player list to member for this server
    db_dis[f'{member_path}#players'] = player_list
    # Add generic entries about this member
    db_dis[f'server:{Server.id}#all_players'].append(rs_name)
    db_dis[f'{player_path}#member'] = Member.id
    db_dis[f'{player_path}#mention'] = True
    db_dis[f'{player_path}#sotw_opt'] = True
    db_dis[f'{player_path}#botw_opt'] = True
    await h.db_write(h.DB_DISCORD_PATH, db_dis)

    # Edit Runescape DB
    db_rs = await h.db_open(h.DB_RUNESCAPE_PATH)  # open Runescape DB
    db_rs[rs_name] = stats_dict
    await h.db_write(h.DB_RUNESCAPE_PATH, db_rs)
    logger.info(
        f"ADDED NEW PLAYER - RS name : {rs_name} | Member: {Member.name} | ID: {Member.id} | Server: {Server.name} | ID: {Server.id}"
    )
    return True