Add unique ID for each context

This commit is contained in:
anthorob committed 2024-12-14 01:07:24 -05:00
1 parent 50430589e6
commit 3314ef6bea
6 files changed
+43 -22

No files matched your search

@@ -39,6 +39,15 @@ public class AsyncContext extends AbstractContext {
return null; return null;
} }
@Override
public String getId() {
if (hook != null) {
return hook.getInteraction().getId();
}
return String.format("async:%s", message.getId());
}
@Override @Override
public ChannelType getType() { public ChannelType getType() {
return ChannelType.UNKNOWN; return ChannelType.UNKNOWN;
@@ -43,6 +43,15 @@ public class MessageCommandContext extends CommandContext {
return event; return event;
} }
@Override
public String getId() {
return String.format("%s:%s:%s:%s",
getGuild() == null ? "PrivateMessage" : getGuild().getId(),
getChannel().getId(),
getDiscordUser().getId(),
message.getId());
}
@Override @Override
public ChannelType getType() { public ChannelType getType() {
return event.getChannelType(); return event.getChannelType();
@@ -29,6 +29,11 @@ public class MusicChannelContext extends AbstractContext {
return null; return null;
} }
@Override
public String getId() {
return String.format("music:%s", getChannel().getId());
}
@Override @Override
public ChannelType getType() { public ChannelType getType() {
return getChannel().getType(); return getChannel().getType();
@@ -62,6 +62,7 @@ public class CommandProcessor {
} }
try { try {
logger.info("Context ID: {}", context.getId());
logger.info("Executing command {} on guild {}", command.getCommandName(), context.getGuild() == null ? "PrivateMessage" : context.getGuild().getId()); logger.info("Executing command {} on guild {}", command.getCommandName(), context.getGuild() == null ? "PrivateMessage" : context.getGuild().getId());
configureSentry(command); configureSentry(command);
@@ -217,13 +218,7 @@ public class CommandProcessor {
scope.setTag("canBeExecutedBy", String.valueOf(command.canBeExecutedBy(context.getGuildUser()))); scope.setTag("canBeExecutedBy", String.valueOf(command.canBeExecutedBy(context.getGuildUser())));
} }
if (context instanceof SlashCommandContext slashCommandContext) scope.setTag("contextId", context.getId());
scope.setTag("interactionId", slashCommandContext.getEvent().getInteraction().getId());
if (context instanceof MessageCommandContext messageCommandContext)
scope.setTag("message", messageCommandContext.getMessage().getId());
}); });
} }
} }
@@ -25,6 +25,8 @@ import java.util.function.Consumer;
public interface Context { public interface Context {
Event getEvent(); Event getEvent();
String getId();
ChannelType getType(); ChannelType getType();
JDA getJDA(); JDA getJDA();
@@ -18,6 +18,11 @@ import java.util.function.Consumer;
public interface InteractionContext extends Context { public interface InteractionContext extends Context {
@Override
default String getId() {
return getInteraction().getId();
}
@Override @Override
default InteractionHook getInteractionHook() { default InteractionHook getInteractionHook() {
if (!isAcknowledged()) { if (!isAcknowledged()) {
@@ -43,24 +48,12 @@ public interface InteractionContext extends Context {
@Override @Override
default boolean isFromGuild() { default boolean isFromGuild() {
Event event = getEvent(); return getInteraction().isFromGuild();
try {
Interaction interaction = (Interaction) event;
return interaction.isFromGuild();
} catch (ClassCastException e) {
throw new IllegalArgumentException(event.getClass().getSimpleName() + " does not implement class 'Interaction'", e);
}
} }
@Override @Override
default boolean isAcknowledged() { default boolean isAcknowledged() {
Event event = getEvent(); return getInteraction().isAcknowledged();
try {
Interaction interaction = (Interaction) event;
return interaction.isAcknowledged();
} catch (ClassCastException e) {
throw new IllegalArgumentException(event.getClass().getSimpleName() + " does not implement class 'Interaction'", e);
}
} }
@Override @Override
@@ -77,7 +70,15 @@ public interface InteractionContext extends Context {
try { try {
return (IReplyCallback) getEvent(); return (IReplyCallback) getEvent();
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new IllegalArgumentException(getEvent().getClass().getSimpleName() + " does not implement class 'IReplyCallback'", e); throw new IllegalArgumentException(getEvent().getClass().getName() + " does not implement class 'IReplyCallback'", e);
}
}
private Interaction getInteraction() {
try {
return (Interaction) getEvent();
} catch (ClassCastException e) {
throw new IllegalArgumentException(getEvent().getClass().getName() + " does not implement class 'Interaction'", e);
} }
} }
} }