Skip to content

Decorators

pumbas600 edited this page Dec 27, 2021 · 18 revisions

Decorators allow for you to call another method, before invoking the action using the decorator pattern. In doing so, you can restrict, or modify how the action is called. It's also possible to add multiple decorators to one action, allowing you to extensively enhance the functionality of an action with very little work.

Halpbot currently has 3 built-in decorators that can be used on any public method annotated with: @ButtonAction, @Command, @CustomParameter:

  1. @Cooldown
  2. @Permission
  3. @Log

@Cooldown

This decorator allows you to specify a period of time that a user must wait before being able to invoke an action again. If the user is still cooling down it will temporarily display a MessageEmbed telling you how many seconds you have to wait before you can use the action again.

Example usages:

@Cooldown(duration = @Duration(30))
@Command(description = "Tests the @Cooldown decorator")
public String cooldown() {
    return "This command has a 30 second cooldown!";
}
@Cooldown(duration = @Duration(value = 1, unit = ChronoUnit.MINUTES))
@Command(description = "Tests the @Cooldown decorator")
public String cooldown() {
    return "This command has a 1 minute cooldown!";
}

@Permission

The @Permission decorator allows you to specify which permissions a user must have if they want to invoke an action. If the user does not have all the specified permissions, then the action won't be invoked and a MessageEmbed will be temporarily displayed informing you that you lack the permissions to use the action.

Permissions allow for role-based restrictions on who can invoke actions. For more information on how they work, including how to create your own permissions refer to the documentation here.

@Permission(HalpbotPermissions.GUILD_OWNER)
@Command(description = "Tests the @Permission decorator")
public String permission() {
    return "This command is restricted to people with the *halpbot.owner.guild* permission";
}
@Permission({"halpbot.example.a", "halpbot.example.b"})
@Command(description = "Tests the @Permission decorator")
public String permission() {
    return "This command is restricted to people with the *halpbot.example.a* and *halpbot.example.b* permissions";
}

@Log

Clone this wiki locally