def start(message):
    try:

        if closer(
                message.chat.id
        ) == None:  #обращение к функции, которая закрывает доступ левым польщователям.
            #Для предоставления доступа необходимо в таблицу внести ID.
            pass
        else:
            con = sqlite3.connect('mp3_base.db')
            cur = con.cursor()
            cur.execute('SELECT * FROM users WHERE Id=:Id',
                        {'Id': message.chat.id})

            #создание клавиатуры с командой /list
            keyboard = types.ReplyKeyboardMarkup(row_width=2,
                                                 resize_keyboard=True,
                                                 one_time_keyboard=False)
            callback_button = types.InlineKeyboardButton(text="/list")
            keyboard.add(callback_button)
            bot.send_message(
                message.chat.id,
                'Чтобы увидеть весь список исполнителей, нажми на кнопку ниже:',
                reply_markup=keyboard
            )  #отправка сообщения после команды /start и открытие клавиатуры с кнопкой /list
    except:
        print('error 1: def start - basic_bot_file.')
def sandler(message):

    if closer(message.chat.id) == None:
        pass
    else:
        con = sqlite3.connect('mp3_base.db')
        cur = con.cursor()

        cur.execute('SELECT * FROM basic_lib WHERE artist=:artist',
                    {'artist': message.text})

        if cur.fetchone() == None:
            bot.send_message(message.chat.id, 'Артист отсутствует в базе')
        else:
            cur.execute('DELETE FROM for_send WHERE id=:id',
                        {'id': message.chat.id}), con.commit()

            if '***' in message.text + '***group':
                btns = []
                keyboarder = types.InlineKeyboardMarkup()
                btns.append(
                    types.InlineKeyboardButton(text="Top songs",
                                               callback_data="top_songs"))
                btns.append(
                    types.InlineKeyboardButton(text="All songs",
                                               callback_data="all_songs"))
                btns.append(
                    types.InlineKeyboardButton(text="Albums",
                                               callback_data="from_albums"))
                keyboarder.add(*btns)
                bot.send_message(message.chat.id,
                                 message.text,
                                 reply_markup=keyboarder,
                                 parse_mode='Markdown')
                con.cursor(), cur.execute(
                    "UPDATE users set last_artist=:last_artist WHERE id=:id", {
                        "last_artist": message.text,
                        'id': message.chat.id
                    }), con.commit()
                users_dict[message.chat.id] = {
                    'artist': message.text,
                    'counter': 4
                }
            else:
                print('omg')
def audio(message):  #Добавление треков в БД
    if closer(message.chat.id) == None:
        pass
    else:
        try:
            id_user = message.chat.id
            format = message.audio.mime_type
            artist = message.audio.performer
            track = message.audio.title
            audio_id = message.audio.file_id
            duration = message.audio.duration

            if format.split('/')[1] == 'flac':
                pass
            else:
                lib_insert(id_user, format, artist, track, audio_id, duration)
        except:
            print('error 11: def audio - basic_bot_file.')
def list(message):

    if closer(message.chat.id) == None:
        pass
    else:
        users_dict[message.chat.id] = 0
        con = sqlite3.connect('mp3_base.db')
        cur = con.cursor()
        cur.execute('SELECT * FROM artist_info')

        btns = []

        if len(cur.fetchall()) <= 32:
            pass
        else:
            btns.append(
                types.InlineKeyboardButton(text="next", callback_data="next"))
        keyboard = groups_list(0, 32)
        keyboard.add(*btns)
        bot.send_message(message.chat.id,
                         "Список исполнителей: ",
                         reply_markup=keyboard,
                         parse_mode='Markdown')
def callback_inline(mes):
    if closer(mes.message.chat.id) == None:
        pass
    else:

        id = mes.message.chat.id
        album_dict = {}

        con = sqlite3.connect('mp3_base.db')
        cur = con.cursor()

        bot.edit_message_text(chat_id=mes.message.chat.id,
                              message_id=mes.message.message_id,
                              text=mes.data)
        keyboard1 = types.ReplyKeyboardMarkup(row_width=2,
                                              resize_keyboard=True,
                                              one_time_keyboard=False)
        keyboard2 = types.ReplyKeyboardMarkup(row_width=2,
                                              resize_keyboard=True,
                                              one_time_keyboard=False)
        callback_button1 = types.InlineKeyboardButton(text="/next")
        callback_button2 = types.InlineKeyboardButton(text="/list")

        if '^^^' in mes.data:
            keyboard1.add(callback_button2)
            bot.send_message(mes.message.chat.id,
                             'genres',
                             reply_markup=keyboard2)
        elif '&&&' in mes.data:
            try:
                cur.execute('DELETE FROM for_send WHERE id=:id',
                            {'id': id}), con.commit()

                x = mes.data.split('&&&')
                a = 0
                c = (users_dict.get(id)['artist']).lower().split('***')[0]

                album_dict[id] = {x[0]}
                cur.execute(
                    'SELECT * FROM basic_lib WHERE artist=:artist AND album=:album',
                    {
                        'artist': c,
                        'album': x[0]
                    })
                fetch_artist = cur.fetchall()

                while a <= 4:
                    bot.send_audio(mes.message.chat.id,
                                   audio=fetch_artist[a][1])
                    a = a + 1
                else:
                    for_send_from_album(c, x[0], id)
                    keyboard1.add(callback_button2, callback_button1)
                    bot.send_message(mes.message.chat.id,
                                     '/next',
                                     reply_markup=keyboard1)
            except:
                keyboard1.add(callback_button2)
                bot.send_message(mes.message.chat.id,
                                 'Выбери:',
                                 reply_markup=keyboard1)
                print('error 7: def callback_inline - basic_bot_file.')
        elif '***' in mes.data:
            bot.delete_message(mes.message.chat.id, mes.message.message_id)
            con.cursor()
            cur.execute(
                "UPDATE users set last_artist=:last_artist WHERE id=:id", {
                    "last_artist": mes.data.split('***')[0],
                    'id': id
                })
            con.commit()

            btns = []
            keyboarder = types.InlineKeyboardMarkup()
            btns.append(
                types.InlineKeyboardButton(text="Top songs",
                                           callback_data="top_songs"))
            btns.append(
                types.InlineKeyboardButton(text="All songs",
                                           callback_data="all_songs"))
            btns.append(
                types.InlineKeyboardButton(text="Albums",
                                           callback_data="from_albums"))
            btns.append(
                types.InlineKeyboardButton(text="Back", callback_data="-back"))
            keyboarder.add(*btns)
            bot.send_message(mes.message.chat.id,
                             'Choose:',
                             reply_markup=keyboarder,
                             parse_mode='Markdown')
            users_dict[id] = {'artist': mes.data, 'counter': 4}

        elif 'top_songs' == mes.data:
            bot.delete_message(mes.message.chat.id, mes.message.message_id)
            cur.execute('DELETE FROM for_send WHERE id=:id',
                        {'id': id}), con.commit()

            try:
                keyboard1 = types.ReplyKeyboardMarkup(row_width=2,
                                                      resize_keyboard=True,
                                                      one_time_keyboard=False)
                callback_button1 = types.InlineKeyboardButton(text="/next")
                callback_button2 = types.InlineKeyboardButton(text="/list")

                under_split_artist = (users_dict.get(id)['artist']).lower()
                artist = under_split_artist.split('***')[0]

                toper(artist)

                c = 0
                d = 0

                try:
                    cur.execute(
                        'SELECT * FROM top_art_table WHERE artist=:artist',
                        {'artist': artist})
                    fetch_art_top = cur.fetchall()
                    print('fetch art table')

                    while c <= 50:
                        if d == 5:
                            keyboard1.add(
                                callback_button2,
                                callback_button1)  #, callback_button3)
                            bot.send_message(
                                mes.message.chat.id,
                                'Чтобы открыть список групп, нажми на кнопку /list:',
                                reply_markup=keyboard1)
                            break
                        if fetch_art_top[c][2] == None:
                            pass
                        else:
                            bot.send_audio(mes.message.chat.id,
                                           audio=fetch_art_top[c][2])
                            d = d + 1
                        c = c + 1
                    if len(fetch_art_top) >= 4:
                        for_send_from_top_func(artist, id)
                except:
                    print('error 7: send from top. 2st send')
            except:
                bot.send_message(mes.message.chat.id,
                                 'Что-то пошло не так. Попробуй еще раз.')
                print('error 9: def top - basic_bot_file.')
        elif 'all_songs' == mes.data:

            bot.delete_message(mes.message.chat.id, mes.message.message_id)
            cur.execute('DELETE FROM for_send WHERE id=:id',
                        {'id': id}), con.commit()

            try:
                keyboard1 = types.ReplyKeyboardMarkup(row_width=2,
                                                      resize_keyboard=True,
                                                      one_time_keyboard=False)
                callback_button1 = types.InlineKeyboardButton(text="/next")
                callback_button2 = types.InlineKeyboardButton(text="/list")

                keyboard2 = types.InlineKeyboardMarkup()
                btns = []
                btns.append(
                    types.InlineKeyboardButton(text="Next",
                                               callback_data="-next"))
                keyboard2.add(*btns)

                under_split_artist = (users_dict.get(id)['artist']).lower()
                artist = under_split_artist.split('***')[0]

                a = 0
                b = 0

                try:
                    cur.execute(
                        "SELECT count(*) as total from basic_lib WHERE artist=:artist",
                        {'artist': artist})
                    len_art = cur.fetchall()[0][0]
                    cur.execute('SELECT * FROM basic_lib WHERE artist=:artist',
                                {'artist': artist})
                    fetch_artist = cur.fetchall()

                    try:
                        while a <= len_art:
                            if b == 5:
                                break
                            if fetch_artist[a][1] == None:
                                pass
                            else:
                                bot.send_audio(mes.message.chat.id,
                                               audio=fetch_artist[a][1])
                                b = b + 1
                            a = a + 1
                        bot.send_message(
                            mes.message.chat.id,
                            "Формируется список отправки, плз подождите")
                    except:
                        print('all songs except')

                    if len_art >= 6:
                        for_send_from_basic_func(artist, id)
                        keyboard1.add(callback_button2, callback_button1)
                        bot.send_message(mes.message.chat.id,
                                         'Выбери:',
                                         reply_markup=keyboard1)
                    else:
                        print('pass all tracks')
                except:
                    print('error 8: def all - basic_bot_file.')
            except:
                bot.send_message(mes.message.chat.id,
                                 'Что-то пошло не так. Попробуй еще раз.')
                print('error basic bot: command all')

        elif 'from_albums' == mes.data:
            bot.delete_message(mes.message.chat.id, mes.message.message_id)
            cur = con.cursor()
            cur.execute('DELETE FROM for_send WHERE id=:id',
                        {'id': id}), con.commit()

            keyboard3 = types.InlineKeyboardMarkup()
            btns = []
            btns.append(
                types.InlineKeyboardButton(text="Back", callback_data="-back"))
            keyboard3.add(*btns)

            x = users_dict.get(id)['artist'].split('***')[0]

            if '/' in x:
                x = x.replace('/', '.')
            else:
                pass
            if album_list(x) == 'to small songs':
                print('sucks')
                bot.send_message(mes.message.chat.id,
                                 'Слишком мало композиций данной группы.',
                                 reply_markup=keyboard3,
                                 parse_mode='Markdown')
            else:
                bot.send_message(mes.message.chat.id,
                                 'Альбомы',
                                 reply_markup=album_list(x))

        elif "+back" == mes.data:
            bot.delete_message(mes.message.chat.id, mes.message.message_id)
            btns = []
            keyboarder = types.InlineKeyboardMarkup()
            btns.append(
                types.InlineKeyboardButton(text="Top songs",
                                           callback_data="top_songs"))
            btns.append(
                types.InlineKeyboardButton(text="All songs",
                                           callback_data="all_songs"))
            btns.append(
                types.InlineKeyboardButton(text="Albums",
                                           callback_data="from_albums"))
            btns.append(
                types.InlineKeyboardButton(text="Back", callback_data="-back"))
            keyboarder.add(*btns)
            bot.send_message(mes.message.chat.id,
                             'Choose:',
                             reply_markup=keyboarder,
                             parse_mode='Markdown')

            con.cursor()
            cur.execute("SELECT * FROM users WHERE id=:id", {"id": id})
            Xxx = cur.fetchone()
            users_dict[id] = {'artist': Xxx[2], 'counter': 4}

        elif "-back" == mes.data:
            bot.delete_message(mes.message.chat.id, mes.message.message_id)
            users_dict[mes.message.chat.id] = 0

            con = sqlite3.connect('mp3_base.db')
            cur = con.cursor()
            cur.execute('SELECT * FROM artist_info')
            x = len(cur.fetchall())
            btns = []

            if x <= 32:
                pass
            else:
                btns.append(
                    types.InlineKeyboardButton(text="next",
                                               callback_data="next"))
            keyboard = groups_list(0, 32)
            keyboard.add(*btns)
            bot.send_message(mes.message.chat.id,
                             "Список исполнителей: ",
                             reply_markup=keyboard,
                             parse_mode='Markdown')
        else:
            pass