From 40f93329725d3ec9645e1ff74c593c67cf5d7217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= <4096670+Citymonstret@users.noreply.github.com> Date: Sun, 14 Jan 2024 21:41:52 +0100 Subject: [PATCH] feat(jda): configure parameter injections (#2) --- .../discord/jda5/JDA5CommandManager.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/cloud-jda5/src/main/java/org/incendo/cloud/discord/jda5/JDA5CommandManager.java b/cloud-jda5/src/main/java/org/incendo/cloud/discord/jda5/JDA5CommandManager.java index aa78d7e..d685c19 100644 --- a/cloud-jda5/src/main/java/org/incendo/cloud/discord/jda5/JDA5CommandManager.java +++ b/cloud-jda5/src/main/java/org/incendo/cloud/discord/jda5/JDA5CommandManager.java @@ -39,7 +39,10 @@ import java.util.function.BiPredicate; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.Channel; +import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent; import net.dv8tion.jda.api.hooks.EventListener; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; @@ -97,6 +100,46 @@ public JDA5CommandManager( .registerParser(JDAParser.channelParser()) .registerParser(JDAParser.mentionableParser()) .registerParser(JDAParser.attachmentParser()); + + // Common parameter injections. + this.parameterInjectorRegistry().registerInjector( + JDAInteraction.class, + (ctx, annotations) -> ctx.get(CONTEXT_JDA_INTERACTION) + ); + this.parameterInjectorRegistry().registerInjector( + User.class, + (ctx, annotations) -> ctx.get(CONTEXT_JDA_INTERACTION).user() + ); + this.parameterInjectorRegistry().registerInjector( + Member.class, + (ctx, annotations) -> { + final JDAInteraction jdaInteraction = ctx.get(CONTEXT_JDA_INTERACTION); + final GenericCommandInteractionEvent event = jdaInteraction.interactionEvent(); + if (event == null) { + return null; + } + return event.getMember(); + } + ); + this.parameterInjectorRegistry().registerInjector( + Guild.class, + (ctx, annotations) -> ctx.get(CONTEXT_JDA_INTERACTION).guild() + ); + this.parameterInjectorRegistry().registerInjector( + Channel.class, + (ctx, annotations) -> { + final JDAInteraction jdaInteraction = ctx.get(CONTEXT_JDA_INTERACTION); + final GenericCommandInteractionEvent event = jdaInteraction.interactionEvent(); + if (event == null) { + return null; + } + return event.getChannel(); + } + ); + this.parameterInjectorRegistry().registerInjector( + JDA.class, + (ctx, annotations) -> ctx.get(CONTEXT_JDA_INTERACTION).user().getJDA() + ); } @Override