feat: 'yplay' play the first result. add 'last' command. #87

This commit is contained in:
Terry Geng 2020-03-03 09:26:11 +08:00
parent 30e33441c5
commit 61497b1ecb
2 changed files with 36 additions and 11 deletions

View File

@ -32,7 +32,7 @@ def register_all_commands(bot):
bot.register_command(constants.commands('play_radio'), cmd_play_radio)
bot.register_command(constants.commands('rb_query'), cmd_rb_query)
bot.register_command(constants.commands('rb_play'), cmd_rb_play)
bot.register_command(constants.commands('yt_query'), cmd_yt_query)
bot.register_command(constants.commands('yt_search'), cmd_yt_search)
bot.register_command(constants.commands('yt_play'), cmd_yt_play)
bot.register_command(constants.commands('help'), cmd_help)
bot.register_command(constants.commands('stop'), cmd_stop)
@ -46,6 +46,7 @@ def register_all_commands(bot):
bot.register_command(constants.commands('ducking_volume'), cmd_ducking_volume)
bot.register_command(constants.commands('current_music'), cmd_current_music)
bot.register_command(constants.commands('skip'), cmd_skip)
bot.register_command(constants.commands('last'), cmd_last)
bot.register_command(constants.commands('remove'), cmd_remove)
bot.register_command(constants.commands('list_file'), cmd_list_file)
bot.register_command(constants.commands('queue'), cmd_queue)
@ -451,7 +452,7 @@ def cmd_rb_play(bot, user, text, command, parameter):
yt_last_result = []
yt_last_page = 0 # TODO: if we keep adding global variables, we need to consider sealing all commands up into classes.
def cmd_yt_query(bot, user, text, command, parameter):
def cmd_yt_search(bot, user, text, command, parameter):
global log, yt_last_result, yt_last_page
item_per_page = 5
@ -489,11 +490,21 @@ def _yt_format_result(results, start, count):
def cmd_yt_play(bot, user, text, command, parameter):
global log, yt_last_result
global log, yt_last_result, yt_last_page
if parameter and parameter.isdigit() and 0 <= int(parameter) - 1 < len(yt_last_result):
if parameter:
if parameter.isdigit() and 0 <= int(parameter) - 1 < len(yt_last_result):
url = "https://www.youtube.com/watch?v=" + yt_last_result[int(parameter) - 1][0]
cmd_play_url(bot, user, text, command, url)
else:
results = util.youtube_search(parameter)
if results:
yt_last_result = results
yt_last_page = 0
url = "https://www.youtube.com/watch?v=" + yt_last_result[0][0]
cmd_play_url(bot, user, text, command, url)
else:
bot.send_msg(constants.strings('yt_query_error'))
else:
bot.send_msg(constants.strings('bad_parameter', command=command), text)
@ -639,6 +650,17 @@ def cmd_skip(bot, user, text, command, parameter):
bot.send_msg(constants.strings('queue_empty'), text)
def cmd_last(bot, user, text, command, parameter):
global log
if len(var.playlist) > 0:
bot.interrupt_playing()
bot.launch_music(len(var.playlist) - 1)
bot.async_download_next()
else:
bot.send_msg(constants.strings('queue_empty'), text)
def cmd_remove(bot, user, text, command, parameter):
global log

View File

@ -126,8 +126,8 @@ play_playlist = playlist
rb_query = rbquery
rb_play = rbplay
yt_query = ytquery
yt_play = ytplay
yt_search = ysearch
yt_play = yplay
help = help
pause = pause
@ -136,6 +136,7 @@ stop = stop
remove = rm
clear = clear
skip = skip
last = last
current_music = np, now
volume = volume
kill = kill
@ -217,6 +218,7 @@ help = <h3>Commands</h3>
<li> <b>!<u>pa</u>use </b> - pause </li>
<li> <b>!<u>st</u>op </b> - stop playing </li>
<li> <b>!<u>sk</u>ip </b> - jump to the next song </li>
<li> <b>!<u>la</u>st </b> - jump to the last song </li>
<li> <b>!<u>v</u>olume </b> {volume} - get or change the volume (from 0 to 100) </li>
<li> <b>!<u>m</u>ode </b> [{mode}] - get or set the playback mode, {mode} should be one of <i>one-shot</i> (remove
item once played), <i>repeat</i> (looping through the playlist), <i>random</i> (randomize the playlist)</li>
@ -237,11 +239,12 @@ help = <h3>Commands</h3>
<li> <b>!<u>rep</u>eat </b> [{num}] - repeat current song {num} (1 by default) times.</li>
<li> <b>!<u>ran</u>dom </b> - randomize the playlist.</li>
<li> <b>!<u>rad</u>io </b> {url} - append a radio {url} to the playlist </li>
<li> <b>!<u>l</u>istfile </b> [{pattern}] - display list of available files (that match the regex pattern if {pattern} is given) </li>
<li> <b>!<u>li</u>stfile </b> [{pattern}] - display list of available files (that match the regex pattern if {pattern} is given) </li>
<li> <b>!<u>rbq</u>uery </b> {keyword} - query http://www.radio-browser.info for a radio station </li>
<li> <b>!<u>rbp</u>lay </b> {id} - play a radio station with {id} (eg. !rbplay 96746) </li>
<li> <b>!<u>ytq</u>uery </b> {keyword} - query youtube. Use <i>!ytquery -n</i> to turn the page. </li>
<li> <b>!<u>ytp</u>lay </b> {index} - play an item from the list returned by <i>!ytquery</i>. </li>
<li> <b>!<u>ys</u>earch </b> {keyword} - query youtube. Use <i>!ytquery -n</i> to turn the page. </li>
<li> <b>!<u>yp</u>lay </b> {index/keywords} - play an item from the list returned by <i>!ytquery</i>, or add the
first search result of {keywords} into the playlist.</li>
</ul>
<b>Other</b>
<ul>