84 lines
2.9 KiB
Java
84 lines
2.9 KiB
Java
package me.bobert.netneutrality.music;
|
|
/*
|
|
* Copyright (c) anthorob
|
|
*
|
|
* https://discord.gg/qUa6zNFZsd
|
|
*/
|
|
|
|
|
|
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
|
|
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
|
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
|
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
|
import me.bobert.netneutrality.core.LogHandler;
|
|
import me.bobert.netneutrality.core.context.CommandContext;
|
|
import me.bobert.netneutrality.tasks.music.LoadPlaylistTask;
|
|
import me.bobert.netneutrality.utils.MessageFunction;
|
|
import me.bobert.netneutrality.utils.MusicUtils;
|
|
import net.dv8tion.jda.api.entities.Guild;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
public class TrackLoadingHandler implements AudioLoadResultHandler {
|
|
private static final Logger logger = LogManager.getLogger(TrackLoadingHandler.class);
|
|
|
|
private final String identifier;
|
|
private final SourceFinder.AudioSourceType type;
|
|
private final boolean silent;
|
|
private final Guild guild;
|
|
private final QueueManager queueManager;
|
|
private final CommandContext context;
|
|
|
|
public TrackLoadingHandler(CommandContext context, MusicManager manager, String identifier, SourceFinder.AudioSourceType type, boolean silent) {
|
|
this.identifier = identifier;
|
|
this.type = type;
|
|
this.silent = silent;
|
|
this.guild = manager.getGuild();
|
|
this.queueManager = manager.getQueueManager();
|
|
this.context = context;
|
|
}
|
|
|
|
@Override
|
|
public void trackLoaded(AudioTrack audioTrack) {
|
|
logger.debug("Guild {}: track loaded: {}", guild.getId(), audioTrack.getIdentifier());
|
|
|
|
context.deleteMessage();
|
|
if (MusicUtils.connect(context)) {
|
|
queueManager.queue(audioTrack, context.getGuildUser());
|
|
|
|
if (!silent) {
|
|
context.reply(MessageFunction.buildAddingTrackMessage(audioTrack, context.getGuildUser(), type));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void playlistLoaded(AudioPlaylist playlist) {
|
|
logger.debug("Guild {}: playlist loaded: {}", guild.getId(), identifier);
|
|
|
|
new LoadPlaylistTask(playlist, type).insert(context);
|
|
}
|
|
|
|
@Override
|
|
public void noMatches() {
|
|
logger.debug("Guild {}: no matches for identifier {}", guild.getId(), identifier);
|
|
|
|
if (!silent) {
|
|
context.reply("No matches found");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadFailed(FriendlyException e) {
|
|
logger.debug("Guild {}: load failed for identifier {}", guild.getId(), identifier);
|
|
|
|
//TODO: better error messages
|
|
RuntimeException exception = new RuntimeException("Failed to load track: " + e.getMessage(), e);
|
|
LogHandler.log(exception);
|
|
|
|
if (!silent) {
|
|
context.reply(MessageFunction.buildSentryErrorMessage(exception));
|
|
}
|
|
}
|
|
}
|