v0.1.11 Cleanup
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package com.bensherriff.siren.audio;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
||||
import com.sedmelluq.discord.lavaplayer.player.event.AudioEvent;
|
||||
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
|
||||
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
|
||||
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
|
||||
import com.sedmelluq.discord.lavaplayer.track.playback.MutableAudioFrame;
|
||||
import net.dv8tion.jda.api.audio.AudioSendHandler;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
@@ -38,15 +40,19 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
|
||||
}
|
||||
|
||||
public void addTrack(AudioTrack track) {
|
||||
if (!player.startTrack(track, true)) {
|
||||
if (!queue.offer(track)) {
|
||||
LOGGER.error("Failed to queue track {}", track.getInfo().title);
|
||||
}
|
||||
if (player.getPlayingTrack() == null) {
|
||||
player.playTrack(track);
|
||||
} else {
|
||||
LOGGER.debug("Track '{}' has been queued", track.getInfo().title);
|
||||
if (!queue.offer(track)) {
|
||||
LOGGER.error("Failed to queue track {}", track.getInfo().title);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void nextTrack() {
|
||||
player.startTrack(queue.poll(), false);
|
||||
LOGGER.debug("Playing next track");
|
||||
player.stopTrack();
|
||||
}
|
||||
|
||||
public void setPaused(boolean paused) {
|
||||
@@ -62,47 +68,51 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
|
||||
}
|
||||
|
||||
public void setVolume(int volume) {
|
||||
LOGGER.debug("Set volume to {}", volume);
|
||||
player.setVolume(volume);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerPause(AudioPlayer player) {
|
||||
super.onPlayerPause(player);
|
||||
LOGGER.debug("isPaused: {} for {}", player.isPaused(), player.getPlayingTrack().getInfo().title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerResume(AudioPlayer player) {
|
||||
super.onPlayerResume(player);
|
||||
LOGGER.debug("isPaused: {} for {}", player.isPaused(), player.getPlayingTrack().getInfo().title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackStart(AudioPlayer player, AudioTrack track) {
|
||||
manager.getListener().getJDA().getPresence().setActivity(Activity.listening(track.getInfo().title));
|
||||
super.onTrackStart(player, track);
|
||||
LOGGER.debug("Starting track {}", track.getInfo().title);
|
||||
manager.getListener().getJDA().getPresence().setActivity(Activity.playing(track.getInfo().title));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
|
||||
LOGGER.debug("Track ended due to {}; {} ", endReason.name(), endReason.mayStartNext ? "starting next track" : "closing connection");
|
||||
if (endReason.mayStartNext) {
|
||||
nextTrack();
|
||||
} else {
|
||||
queue.clear();
|
||||
LOGGER.debug("Track ended due to {}; {} ", endReason.name(), endReason.mayStartNext);
|
||||
if (queue.isEmpty()) {
|
||||
manager.getListener().closeAudioConnection(guildID);
|
||||
manager.getListener().getJDA().getPresence().setActivity(Activity.playing("nothing"));
|
||||
} else {
|
||||
player.playTrack(queue.poll());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(AudioEvent event) {
|
||||
super.onEvent(event);
|
||||
// LOGGER.trace("On event {}", event.getClass().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
|
||||
LOGGER.warn("Exception on track '{}': {}", track.getInfo().title, exception.getMessage());
|
||||
super.onTrackException(player, track, exception);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackStuck(AudioPlayer player, AudioTrack track, long thresholdMs) {
|
||||
LOGGER.warn("{} - 'track {}' is stuck", guildID, track.getInfo().title);
|
||||
super.onTrackStuck(player, track, thresholdMs);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,13 +27,11 @@ public class Listener extends ListenerAdapter {
|
||||
private static final Logger LOGGER = LogManager.getLogger(Listener.class);
|
||||
|
||||
private final PlayerManager playerManager;
|
||||
private final Map<Long, AudioHandler> musicManagers;
|
||||
private final Settings settings;
|
||||
private final Map<String, Command> commands = new HashMap<>();
|
||||
private JDA jda;
|
||||
|
||||
public Listener(Settings settings) {
|
||||
this.musicManagers = new HashMap<>();
|
||||
this.settings = settings;
|
||||
|
||||
this.playerManager = new PlayerManager(this);
|
||||
@@ -51,18 +49,10 @@ public class Listener extends ListenerAdapter {
|
||||
return playerManager;
|
||||
}
|
||||
|
||||
public Map<Long, AudioHandler> getMusicManagers() {
|
||||
return musicManagers;
|
||||
}
|
||||
|
||||
public Settings getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
public Map<String, Command> getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
public JDA getJDA() {
|
||||
return jda;
|
||||
}
|
||||
@@ -95,20 +85,18 @@ public class Listener extends ListenerAdapter {
|
||||
|
||||
public synchronized AudioHandler getGuildAudioPlayer(Guild guild) throws IOException {
|
||||
long guildId = Long.parseLong(guild.getId());
|
||||
AudioHandler audioHandler = musicManagers.get(guildId);
|
||||
AudioHandler audioHandler;
|
||||
|
||||
if (audioHandler == null) {
|
||||
if (guild.getAudioManager().getSendingHandler() == null) {
|
||||
LOGGER.info("Creating Audio Handler for guild {}", guildId);
|
||||
if (!settings.getGuildSettings().containsKey(guildId)) {
|
||||
settings.getGuildSettings().put(guildId, new GuildSettings());
|
||||
SettingsManager.write(settings);
|
||||
}
|
||||
audioHandler = new AudioHandler(playerManager, guildId);
|
||||
musicManagers.put(guildId, audioHandler);
|
||||
}
|
||||
|
||||
if (!audioHandler.equals(guild.getAudioManager().getSendingHandler())) {
|
||||
guild.getAudioManager().setSendingHandler(audioHandler);
|
||||
} else {
|
||||
audioHandler = (AudioHandler) guild.getAudioManager().getSendingHandler();
|
||||
}
|
||||
|
||||
return audioHandler;
|
||||
@@ -132,7 +120,7 @@ public class Listener extends ListenerAdapter {
|
||||
|
||||
try {
|
||||
if (commands.containsKey(command)) {
|
||||
commands.get(command).doCommand(event);
|
||||
commands.get(command).execute(event);
|
||||
} else {
|
||||
event.getHook().sendMessage("Unexpected command received.").queue();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public abstract class Command {
|
||||
public Command(Listener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
public abstract void doCommand(SlashCommandInteractionEvent event) throws IOException;
|
||||
public abstract void execute(SlashCommandInteractionEvent event) throws IOException;
|
||||
|
||||
public SlashCommandData getSlashCommandData() {
|
||||
return slashCommandData;
|
||||
|
||||
@@ -16,7 +16,7 @@ public class PauseCommand extends Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCommand(SlashCommandInteractionEvent event) throws IOException {
|
||||
public void execute(SlashCommandInteractionEvent event) throws IOException {
|
||||
Guild guild = getGuild(event);
|
||||
AudioHandler audioHandler = listener.getGuildAudioPlayer(guild);
|
||||
if (audioHandler.isPaused()) {
|
||||
|
||||
@@ -24,59 +24,74 @@ public class PlayCommand extends Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCommand(SlashCommandInteractionEvent event) throws IOException {
|
||||
String userID = event.getUser().getId();
|
||||
Guild guild = getGuild(event);
|
||||
public void execute(SlashCommandInteractionEvent event) throws IOException {
|
||||
String trackURL = Objects.requireNonNull(event.getOption("url")).getAsString();
|
||||
AudioHandler audioHandler = listener.getGuildAudioPlayer(guild);
|
||||
|
||||
listener.getPlayerManager().loadItemOrdered(audioHandler, trackURL, new AudioLoadResultHandler() {
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack track) {
|
||||
try {
|
||||
playTrack(guild, userID, audioHandler, track);
|
||||
event.getHook().sendMessage("Adding **" + track.getInfo().title + "** to queue...").queue();
|
||||
} catch (EmptyVoiceChannelException e) {
|
||||
event.getHook().sendMessage("You must be connected to a voice channel in order to play tracks!").queue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
AudioTrack firstTrack = playlist.getSelectedTrack();
|
||||
|
||||
if (firstTrack == null) {
|
||||
firstTrack = playlist.getTracks().get(0);
|
||||
}
|
||||
|
||||
try {
|
||||
playTrack(guild, userID, audioHandler, firstTrack);
|
||||
event.getHook().sendMessage("Adding **" + firstTrack.getInfo().title + "** to queue (first track of playlist " + playlist.getName() + ")...").queue();
|
||||
} catch (EmptyVoiceChannelException e) {
|
||||
event.getHook().sendMessage("You must be connected to a voice channel in order to play tracks!").queue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
event.getHook().sendMessage("Nothing found by " + trackURL).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFailed(FriendlyException exception) {
|
||||
String msg = "Failed to play track";
|
||||
if (exception.getMessage().contains("Unknown file format")) {
|
||||
event.getHook().sendMessage(msg + ". " + exception.getMessage()).queue();
|
||||
} else {
|
||||
event.getHook().sendMessage(msg + ". Please contact your administrator.").queue();
|
||||
}
|
||||
LOGGER.error("{}: {}", msg, exception.getMessage());
|
||||
}
|
||||
});
|
||||
listener.getPlayerManager().loadItemOrdered(event.getGuild(), trackURL, new ResultHandler(event));
|
||||
}
|
||||
|
||||
public void playTrack(Guild guild, String userID, AudioHandler audioHandler, AudioTrack track) throws EmptyVoiceChannelException {
|
||||
listener.connectToVoiceChannel(userID, guild.getAudioManager());
|
||||
audioHandler.addTrack(track);
|
||||
private class ResultHandler implements AudioLoadResultHandler {
|
||||
|
||||
private final Guild guild;
|
||||
private final String userID;
|
||||
private final AudioHandler audioHandler;
|
||||
private final SlashCommandInteractionEvent event;
|
||||
|
||||
private ResultHandler(SlashCommandInteractionEvent event) throws IOException {
|
||||
this.event = event;
|
||||
this.userID = event.getUser().getId();
|
||||
this.guild = getGuild(event);
|
||||
this.audioHandler = listener.getGuildAudioPlayer(guild);
|
||||
}
|
||||
|
||||
private void playTrack(Guild guild, String userID, AudioHandler audioHandler, AudioTrack track) throws EmptyVoiceChannelException {
|
||||
listener.connectToVoiceChannel(userID, guild.getAudioManager());
|
||||
audioHandler.addTrack(track);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack track) {
|
||||
try {
|
||||
playTrack(guild, userID, audioHandler, track);
|
||||
event.getHook().sendMessage("Adding **" + track.getInfo().title + "** to queue...").queue();
|
||||
} catch (EmptyVoiceChannelException e) {
|
||||
event.getHook().sendMessage("You must be connected to a voice channel in order to play tracks!").queue();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
AudioTrack firstTrack = playlist.getSelectedTrack();
|
||||
|
||||
if (firstTrack == null) {
|
||||
firstTrack = playlist.getTracks().get(0);
|
||||
}
|
||||
|
||||
try {
|
||||
playTrack(guild, userID, audioHandler, firstTrack);
|
||||
event.getHook().sendMessage("Adding **" + firstTrack.getInfo().title + "** to queue (first track of playlist " + playlist.getName() + ")...").queue();
|
||||
} catch (EmptyVoiceChannelException e) {
|
||||
event.getHook().sendMessage("You must be connected to a voice channel in order to play tracks!").queue();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
event.getHook().sendMessage("Nothing found at that URL").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFailed(FriendlyException exception) {
|
||||
String errorMsg = "Failed to play track";
|
||||
if (exception.getMessage().contains("Unknown file format")) {
|
||||
event.getHook().sendMessage(errorMsg + ". " + exception.getMessage()).queue();
|
||||
} else {
|
||||
event.getHook().sendMessage(errorMsg + ". Please contact your administrator.").queue();
|
||||
}
|
||||
LOGGER.error("{}: {}", errorMsg, exception.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ public class ResumeCommand extends Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCommand(SlashCommandInteractionEvent event) throws IOException {
|
||||
public void execute(SlashCommandInteractionEvent event) throws IOException {
|
||||
Guild guild = getGuild(event);
|
||||
AudioHandler audioHandler = listener.getGuildAudioPlayer(guild);
|
||||
if (audioHandler.isPaused()) {
|
||||
|
||||
@@ -16,7 +16,7 @@ public class SkipCommand extends Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCommand(SlashCommandInteractionEvent event) throws IOException {
|
||||
public void execute(SlashCommandInteractionEvent event) throws IOException {
|
||||
Guild guild = getGuild(event);
|
||||
AudioHandler audioHandler = listener.getGuildAudioPlayer(guild);
|
||||
audioHandler.nextTrack();
|
||||
|
||||
@@ -16,7 +16,7 @@ public class StopCommand extends Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCommand(SlashCommandInteractionEvent event) throws IOException {
|
||||
public void execute(SlashCommandInteractionEvent event) throws IOException {
|
||||
Guild guild = getGuild(event);
|
||||
AudioHandler audioHandler = listener.getGuildAudioPlayer(guild);
|
||||
audioHandler.stopTrack();
|
||||
|
||||
@@ -20,7 +20,7 @@ public class VolumeCommand extends Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCommand(SlashCommandInteractionEvent event) throws IOException {
|
||||
public void execute(SlashCommandInteractionEvent event) throws IOException {
|
||||
Guild guild = getGuild(event);
|
||||
int volume = Objects.requireNonNull(event.getOption("volume")).getAsInt();
|
||||
listener.getSettings().getGuildSettings().get(guild.getIdLong()).setVolume(volume);
|
||||
|
||||
Reference in New Issue
Block a user