Discord bot that plays music from every website ever via youtube-dl
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Chords/src/main/java/moe/nekojimi/chords/commands/PlayCommand.java

133 lines
5.1 KiB

/*
* Copyright (C) 2022 jimj316
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package moe.nekojimi.chords.commands;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import moe.nekojimi.chords.Chords;
import moe.nekojimi.chords.TrackRequest;
import moe.nekojimi.musicsearcher.Query;
import moe.nekojimi.musicsearcher.Result;
/**
*
* @author jimj316
*/
public class PlayCommand extends Command
{
private static final double SEARCH_SCORE_THRESHOLD_DISPLAY = 0.6;
private static final double SEARCH_SCORE_THRESHOLD_AUTOPLAY = 9999; // disable autoplay it sucks
// private List<Result> lastSearchResults;
public PlayCommand(Chords main)
{
super(main, "play");
}
@Override
public void call(Invocation invocation)
{
TrackRequest request = new TrackRequest();
request.setInvocation(invocation);
try
{
final URL url = new URL(invocation.getArgs().get(0));
request.setUrl(url);
bot.queueDownload(request);
} catch (MalformedURLException mux)
{
// not a URL, try parsing it as a search result
if (request.getSearchResults() != null && !request.getSearchResults().isEmpty())
{
try
{
int index = Integer.parseInt(invocation.getArgs().get(0));
int size = request.getSearchResults().size();
if (index >= 1 && index <= size)
{
Result result = request.getSearchResults().get(index - 1);
request.setResult(result);
bot.queueDownload(request);
// event.getChannel().sendMessage("Track removed.").queue();
} else if (size > 1)
invocation.respond("That's not a number between 1 and " + size + "!");
else if (size == 1)
invocation.respond("There's only one track and that's not one of them!");
return;
} catch (NumberFormatException nfx)
{
// event.getChannel().sendMessage(arg + " isn't a number!").queue();
}
}
// otherwise, try searching
CompletableFuture<List<Result>> search = bot.getSearcher().search(Query.fullText(invocation.getArgs().stream().reduce((t, u) -> t + " " + u).get()));
invocation.respond("Searching for \"" + invocation.getArgs() + "\" ...");
search.orTimeout(30, TimeUnit.SECONDS).whenCompleteAsync((List<Result> results, Throwable exec) ->
{
if (exec != null)
{
invocation.respond("Failed to search! Reason: " + exec.getMessage());
return;
}
request.setSearchResults(results);
// lastSearchResults = results;
if (results.isEmpty())
{
invocation.respond("Found nothing! :(");
return;
}
if (results.get(0).getScore() >= SEARCH_SCORE_THRESHOLD_AUTOPLAY)
{
request.setResult(results.get(0));
bot.queueDownload(request);
return;
}
String resultString = ">>> :mag: __Search results:__\n";
int i = 1;
for (Result result : results)
{
if (result.getScore() <= SEARCH_SCORE_THRESHOLD_DISPLAY && i > 5)
break;
if (i > 10)
break;
resultString += "**" + i + ":** "
+ "[" + result.getSourceAbbr() + "] "
+ "*" + result.getTitle() + "* "
+ "by " + (result.getArtist() != null ? result.getArtist().trim() : "unknown") + " "
// + (result.getAlbum() != null ? "from the album *" + result.getAlbum().trim() + "*" : "")
+ "\n";
i++;
}
resultString += "Type eg. `!play 1` to select";
invocation.respond(resultString);
});
// event.getChannel().sendMessage("That's not a valid URL you idiot! " + ex.getMessage()).queue();
}
}
}