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