Add more useful logs in reply and use snapshot of lavalink to temporarily fix player issues

This commit is contained in:
anthorob committed 2024-03-30 15:14:40 -04:00
1 parent 23108fb2d9
commit e6be74ad53
8 files changed
+53 -33

No files matched your search

@@ -27,16 +27,18 @@ public class LogHandler {
* @param e The exceptions to log
*/
public static void log(Exception e) {
if (e instanceof CommandExecutionException) {
Sentry.captureException(e);
logInternally((CommandExecutionException) e);
tryLogToDebugChannel((CommandExecutionException) e);
tryGracefulSlashContextError((CommandExecutionException) e);
Sentry.captureException(e);
if (e instanceof CommandExecutionException exception) {
CommandContext context = exception.getCommandContext();
logInternally(exception, context);
tryLogToDebugChannel(exception, context);
tryGracefulSlashContextError(exception, context);
return;
}
Sentry.captureException(e);
logger.error(e.getMessage(), e);
}
@@ -59,14 +61,11 @@ public class LogHandler {
}
}
private static void logInternally(CommandException error) {
CommandContext context = error.getCommandContext();
logger.error("Exception thrown from command [{}] in guild [{}]: {}", context.getCommand().getCommandName(), context.getGuild().getId(), error.getMessage(), error);
private static void logInternally(CommandException exception, CommandContext context) {
logger.error("Exception thrown from command [{}] in guild [{}]: {}", context.getCommand().getCommandName(), context.getGuild().getId(), exception.getMessage(), exception);
}
private static void tryLogToDebugChannel(CommandException error) {
CommandContext context = error.getCommandContext();
private static void tryLogToDebugChannel(CommandException exception, CommandContext context) {
if (context.getGuild() != null) {
if (GuildManager.getManager(context.getGuild()).isDebugEnabled()) {
context.getGuild()
@@ -74,16 +73,14 @@ public class LogHandler {
.stream()
.filter(textChannel -> textChannel.getName().equals("debug") || textChannel.getName().equals("log"))
.filter(TextChannel::canTalk)
.forEach(textChannel -> textChannel.sendMessageEmbeds(MessageFunction.buildDebugMessage(context, error)).queue());
.forEach(textChannel -> textChannel.sendMessageEmbeds(MessageFunction.buildDebugMessage(context, exception)).queue());
}
}
}
private static void tryGracefulSlashContextError(CommandExecutionException error) {
CommandContext context = error.getCommandContext();
private static void tryGracefulSlashContextError(CommandException exception, CommandContext context) {
if (context instanceof SlashCommandContext slashCommandContext) {
slashCommandContext.replyEphemeral("Error during command execution");
slashCommandContext.reply(MessageFunction.buildSentryErrorMessage(exception.getMessage()));
}
}
}
@@ -53,5 +53,9 @@ public interface CommandContext {
replyToMessage(String.format(format, arguments));
}
default void replyToMessage(MessageEmbed... embeds) {
replyToMessage(MessageCreateData.fromEmbeds(embeds));
}
void dispatchToCommand(String commandName);
}
@@ -138,6 +138,10 @@ public class SlashCommandContext implements CommandContext {
event.reply(message).setEphemeral(true).queue();
}
public void replyEphemeral(MessageEmbed... embeds) {
replyEphemeral(MessageCreateData.fromEmbeds(embeds));
}
public void replyEphemeral(String format, Object... arguments) {
replyEphemeral(String.format(format, arguments));
}
@@ -56,9 +56,8 @@ public class TrackLoadingHandler implements AudioLoadResultHandler {
loadConsumer.accept(true);
}
if (loadingDisabled) {
if (loadingDisabled)
return;
}
context.deleteMessage();
@@ -76,9 +75,9 @@ public class TrackLoadingHandler implements AudioLoadResultHandler {
loadConsumer.accept(true);
}
if (loadingDisabled) {
if (loadingDisabled)
return;
}
logger.debug("Guild {}: playlist loaded: {}", guild.getId(), identifier);
if (!playlist.getTracks().isEmpty()) {
@@ -110,24 +109,22 @@ public class TrackLoadingHandler implements AudioLoadResultHandler {
@Override
public void noMatches() {
logger.debug("Guild {}: no matches for identifier {}", guild.getId(), identifier);
if (loadConsumer != null)
loadConsumer.accept(false);
if (!silent)
context.replyToMessage("No matches found");
if (loadConsumer != null)
loadConsumer.accept(false);
}
@Override
public void loadFailed(FriendlyException e) {
if (loadConsumer != null)
loadConsumer.accept(false);
logger.debug("Guild {}: load failed for identifier {}", guild.getId(), identifier, e);
LogHandler.log(e);
if (!silent)
context.replyToMessage("Failed to load track: " + e.getMessage());
if (loadConsumer != null)
loadConsumer.accept(false);
context.replyToMessage(MessageFunction.buildSentryErrorMessage("Failed to load track: " + e.getMessage()));
}
}
@@ -16,6 +16,7 @@ import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import netneutrality.core.LogHandler;
import netneutrality.utils.MessageFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -119,7 +120,7 @@ public class TrackScheduler extends AudioEventAdapter {
public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
logger.debug("Guild {}: exception while playing track [{}]: {}", guild.getId(), track.getIdentifier(), exception.getMessage());
LogHandler.log(exception);
sendMessageToChannel("Error while playing track [" + track.getInfo().title + "]: " + exception.getMessage());
sendMessageToChannel(MessageFunction.buildSentryErrorMessage(String.format("Error while playing track [%s]: %s", track.getInfo().title, exception.getMessage())));
}
@Override
@@ -10,6 +10,7 @@ package netneutrality.utils;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import io.sentry.Sentry;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
@@ -208,6 +209,17 @@ public class MessageFunction {
return messages;
}
public static MessageEmbed buildSentryErrorMessage(String error) {
EmbedBuilder message = new EmbedBuilder();
message.setColor(Color.RED);
message.setTitle(":warning: Error during execution :warning:");
message.addField("Error message:", error, false);
message.addField("Error ID:", Sentry.getLastEventId().toString(), false);
message.setTimestamp(OffsetDateTime.now());
return message.build();
}
public static MessageEmbed buildDebugMessage(CommandContext context, CommandException error) {
EmbedBuilder message = new EmbedBuilder();
message.setColor(Color.RED);
@@ -16,7 +16,6 @@ public class MusicUtils {
public static boolean checkIfConnected(MusicManager manager, CommandContext context) {
if (context.getGuildUser().getVoiceState() == null) {
context.reply("Voice state is null, this should not happen");
throw new CommandExecutionException("Voice state is null, this should not happen", context, null);
}