예제 #1
0
def new_throw_joey(message, command, db_session=None):
    match = command.pattern.match(message['text'])
    new_message = match.group(1)

    db_session.add(ThrowJoey(message=new_message))
    db_session.commit()

    return f"New potential response for !throwJoey added: '{new_message}'"
예제 #2
0
def new_weapoj(message, command, db_session=None):
    match = command.pattern.match(message['text'])
    new_message = match.group(1)

    db_session.add(Weapoj(message=new_message))
    db_session.commit()

    return f"New potential response for !weapoj added: '{new_message}'"
예제 #3
0
def new_smack_patrick(message, command, db_session=None):
    match = command.pattern.match(message['text'])
    new_message = match.group(1)

    db_session.add(SmackPatrick(message=new_message))
    db_session.commit()

    return f"New potential response for !smackPatrick added: '{new_message}'"
예제 #4
0
def new_hack_the_planet(message, command, db_session=None):
    match = command.pattern.match(message['text'])
    new_message = match.group(1)

    db_session.add(HackThePlanet(message=new_message))
    db_session.commit()

    return f"New potential response for !hackThePlanet added: '{new_message}'"
예제 #5
0
def new_shanty(message, command, db_session=None):
    match = command.pattern.match(message['text'])

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

        db_session.add(Shanty(link=new_link))
        db_session.commit()
        return f"New potential link for !shanty added: '{new_link}'"
    else:
        return "That's not a valid YouTube link!"
예제 #6
0
def new_plank(message, command, db_session=None):
    match = command.pattern.match(message['text'])
    new_template = match.group(1)

    if '{target_first_name}' in new_template or '{target_last_name}' in new_template:
        can_self_plank = True
    else:
        can_self_plank = False

    db_session.add(Plank(template=new_template, is_self_plank_compatible=can_self_plank))
    db_session.commit()

    return f"New template for !walkThePlank added: '{new_template}'"
예제 #7
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}'"
예제 #8
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}.'
예제 #9
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}.'
예제 #10
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!')