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
+52 -32

No files matched your search

+8 -2
View File
@@ -96,6 +96,12 @@
<url>https://jitpack.io</url> <url>https://jitpack.io</url>
</repository> </repository>
<repository>
<id>arbjergDev-snapshots</id>
<name>Lavalink Repository</name>
<url>https://maven.lavalink.dev/snapshots</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@@ -113,7 +119,7 @@
<dependency> <dependency>
<groupId>dev.arbjerg</groupId> <groupId>dev.arbjerg</groupId>
<artifactId>lavaplayer</artifactId> <artifactId>lavaplayer</artifactId>
<version>2.1.1</version> <version>0eaeee195f0315b2617587aa3537fa202df07ddc-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
@@ -167,7 +173,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId> <artifactId>log4j-slf4j-impl</artifactId>
<version>2.23.1</version> <version>2.23.1</version>
</dependency> </dependency>
@@ -27,16 +27,18 @@ public class LogHandler {
* @param e The exceptions to log * @param e The exceptions to log
*/ */
public static void log(Exception e) { public static void log(Exception e) {
if (e instanceof CommandExecutionException) {
Sentry.captureException(e); Sentry.captureException(e);
logInternally((CommandExecutionException) e);
tryLogToDebugChannel((CommandExecutionException) e); if (e instanceof CommandExecutionException exception) {
tryGracefulSlashContextError((CommandExecutionException) e); CommandContext context = exception.getCommandContext();
logInternally(exception, context);
tryLogToDebugChannel(exception, context);
tryGracefulSlashContextError(exception, context);
return; return;
} }
Sentry.captureException(e);
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
@@ -59,14 +61,11 @@ public class LogHandler {
} }
} }
private static void logInternally(CommandException error) { private static void logInternally(CommandException exception, CommandContext context) {
CommandContext context = error.getCommandContext(); logger.error("Exception thrown from command [{}] in guild [{}]: {}", context.getCommand().getCommandName(), context.getGuild().getId(), exception.getMessage(), exception);
logger.error("Exception thrown from command [{}] in guild [{}]: {}", context.getCommand().getCommandName(), context.getGuild().getId(), error.getMessage(), error);
} }
private static void tryLogToDebugChannel(CommandException error) { private static void tryLogToDebugChannel(CommandException exception, CommandContext context) {
CommandContext context = error.getCommandContext();
if (context.getGuild() != null) { if (context.getGuild() != null) {
if (GuildManager.getManager(context.getGuild()).isDebugEnabled()) { if (GuildManager.getManager(context.getGuild()).isDebugEnabled()) {
context.getGuild() context.getGuild()
@@ -74,16 +73,14 @@ public class LogHandler {
.stream() .stream()
.filter(textChannel -> textChannel.getName().equals("debug") || textChannel.getName().equals("log")) .filter(textChannel -> textChannel.getName().equals("debug") || textChannel.getName().equals("log"))
.filter(TextChannel::canTalk) .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) { private static void tryGracefulSlashContextError(CommandException exception, CommandContext context) {
CommandContext context = error.getCommandContext();
if (context instanceof SlashCommandContext slashCommandContext) { 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)); replyToMessage(String.format(format, arguments));
} }
default void replyToMessage(MessageEmbed... embeds) {
replyToMessage(MessageCreateData.fromEmbeds(embeds));
}
void dispatchToCommand(String commandName); void dispatchToCommand(String commandName);
} }
@@ -138,6 +138,10 @@ public class SlashCommandContext implements CommandContext {
event.reply(message).setEphemeral(true).queue(); event.reply(message).setEphemeral(true).queue();
} }
public void replyEphemeral(MessageEmbed... embeds) {
replyEphemeral(MessageCreateData.fromEmbeds(embeds));
}
public void replyEphemeral(String format, Object... arguments) { public void replyEphemeral(String format, Object... arguments) {
replyEphemeral(String.format(format, arguments)); replyEphemeral(String.format(format, arguments));
} }
@@ -56,9 +56,8 @@ public class TrackLoadingHandler implements AudioLoadResultHandler {
loadConsumer.accept(true); loadConsumer.accept(true);
} }
if (loadingDisabled) { if (loadingDisabled)
return; return;
}
context.deleteMessage(); context.deleteMessage();
@@ -76,9 +75,9 @@ public class TrackLoadingHandler implements AudioLoadResultHandler {
loadConsumer.accept(true); loadConsumer.accept(true);
} }
if (loadingDisabled) { if (loadingDisabled)
return; return;
}
logger.debug("Guild {}: playlist loaded: {}", guild.getId(), identifier); logger.debug("Guild {}: playlist loaded: {}", guild.getId(), identifier);
if (!playlist.getTracks().isEmpty()) { if (!playlist.getTracks().isEmpty()) {
@@ -110,24 +109,22 @@ public class TrackLoadingHandler implements AudioLoadResultHandler {
@Override @Override
public void noMatches() { public void noMatches() {
logger.debug("Guild {}: no matches for identifier {}", guild.getId(), identifier); if (loadConsumer != null)
loadConsumer.accept(false);
if (!silent) if (!silent)
context.replyToMessage("No matches found"); context.replyToMessage("No matches found");
if (loadConsumer != null)
loadConsumer.accept(false);
} }
@Override @Override
public void loadFailed(FriendlyException e) { public void loadFailed(FriendlyException e) {
if (loadConsumer != null)
loadConsumer.accept(false);
logger.debug("Guild {}: load failed for identifier {}", guild.getId(), identifier, e); logger.debug("Guild {}: load failed for identifier {}", guild.getId(), identifier, e);
LogHandler.log(e); LogHandler.log(e);
if (!silent) if (!silent)
context.replyToMessage("Failed to load track: " + e.getMessage()); context.replyToMessage(MessageFunction.buildSentryErrorMessage("Failed to load track: " + e.getMessage()));
if (loadConsumer != null)
loadConsumer.accept(false);
} }
} }
@@ -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.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import netneutrality.core.LogHandler; import netneutrality.core.LogHandler;
import netneutrality.utils.MessageFunction;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -119,7 +120,7 @@ public class TrackScheduler extends AudioEventAdapter {
public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) { public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
logger.debug("Guild {}: exception while playing track [{}]: {}", guild.getId(), track.getIdentifier(), exception.getMessage()); logger.debug("Guild {}: exception while playing track [{}]: {}", guild.getId(), track.getIdentifier(), exception.getMessage());
LogHandler.log(exception); 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 @Override
@@ -10,6 +10,7 @@ package netneutrality.utils;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import io.sentry.Sentry;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
@@ -208,6 +209,17 @@ public class MessageFunction {
return messages; 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) { public static MessageEmbed buildDebugMessage(CommandContext context, CommandException error) {
EmbedBuilder message = new EmbedBuilder(); EmbedBuilder message = new EmbedBuilder();
message.setColor(Color.RED); message.setColor(Color.RED);
@@ -16,7 +16,6 @@ public class MusicUtils {
public static boolean checkIfConnected(MusicManager manager, CommandContext context) { public static boolean checkIfConnected(MusicManager manager, CommandContext context) {
if (context.getGuildUser().getVoiceState() == null) { 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); throw new CommandExecutionException("Voice state is null, this should not happen", context, null);
} }