Help for script to add current song to a playlist

I would like to ask someone to help me please: I installed and configured Lirc to operate a Xiaomi Box TV remote control and I managed to do everything I wanted, except one last thing for which I don’t know if there is a specific mpc command or a script is needed: I would like that when I click the KEY_HOME + KEY_OK key the current song is saved at the end of the “myplaylist.m3u” playlist; obviously if the playlist does not exist yet, the script has to create it and also has to check that the song has not already been inserted once in the playlist. it’s possible? can someone help me please because unfortunately i don’t know linux language well. thank you !!!