示例#1
0
def start_lightsoff(client: WebClient, poll: Poll, request_form: dict) -> None:
    """
    Function, that is invoked when we run /lightsoff command.
    Finish the last poll and give the song.
    """
    send_msg_to_chat(client, request_form,
                     'The poll is finished. The winner is ...')
    winner = poll.find_the_winner_song()

    if poll.is_music_upload:
        upload_song(client, request_form, winner)
    else:
        send_msg_to_chat(
            client, request_form,
            f"{winner['artist']} - {winner['title']} with {len(winner['voted_users'])} votes !!!"
        )

    # Reset poll status
    poll.storage.data['is_music_upload'] = False
    poll.storage.data['is_started'] = False
    poll.storage.save()

    # Delete message(s) from chat
    channel_id = request_form.get('channel_id')
    for message_id in poll.storage.get_all_messages_id():
        delete_msg_in_chat(client, channel_id, message_id)
示例#2
0
def prepare_songs_for_poll(client: WebClient, poll: Poll, request_form: dict,
                           songs: list) -> None:
    """
    Function that create and save poll in storage.
    """
    # If previous steps are good, do ...
    poll.number_of_songs = len(songs)
    poll.storage.data['is_started'] = True

    # As slack message allows having only < 50 songs in the message, so next code
    # seperate all the songs on 30 songs chunks and put each chunk in its message.
    messages = []

    if len(songs) > 30:
        chunks = poll.divide_all_songs_into_chunks([songs])
    else:
        chunks = [songs]

    send_msg_to_chat(client, request_form,
                     "Please, vote for the next song to play 🎶")

    for songs_chunk in chunks:
        message_blocks = poll.create_poll_blocks(songs_chunk)
        response = send_msg_to_chat(client,
                                    request_form,
                                    '',
                                    blocks=message_blocks)
        messages.append({'id': response.get('ts'), 'songs': songs_chunk})

    poll.storage.data['messages'] = messages
    poll.storage.save()
示例#3
0
def start_resume(client: WebClient, poll: Poll, request_form: dict) -> None:
    """
    Function, that is invoked when we run /drop command.
    /drop is valid only for channel admin.
    """
    poll.storage.data = poll.storage.check_for_unfinished_poll() # Get data from unfinished poll  

    send_msg_to_chat(client, request_form, "Please, vote for the next song to play 🎶")
    for message in poll.storage.data['messages']:
        message_blocks = poll.create_poll_blocks(message.get('songs'))
        response = send_msg_to_chat(client, request_form, '', blocks=message_blocks)
        message['id'] = response.get('ts') # Update messages id
示例#4
0
def start_lightsoff(client: WebClient, poll: Poll, request_form: dict):
    """
    Function that is invoked when we run /lightsoff command.
    Finish the last poll and give the song.
    """
    if poll.is_started:
        send_msg_to_chat(client, request_form,
                         'The poll is finished. The winner is ...')
        winner = poll.find_the_winner_song()
        song_title = make_valid_song_name(winner)
        download_song(song_title, winner['link'], './media')
        upload_file(client, request_form, './media/{}.mp3'.format(song_title))
        poll.is_started = False
        delete_songs('./media')
    else:
        send_msg_to_user(
            client, request_form,
            'No polls started yet. Use /disco command to run poll.')
示例#5
0
def start_poptop(client: WebClient, poll: Poll, request_form: dict) -> None:
    """
    Function, that is invoked when we run /poptop command.
    """
    channel_id = request_form.get('channel_id')
    selected_song_id = check_poptop_argument(poll, request_form)
    sorted_songs = sort_songs(poll.storage.get_all_songs())
    song = sorted_songs[selected_song_id - 1]
    message = poll.storage.get_message_from_song(song)

    if poll.is_music_upload:
        upload_song(client, request_form, song)
    else:
        send_msg_to_chat(
            client, request_form,
            f'Poptop song {selected_song_id} is {song["artist"]} - {song["title"]}'
        )

    # Reste selected song votes
    song['voted_users'] = []

    edit_msg_in_chat(client, channel_id, message.get('id'), "POPTOP SONG",
                     poll.create_poll_blocks(message.get('songs')))
    poll.storage.save()
示例#6
0
def start_disco(client: WebClient, poll: Poll, request_form: dict) -> None:
    """
    Main function that is invoked when we run /disco command.
    /disco is valid only for channel admin.
    """
    if is_admin(client, request_form):
        if poll.is_started:
            send_msg_to_user(client, request_form, 'Previous poll is not finished. Type /lightsoff to finish it.')
        else:
            songs = get_all_songs(poll.number_of_songs)
            poll.start(None, songs)
            blocks = poll.update_block()
            poll_response = send_msg_to_chat(client, request_form, 'MUSIC POLL', blocks=blocks)
            poll.storage.update_message_id(poll_response['ts'])
    else:
        send_msg_to_user(client, request_form, 'You have no permission.')