Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced Cooldown #10

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.mineking.discordutils.commands.condition.IRegistrationCondition;
import de.mineking.discordutils.commands.condition.cooldown.Cooldown;
import de.mineking.discordutils.commands.condition.cooldown.CooldownImpl;
import de.mineking.discordutils.commands.condition.cooldown.CooldownIncrement;
import de.mineking.discordutils.commands.condition.cooldown.CooldownPool;
import de.mineking.discordutils.commands.context.IAutocompleteContext;
import de.mineking.discordutils.commands.context.ICommandContext;
Expand Down Expand Up @@ -34,7 +35,7 @@
* @see CommandManager#registerCommand(Class)
*/
public class AnnotatedCommand<T, C extends ICommandContext, A extends IAutocompleteContext> extends Command<C> {
private final static Map<String, CooldownImpl<?>> cooldowns = new HashMap<>();
public final static Map<String, CooldownImpl<?>> cooldowns = new HashMap<>();

private final Class<T> clazz;
private final ApplicationCommand info;
Expand Down Expand Up @@ -104,7 +105,7 @@ else if(IRegistrationCondition.class.isAssignableFrom(f.getType()))
var cooldown = m.getAnnotation(Cooldown.class);

if(cooldown != null) {
var impl = new CooldownImpl<C>(Duration.ofMillis(cooldown.unit().toMillis(cooldown.interval())), cooldown.uses(), (man, context) -> instance.apply(context).ifPresent(i -> {
var impl = new CooldownImpl<C>(Duration.ofMillis(cooldown.unit().toMillis(cooldown.interval())), cooldown.uses(), cooldown.auto(), (man, context) -> instance.apply(context).ifPresent(i -> {
try {
manager.getManager().invokeMethod(m, i, (x, p) -> {
if(p.getType().isAssignableFrom(context.getClass())) return context;
Expand Down Expand Up @@ -253,8 +254,12 @@ public void performCommand(@NotNull C context) throws Exception {
manager.getManager().invokeMethod(method, instance.get(), (i, p) -> {
if(p.getType().isAssignableFrom(context.getEvent().getClass())) return context.getEvent();
else if(p.getType().isAssignableFrom(context.getClass())) return context;
else if(p.isAnnotationPresent(Option.class))
return manager.parseOption(context.getEvent(), getOptionName(p), p, method.getGenericParameterTypes()[i]);
else if (p.isAnnotationPresent(Option.class)) return manager.parseOption(context.getEvent(), getOptionName(p), p, method.getGenericParameterTypes()[i]);
else if (p.isAnnotationPresent(CooldownIncrement.class)) return (Runnable) () -> getCondition().all().forEach(ec -> {
if (ec instanceof CooldownImpl<C> c) {
c.increment(context.getEvent().getUser().getIdLong());
}
});
else return null;
});
} catch(CommandCancellation ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@
* @see CooldownPool
*/
String identifier() default "";

/**
* Whether to automatically increment the cooldown. Set this to false if you want to only increment the cooldown after validation in your handler method.
*/
boolean auto() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
import java.util.function.BiConsumer;

public class CooldownImpl<C extends ICommandContext> implements IExecutionCondition<C> {
private final Cache<Long, AtomicInteger> cooldown;
public final Cache<Long, AtomicInteger> cooldown;

private final Duration duration;
private final int uses;
public final boolean auto;
public final Duration duration;
public final int uses;

private final BiConsumer<CommandManager<C, ?>, C> handler;
public final BiConsumer<CommandManager<C, ?>, C> handler;

/**
* @param duration The cooldown duration
* @param uses The allowed number of uses in the provided interval
* @param handler A handler that is executed if an execution is blocked due to the user being on cooldown. You should send an error message here.
* @param auto Whether to automatically increment the cooldown
*/
public CooldownImpl(@NotNull Duration duration, int uses, @NotNull BiConsumer<CommandManager<C, ?>, C> handler) {
public CooldownImpl(@NotNull Duration duration, int uses, boolean auto, @NotNull BiConsumer<CommandManager<C, ?>, C> handler) {
cooldown = Caffeine.newBuilder().expireAfterWrite(duration).build();

this.auto = auto;
this.duration = duration;
this.uses = uses;
this.handler = handler;
Expand All @@ -40,13 +43,18 @@ public boolean isAllowed(@NotNull CommandManager<C, ?> manager, @NotNull C conte
var current = cooldown.getIfPresent(user);

if(current == null) cooldown.put(user, current = new AtomicInteger());
else current.incrementAndGet();
else if (auto) increment(user);

if(current.get() >= uses) handler.accept(manager, context);

return current.get() < uses;
}

public void increment(long user) {
var cooldown = this.cooldown.getIfPresent(user);
if (cooldown != null) cooldown.getAndIncrement();
}

@NotNull
@Override
public String format(@NotNull DiscordLocale locale) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.mineking.discordutils.commands.condition.cooldown;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface CooldownIncrement {
}