public final class ThreadLocalReason
extends java.lang.Object
AuditableRestAction
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.
String previousReason = ThreadLocalReason.getCurrent();
ThreadLocalReason.setCurrent("Hello World");
try {
GuildController c = guild.getController();
c.ban(user, 0).queue(v -> {
c.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
c.kick(user).queue();
try (ThreadLocalReason.Closable __ = ThreadLocalReason.closable("Hello World")) {
GuildController c = guild.getController();
c.ban(user, 0).queue(v -> {
c.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
c.kick(user).queue();
AuditableRestAction.reason(String)
,
ThreadLocal
Modifier and Type | Class | Description |
---|---|---|
static class |
ThreadLocalReason.Closable |
Allows to use try-with-resources blocks for setting reasons
|
Modifier and Type | Method | Description |
---|---|---|
static ThreadLocalReason.Closable |
closable(java.lang.String reason) |
Creates a new
ThreadLocalReason.Closable instance. |
static java.lang.String |
getCurrent() |
The current reason that should be used for
AuditableRestAction . |
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 for
AuditableRestAction . |
public static void setCurrent(@Nullable java.lang.String reason)
AuditableRestAction
.reason
- The reason to use, or null
to resetpublic static void resetCurrent()
@Nullable public static java.lang.String getCurrent()
AuditableRestAction
.public static ThreadLocalReason.Closable closable(java.lang.String reason)
ThreadLocalReason.Closable
instance.
reason
- The reason to use