Package net.dv8tion.jda.api.audit
Class ThreadLocalReason
- java.lang.Object
-
- net.dv8tion.jda.api.audit.ThreadLocalReason
-
public final class ThreadLocalReason extends java.lang.Object
Thread-Local audit-log reason used automatically byAuditableRestAction
instances when no other reason was set.Note that
RestAction.queue()
will forward any thread-local reason set through this handle. Thus audit-log reasons done by callbacks will also use the one set from the executing thread.Example without closable
String previousReason = ThreadLocalReason.getCurrent(); ThreadLocalReason.setCurrent("Hello World"); try { guild.ban(user, 0).queue(v -> { guild.unban(user).queue(); // also uses the reason "Hello World" }); } finally { //Forwarding the reason is not async so resetting it here is fine. ThreadLocalReason.setCurrent(previousReason); } //This will not use the reason "Hello World" but the previous, or none if none was set previously guild.kick(user).queue();
Example with closable
try (ThreadLocalReason.Closable __ = ThreadLocalReason.closable("Hello World")) { guild.ban(user, 0).queue(v -> { guild.unban(user).queue(); // also uses the reason "Hello World" }); } // automatically changes reason back //This will not use the reason "Hello World" but the previous, or none if none was set previously guild.kick(user).queue();
- See Also:
AuditableRestAction.reason(String)
,ThreadLocal
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ThreadLocalReason.Closable
Allows to use try-with-resources blocks for setting reasons
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ThreadLocalReason.Closable
closable(java.lang.String reason)
Creates a newThreadLocalReason.Closable
instance.static java.lang.String
getCurrent()
The current reason that should be used forAuditableRestAction
.static void
resetCurrent()
Resets the currently set thread-local reason, if present.static void
setCurrent(java.lang.String reason)
Sets the current reason that should be used forAuditableRestAction
.
-
-
-
Method Detail
-
setCurrent
public static void setCurrent(@Nullable java.lang.String reason)
Sets the current reason that should be used forAuditableRestAction
.- Parameters:
reason
- The reason to use, ornull
to reset
-
resetCurrent
public static void resetCurrent()
Resets the currently set thread-local reason, if present.
-
getCurrent
@Nullable public static java.lang.String getCurrent()
The current reason that should be used forAuditableRestAction
.- Returns:
- The current thread-local reason, or null
-
closable
@Nonnull public static ThreadLocalReason.Closable closable(@Nullable java.lang.String reason)
Creates a newThreadLocalReason.Closable
instance.
Allows to use try-with-resources blocks for setting reasons- Parameters:
reason
- The reason to use- Returns:
- The closable instance
-
-