Ejemplo n.º 1
0
def sort(db_session, user):
    new_pirate_info = Pirate()
    new_pirate_info.user = user
    new_pirate_info.user_id = user.user_id

    new_pirate_info.current_points = 0
    new_pirate_info.total_points = 0
    new_pirate_info.doubloons = 0
    new_pirate_info.times_planked = 0
    new_pirate_info.plank_sniper_charges = 0
    new_pirate_info.mutiny_charges = 0
    new_pirate_info.rum = 3
    new_pirate_info.enable_polly_cmd = False

    names = db_session.query(PirateName).filter(PirateName.pirate_id==None).all()
    name = names[randint(0, len(names) - 1)]
    name.pirate = new_pirate_info
    new_pirate_info.pirate_name_id = name.pirate_name_id

    ships = db_session.query(Ship).all()
    ship = ships[0]
    for s in ships:
        if len(s.pirates) <= len(ship.pirates):
            ship = s

    new_pirate_info.ship = ship
    new_pirate_info.ship_id = ship.ship_id
Ejemplo n.º 2
0
def remove_Restricted_Command_grant(message, command, db_session=None):
    match = command.pattern.match(message['text'])
    first_name = match.group(1)
    last_name = match.group(2)
    restricted_command_key = match.group(3)

    restricted_command = db_session.query(RestrictedCommand).filter().first()
    if restricted_command is None:
        return f'No restricted command with key \'{restricted_command_key}\' found.'

    user = db_session.query(User).filter(User.slack_first_name==first_name).filter(User.slack_last_name==last_name).first()
    if user is None:
        return f'No user found, please check your spelling: {first_name} {last_name}'

    for grant in user.restricted_command_grants:
        if grant.command.command_key == restricted_command_key:
            db_session.delete(grant)
            db_session.commit()
            return f'Access to {restricted_command_key} has been revoked for {first_name} {last_name}.'

    return f'{first_name} {last_name} doesn\'t have permission to {restricted_command_key}.'
Ejemplo n.º 3
0
def add_Restricted_Command_grant(message, command, db_session=None):
    match = command.pattern.match(message['text'])
    first_name = match.group(1)
    last_name = match.group(2)
    restricted_command_key = match.group(3)

    restricted_command = db_session.query(RestrictedCommand).filter(RestrictedCommand.command_key==restricted_command_key).first()
    if restricted_command is None:
        restricted_command = RestrictedCommand(command_key=restricted_command_key)
        db_session.add(restricted_command)

    user = db_session.query(User).filter(User.slack_first_name==first_name).filter(User.slack_last_name==last_name).first()
    if user is None:
        return f'No user found, please check your spelling: {first_name} {last_name}'

    for grant in user.restricted_command_grants:
        if grant.command.command_key == restricted_command_key:
            return f'{first_name} {last_name} already has access to that command.'

    user.restricted_command_grants.append(RestrictedCommandGrant(user_id=user.user_id, restricted_command_id=restricted_command.restricted_command_id))
    db_session.commit()
    return f'{first_name} {last_name} granted access to {restricted_command_key}.'
Ejemplo n.º 4
0
def set_help_link(message, command, db_session=None):
    match = command.pattern.match(message['text'])

    if match is not None:
        new_link = match.group(1)

        config_item = db_session.query(ConfigItem).filter(ConfigItem.key=='help_link').first()
        if config_item is None:
            config_item = ConfigItem(key='help_link')
            db_session.add(config_item)

        config_item.value = new_link
        db_session.commit()
        return f"Help link set: '{new_link}'"
Ejemplo n.º 5
0
def dump_user(message, command, db_session=None):
    match = command.pattern.match(message['text'])
    name = [match.group(1), match.group(2)]
    user = db_session.query(User).filter(User.slack_first_name==name[0]).filter(User.slack_last_name==name[1]).first()
    return str(user)
Ejemplo n.º 6
0
def list_restricted_command_keys(message, command, db_session=None):
    restricted_commands = db_session.query(RestrictedCommand).all()
    return '\n'.join(['Here are all the restricted command keys:'] + [rc.command_key for rc in restricted_commands])
Ejemplo n.º 7
0
def refresh_users(slack_client, db_session=None):
    print('Full user refresh requested, this might take a minute...')

    members = slack_client.api_call(
        'users.list'
    )
    members = members['members']

    members = [m for m in members if not m['is_bot'] and not m['is_app_user']]

    slack_users = []

    print("Beginning phase 1: Slack user retrieval")
    name_pattern = re.compile(r'([\w\-]+)\.([\w\-]+)')
    i = 0
    for member in members:
        id_ = member['id']
        name = member['name']
        deleted = member['deleted']
        email = member.get('profile')
        if email is not None:
            email = email.get('email')
        dm_channel = slack_client.api_call('im.open', user=id_)['channel']['id']

        existing_record = db_session.query(User).filter(User.slack_id==id_).first()
        if existing_record is None:
            existing_record = User()
            existing_record.slack_id = id_
            existing_record.in_pirate_channel = False
            db_session.add(existing_record)

        slack_users.append(existing_record)
        name_matcher = name_pattern.match(name)

        existing_record.email = email

        existing_record.is_deleted = deleted
        existing_record.dm_channel_id = dm_channel
        if name_matcher is not None:
            existing_record.slack_first_name = name_matcher.group(1).title()
            existing_record.slack_last_name = name_matcher.group(2).title()
            if not existing_record.display_first_name_locked:
                existing_record.display_first_name = name_matcher.group(1).title()
            if not existing_record.display_last_name_locked:
                existing_record.display_last_name = name_matcher.group(2).title()
        else:
            if not existing_record.display_last_name_locked:
                existing_record.display_last_name = name
            print(f'Error processing user with Slack ID {id_} and name {name}; manual processing may be required')

        i += 1
        if i % 25 is 0:
            print(f'{str(i).zfill(4)} Slack records processed...')
    print(f'{str(i).zfill(4)} total Slack records processed.')

    channel_member_ids = slack_client.api_call('channels.info', channel=__pirate_channel)
    channel_member_ids = channel_member_ids['channel']['members']
    print('Beginning phase 2: verifying channel membership...')
    for user in slack_users:
        user.in_pirate_channel = user.slack_id in channel_member_ids

    print('Beginning phase 3: verifying presence of pirate info')
    for user in [su for su in slack_users if su.in_pirate_channel]:
        if user.pirate_info is None:
            print(f'Sorting {user.display_first_name} {user.display_last_name}…')
            sort(db_session, user)
    print(f'Available pirate names remaining: {len(db_session.query(PirateName).filter(PirateName.pirate_id==None).all())}')

    db_session.commit()

    print('Full user refresh completed!')