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/Main.java

437 lines
17 KiB

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package moe.nekojimi.chords;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.security.auth.login.LoginException;
import moe.nekojimi.musicsearcher.Query;
import moe.nekojimi.musicsearcher.Result;
import moe.nekojimi.musicsearcher.providers.MetaSearcher;
import moe.nekojimi.musicsearcher.providers.Searcher;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.Compression;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
/**
*
* @author jimj316
*/
public class Main extends ListenerAdapter
{
private static final double SEARCH_SCORE_THRESHOLD_AUTOPLAY = 0.9;
private static final double SEARCH_SCORE_THRESHOLD_DISPLAY = 0.6;
private MusicHandler musicHandler;
private final Downloader downloader;
private final Searcher searcher;
private JDA jda;
private List<Result> lastSearchResults;
private int trackNumber = 1;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws LoginException
{
// We only need 2 gateway intents enabled for this example:
EnumSet<GatewayIntent> intents = EnumSet.of(
// We need messages in guilds to accept commands from users
GatewayIntent.GUILD_MESSAGES,
// We need voice states to connect to the voice channel
GatewayIntent.GUILD_VOICE_STATES
);
JDABuilder builder = JDABuilder.createDefault(args[0], intents);
// Disable parts of the cache
builder.disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE);
// Enable the bulk delete event
builder.setBulkDeleteSplittingEnabled(false);
// Disable compression (not recommended)
builder.setCompression(Compression.NONE);
// Set activity (like "playing Something")
builder.setActivity(Activity.playing("music!"));
final Main listener = new Main();
builder.addEventListeners(listener);
JDA jda = builder.build();
listener.setJda(jda);
}
public Main()
{
downloader = new Downloader();
downloader.setMessageHandler((Song song, Exception ex) ->
{
TextChannel channel = jda.getTextChannelById(song.getRequestedIn());
String bracketNo = "[" + song.getNumber() + "] ";
if (channel != null)
if (ex == null)
if (song.getLocation() != null)
channel.sendMessage(bracketNo + "Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue();
else
channel.sendMessage(bracketNo + "Now downloading " + song + " for " + song.getRequestedBy() + " ...").queue();
else
channel.sendMessage(bracketNo + "Failed to download " + song + " for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue();
});
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml"));
}
public void setJda(JDA jda)
{
this.jda = jda;
}
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event)
{
Message message = event.getMessage();
User author = message.getAuthor();
String content = message.getContentRaw();
Guild guild = event.getGuild();
// Ignore message if it's not in a music channel
if (!event.getChannel().getName().contains("music"))
return;
// Ignore message if bot
if (author.isBot())
return;
try
{
String arg = "";
if (content.contains(" "))
arg = content.split(" ", 2)[1];
if (content.startsWith("!join "))
onJoinCommand(event, guild, arg);
else if (content.equals("!join"))
onJoinCommand(event);
else if (content.startsWith("!play "))
onPlayCommand(event, guild, arg);
else if (content.startsWith("!queue"))
onQueueCommand(event, guild);
else if (content.startsWith("!remove "))
onRemoveCommand(event, guild, arg);
else if (content.startsWith("!restart"))
onRestartCommand(event);
else if (content.startsWith("!skip"))
onSkipCommand(event);
else if (content.startsWith("!"))
onHelpCommand(event);
} catch (Exception ex)
{
event.getChannel().sendMessage("Error in command! " + ex.getMessage()).queue();
}
}
/**
* Handle command without arguments.
*
* @param event
* The event for this command
*/
private void onJoinCommand(GuildMessageReceivedEvent event)
{
// Note: None of these can be null due to our configuration with the JDABuilder!
Member member = event.getMember(); // Member is the context of the user for the specific guild, containing voice state and roles
GuildVoiceState voiceState = member.getVoiceState(); // Check the current voice state of the user
VoiceChannel channel = voiceState.getChannel(); // Use the channel the user is currently connected to
// if (channel != null)
// {
// connectTo(channel); // Join the channel of the user
// onConnecting(channel, event.getChannel()); // Tell the user about our success
// } else
// onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure
onJoinCommand(event, member.getGuild(), channel.getName());
}
/**
* Handle command with arguments.
*
* @param event
* The event for this command
* @param guild
* The guild where its happening
* @param arg
* The input argument
*/
private void onJoinCommand(GuildMessageReceivedEvent event, Guild guild, String arg)
{
boolean isNumber = arg.matches("\\d+"); // This is a regular expression that ensures the input consists of digits
VoiceChannel channel = null;
if (isNumber) // The input is an id?
channel = guild.getVoiceChannelById(arg);
if (channel == null) // Then the input must be a name?
{
List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg, true);
if (!channels.isEmpty()) // Make sure we found at least one exact match
channel = channels.get(0); // We found a channel! This cannot be null.
}
TextChannel textChannel = event.getChannel();
if (channel == null) // I have no idea what you want mr user
{
onUnknownChannel(textChannel, arg); // Let the user know about our failure
return;
}
connectTo(channel); // We found a channel to connect to!
onConnecting(channel, textChannel); // Let the user know, we were successful!
}
private void onPlayCommand(GuildMessageReceivedEvent event, Guild guild, String arg)
{
try
{
final URL url = new URL(arg);
queueDownload(url, event);
} catch (MalformedURLException mux)
{
// not a URL, try parsing it as a search result
if (lastSearchResults != null && !lastSearchResults.isEmpty())
{
try
{
int index = Integer.parseInt(arg);
int size = lastSearchResults.size();
if (index >= 1 && index <= size)
{
Result res = lastSearchResults.get(index - 1);
queueDownload(res.getLink(), event);
// event.getChannel().sendMessage("Song removed.").queue();
} else if (size > 1)
event.getChannel().sendMessage("That's not a number between 1 and " + size + "!").queue();
else if (size == 1)
event.getChannel().sendMessage("There's only one song and that's not one of them!").queue();
return;
} catch (NumberFormatException nfx)
{
// event.getChannel().sendMessage(arg + " isn't a number!").queue();
}
}
// otherwise, try searching
CompletableFuture<List<Result>> search = searcher.search(Query.fullText(arg));
event.getChannel().sendMessage("Searching for \"" + arg + "\" ...").queue();
search.orTimeout(30, TimeUnit.SECONDS).whenCompleteAsync((List<Result> results, Throwable exec) ->
{
if (exec != null)
{
event.getChannel().sendMessage("Failed to search! Reason: " + exec.getMessage()).queue();
return;
}
lastSearchResults = results;
if (results.isEmpty())
{
event.getChannel().sendMessage("Found nothing! :(").queue();
return;
}
if (results.get(0).getScore() >= SEARCH_SCORE_THRESHOLD_AUTOPLAY)
{
queueDownload(results.get(0).getLink(), event);
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";
event.getChannel().sendMessage(resultString).queue();
});
// event.getChannel().sendMessage("That's not a valid URL you idiot! " + ex.getMessage()).queue();
}
}
private void queueDownload(final URL url, GuildMessageReceivedEvent event)
{
Song song = new Song(url);
song.setRequestedBy(event.getAuthor().getName());
song.setRequestedIn(event.getChannel().getId());
song.setNumber(trackNumber);
trackNumber++;
downloader.accept(song);
}
private void onRestartCommand(GuildMessageReceivedEvent event)
{
// TODO: this needs to clear the current data queue
boolean ok = musicHandler.restartSong();
if (ok)
event.getChannel().sendMessage("Restarted current song!").queue();
else
event.getChannel().sendMessage("Cannot restart!").queue();
}
private void onSkipCommand(GuildMessageReceivedEvent event)
{
boolean ok = musicHandler.nextSong(true);
if (ok)
event.getChannel().sendMessage("Skipped to next song!").queue();
else
event.getChannel().sendMessage("There's no more songs!").queue();
}
private void onQueueCommand(GuildMessageReceivedEvent event, Guild guild)
{
String message = ">>> ";
int i = 1;
final Song currentSong = musicHandler.getCurrentSong();
if (currentSong != null)
message += ":notes: **Now playing: " + currentSong + "**\n";
else
message += ":mute: **Not playing anything right now.**\n";
final Queue<Song> songQueue = musicHandler.getSongQueue();
if (!songQueue.isEmpty())
{
message += "__Ready to play:__\n";
for (Song song : songQueue)
{
message += ":bread: **" + (i) + ":** " + song + "\n";
i++;
}
}
final List<Song> downloadQueue = downloader.getDownloadQueue();
if (!downloadQueue.isEmpty())
{
message += "__Downloading:__\n";
for (Song song : downloadQueue)
{
message += ":inbox_tray: **" + (i) + ":** " + song + "\n";
i++;
}
}
if (downloadQueue.isEmpty() && songQueue.isEmpty())
message += ":mailbox_with_no_mail: The track queue is empty.";
// :inbox_tray:
event.getChannel().sendMessage(message).queue();
}
private void onRemoveCommand(GuildMessageReceivedEvent event, Guild guild, String arg)
{
try
{
int i = Integer.parseInt(arg);
boolean removed = musicHandler.removeSong(i - 1);
final int size = musicHandler.getSongQueue().size();
if (removed)
event.getChannel().sendMessage("Song removed.").queue();
else if (size > 1)
event.getChannel().sendMessage("That's not a number between 1 and " + size + "!").queue();
else if (size == 1)
event.getChannel().sendMessage("There's only one song to remove and that's not one of them!").queue();
} catch (NumberFormatException ex)
{
event.getChannel().sendMessage(arg + " isn't a number!").queue();
}
}
private void onHelpCommand(GuildMessageReceivedEvent event)
{
String help = "Commands available:\n"
+ "!join <Channel> - Joins a voice channel\n"
+ "!play <URL> - Downloads the track at that URL and adds it to the queue.\n"
+ "!queue - Show the songs currently playing and the current queue.\n"
+ "!remove <Index> - Remove the song at position <Index> from the queue.\n"
+ "!skip - Skip the current song and play the next one.\n"
+ "!restart - Try playing the current song again in case it goes wrong.\n";
event.getChannel().sendMessage(help).queue();
}
/**
* Inform user about successful connection.
*
* @param channel
* The voice channel we connected to
* @param textChannel
* The text channel to send the message in
*/
private void onConnecting(VoiceChannel channel, TextChannel textChannel)
{
textChannel.sendMessage("Connecting to " + channel.getName()).queue(); // never forget to queue()!
}
/**
* The channel to connect to is not known to us.
*
* @param channel
* The message channel (text channel abstraction) to send failure
* information to
* @param comment
* The information of this channel
*/
private void onUnknownChannel(MessageChannel channel, String comment)
{
channel.sendMessage("Unable to connect to ``" + comment + "``, no such channel!").queue(); // never forget to queue()!
}
/**
* Connect to requested channel and start echo handler
*
* @param channel
* The channel to connect to
*/
private void connectTo(VoiceChannel channel)
{
Guild guild = channel.getGuild();
// Get an audio manager for this guild, this will be created upon first use for each guild
AudioManager audioManager = guild.getAudioManager();
musicHandler = new MusicHandler();
downloader.setNext(musicHandler);
// Create our Send/Receive handler for the audio connection
// EchoHandler handler = new EchoHandler();
// The order of the following instructions does not matter!
// Set the sending handler to our echo system
audioManager.setSendingHandler(musicHandler);
// Set the receiving handler to the same echo system, otherwise we can't echo anything
// audioManager.setReceivingHandler(handler);
// Connect to the voice channel
audioManager.openAudioConnection(channel);
}
}