Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Add preconditions #14

Closed
KagChi opened this issue Mar 11, 2024 · 1 comment
Closed

Add preconditions #14

KagChi opened this issue Mar 11, 2024 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@KagChi
Copy link

KagChi commented Mar 11, 2024

Before the command executed, preconditon will run first

@Blad3Mak3r
Copy link
Owner

Blad3Mak3r commented Mar 30, 2024

You can use interceptors when you initialize the client.

Client code

SlashCommandClient.builder("bot.huge.packages.commands")
    .addSlashInterceptor(object : SlashCommandInterceptor {
        override suspend fun intercept(ctx: SlashCommandContext): Boolean {
            if (!ctx.function.hasAnnotation<RequireBetaAccess>())
                return true

            if (!ctx.isFromGuild || !ctx.guildData.hasBetaAccess()) {
                ctx.located(EmojiUtils.NoEntry, "interceptors.requireBetaAccess").setEphemeral(true).queue()
                return false
            }

            return true
        }
    }
    .addSlashInterceptor { SlashInterceptors.requireVoiceConnected(it) }
    .enableMetrics()
    .setRateLimitClient(redis.slashRateLimitClient)
    .withTimeout(5.minutes)
    .buildWith(shardManager)

Command code

@RequireBetaAccess
@OnSlashCommand(name = "commandname", target = InteractionTarget.GUILD)
suspend fun command(ctx: GuildSlashCommandContext) {
    // Your command code
}

Annotation code

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class RequireBetaAccess()

The code shown is a small part of HUGE.

@Blad3Mak3r Blad3Mak3r pinned this issue Mar 30, 2024
@Blad3Mak3r Blad3Mak3r self-assigned this Mar 30, 2024
@Blad3Mak3r Blad3Mak3r added documentation Improvements or additions to documentation enhancement New feature or request labels Mar 30, 2024
Repository owner locked and limited conversation to collaborators Mar 31, 2024
@Blad3Mak3r Blad3Mak3r converted this issue into discussion #15 Mar 31, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants