Skip to content

Commit

Permalink
relese 4.0.0-alpha.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaktushose committed Oct 31, 2023
2 parents a50c183 + 82503bd commit 875421b
Show file tree
Hide file tree
Showing 107 changed files with 1,940 additions and 1,842 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[![JDA-Version](https://img.shields.io/badge/JDA%20Version-5.0.0--beta.15-important)](https://github.com/DV8FromTheWorld/JDA#download)
[![Generic badge](https://img.shields.io/badge/Download-4.0.0--alpha.4-green.svg)](https://github.com/Kaktushose/jda-commands/releases/latest)
[![JDA-Version](https://img.shields.io/badge/JDA%20Version-5.0.0--beta.17-important)](https://github.com/DV8FromTheWorld/JDA#download)
[![Generic badge](https://img.shields.io/badge/Download-4.0.0--alpha.5-green.svg)](https://github.com/Kaktushose/jda-commands/releases/latest)
[![Java CI](https://github.com/Kaktushose/jda-commands/actions/workflows/ci.yml/badge.svg?branch=dev)](https://github.com/Kaktushose/jda-commands/actions/workflows/ci.yml)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/f2b4367f6d0f42d89b7e51331f3ce299)](https://app.codacy.com/gh/Kaktushose/jda-commands/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/f2b4367f6d0f42d89b7e51331f3ce299)](https://app.codacy.com/gh/Kaktushose/jda-commands/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
[![license-shield](https://img.shields.io/badge/License-Apache%202.0-lightgrey.svg)]()
[![migration-shield](https://img.shields.io/badge/Wiki-Migrating%20to%20V4-green.svg)](https://github.com/Kaktushose/jda-commands/wiki/Migration)

# JDA-Commands

Expand All @@ -16,7 +15,7 @@ any boilerplate code, so you can focus solely on the business logic of your bot

| jda-commands | JDA | Text Commands | Interactions | Stable |
|-----------------------------------------------------------------------------|-|--|---|---|
| [4.0.0-alpha.4](https://github.com/Kaktushose/jda-commands/releases/latest) |5||||
| [4.0.0-alpha.5](https://github.com/Kaktushose/jda-commands/releases/latest) |5||||
| [3.0.0](https://github.com/Kaktushose/jda-commands/releases/tag/v3.0.0) |5||||
| [2.2.0](https://github.com/Kaktushose/jda-commands/releases/tag/v.2.0.0) |4||||

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.kaktushose</groupId>
<artifactId>jda-commands</artifactId>
<version>4.0.0-alpha.4</version>
<version>4.0.0-alpha.5</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -66,7 +66,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-beta.15</version>
<version>5.0.0-beta.17</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/github/kaktushose/jda/commands/JDACommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.kaktushose.jda.commands.dependency.DependencyInjector;
import com.github.kaktushose.jda.commands.dispatching.DispatcherSupervisor;
import com.github.kaktushose.jda.commands.dispatching.ParserSupervisor;
import com.github.kaktushose.jda.commands.dispatching.RuntimeSupervisor;
import com.github.kaktushose.jda.commands.dispatching.adapter.TypeAdapterRegistry;
import com.github.kaktushose.jda.commands.dispatching.filter.FilterRegistry;
import com.github.kaktushose.jda.commands.dispatching.validation.ValidatorRegistry;
Expand Down Expand Up @@ -30,19 +30,19 @@ public class JDACommands {
private final JDAContext jdaContext;
private final ImplementationRegistry implementationRegistry;
private final DispatcherSupervisor dispatcherSupervisor;
private final ParserSupervisor parserSupervisor;
private final FilterRegistry filterRegistry;
private final TypeAdapterRegistry adapterRegistry;
private final ValidatorRegistry validatorRegistry;
private final DependencyInjector dependencyInjector;
private final InteractionRegistry interactionRegistry;
private final SlashCommandUpdater updater;
private final RuntimeSupervisor runtimeSupervisor;

// this is needed for unit testing
protected JDACommands() {
jdaContext = null;
implementationRegistry = null;
parserSupervisor = null;
runtimeSupervisor = null;
filterRegistry = null;
adapterRegistry = null;
validatorRegistry = null;
Expand Down Expand Up @@ -74,16 +74,16 @@ private JDACommands(Object jda, Class<?> clazz, LocalizationFunction function, S
);
interactionRegistry = new InteractionRegistry(validatorRegistry, dependencyInjector, function);

runtimeSupervisor = new RuntimeSupervisor(dependencyInjector);
dispatcherSupervisor = new DispatcherSupervisor(this);
parserSupervisor = new ParserSupervisor(dispatcherSupervisor);

implementationRegistry.index(clazz, packages);

interactionRegistry.index(clazz, packages);

updater = new SlashCommandUpdater(this, interactionRegistry.getCommands());
updater = new SlashCommandUpdater(this);
updater.updateAllCommands();
jdaContext.performTask(it -> it.addEventListener(parserSupervisor));
jdaContext.performTask(it -> it.addEventListener(dispatcherSupervisor));

isActive = true;
log.info("Finished loading!");
Expand Down Expand Up @@ -153,7 +153,7 @@ public static boolean isActive() {
* This will <b>not</b> unregister any slash commands.
*/
public void shutdown() {
jdaContext.performTask(jda -> jda.removeEventListener(parserSupervisor));
jdaContext.performTask(jda -> jda.removeEventListener(dispatcherSupervisor));
isActive = false;
}

Expand All @@ -178,13 +178,14 @@ public ImplementationRegistry getImplementationRegistry() {
return implementationRegistry;
}


/**
* Gets the {@link ParserSupervisor}.
* Gets the {@link RuntimeSupervisor}
*
* @return the {@link ParserSupervisor}
* @return the {@link RuntimeSupervisor}
*/
public ParserSupervisor getParserSupervisor() {
return parserSupervisor;
public RuntimeSupervisor getRuntimeSupervisor() {
return runtimeSupervisor;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.github.kaktushose.jda.commands.annotations.interactions.SlashCommand;
import com.github.kaktushose.jda.commands.data.CommandTree;
import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
import com.github.kaktushose.jda.commands.reflect.InteractionRegistry;
import com.github.kaktushose.jda.commands.reflect.interactions.commands.GenericCommandDefinition;
import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import com.github.kaktushose.jda.commands.scope.GuildScopeProvider;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import org.slf4j.Logger;
Expand All @@ -15,7 +18,7 @@

/**
* Class that sends the {@link SlashCommandData} to Discord. Uses a {@link CommandTree} to properly transpile all
* {@link CommandDefinition CommandDefinitions} to {@link SlashCommandData}.
* {@link SlashCommandDefinition CommandDefinitions} to {@link SlashCommandData}.
*
* @author Kaktushose
* @version 4.0.0
Expand All @@ -26,18 +29,18 @@ public class SlashCommandUpdater {

private static final Logger log = LoggerFactory.getLogger(SlashCommandUpdater.class);
private final JDAContext jdaContext;
private final Collection<CommandDefinition> commands;
private final GuildScopeProvider guildScopeProvider;
private final InteractionRegistry interactionRegistry;

/**
* Constructs a new SlashCommandUpdater.
*
* @param jdaCommands the corresponding {@link JDACommands} instance
*/
public SlashCommandUpdater(JDACommands jdaCommands, Collection<CommandDefinition> commands) {
public SlashCommandUpdater(JDACommands jdaCommands) {
this.jdaContext = jdaCommands.getJdaContext();
this.commands = commands;
guildScopeProvider = jdaCommands.getImplementationRegistry().getGuildScopeProvider();
interactionRegistry = jdaCommands.getInteractionRegistry();
}

/**
Expand All @@ -53,19 +56,34 @@ public void updateAllCommands() {
* Sends the guild scope {@link SlashCommandData} to Discord.
*/
public void updateGuildCommands() {
log.debug("Updating guild slash commands...");
Set<CommandDefinition> globalCommands = commands.stream()
log.debug("Updating guild commands...");

Set<GenericCommandDefinition> guildCommands = interactionRegistry.getCommands()
.stream()
.filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GUILD)
.collect(Collectors.toSet());
CommandTree tree = new CommandTree(globalCommands);
log.debug("Generated command tree:\n" + tree);
Collection<String> labels = tree.getNames();
log.debug("Using commands: " + labels);
Map<Long, Set<CommandData>> guildMapping = new HashMap<>();

for (SlashCommandData command : tree.getCommands()) {
CommandTree tree = new CommandTree(
guildCommands.stream()
.filter(it -> it.getCommandType() == Command.Type.SLASH)
.map(it -> (SlashCommandDefinition) it)
.collect(Collectors.toSet())
);
log.debug("Generated slash command tree:\n" + tree);

Set<CommandData> result = new HashSet<>();
result.addAll(tree.getCommands());
result.addAll(guildCommands.stream().
filter(it -> (it.getCommandType() == Command.Type.USER || it.getCommandType() == Command.Type.MESSAGE))
.map(GenericCommandDefinition::toCommandData)
.collect(Collectors.toSet())
);
log.debug("Registering commands: " + result.stream().map(CommandData::getName).collect(Collectors.toSet()));

Map<Long, Set<CommandData>> guildMapping = new HashMap<>();
for (CommandData command : result) {
// create a copy so that a user doesn't modify the command data used for registration
Set<Long> guildIds = guildScopeProvider.getGuildsForCommand(SlashCommandData.fromData(command.toData()));
Set<Long> guildIds = guildScopeProvider.getGuildsForCommand(CommandData.fromData(command.toData()));
if (guildIds.isEmpty()) {
log.debug("No guilds provided for command {}", command.getName());
} else {
Expand All @@ -88,17 +106,31 @@ public void updateGuildCommands() {
* Sends the global scope {@link SlashCommandData} to Discord.
*/
public void updateGlobalCommands() {
log.debug("Updating global slash commands...");
Set<CommandDefinition> globalCommands = commands.stream()
log.debug("Updating global commands...");

Set<GenericCommandDefinition> globalCommands = interactionRegistry.getCommands()
.stream()
.filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GLOBAL)
.collect(Collectors.toSet());
CommandTree tree = new CommandTree(globalCommands);
log.debug("Generated command tree:\n" + tree);
Collection<String> labels = tree.getNames();
log.debug("Using commands: " + labels);
jdaContext.performTask(jda -> jda.updateCommands().addCommands(tree.getCommands()).queue());
log.debug("Done!");
}

CommandTree tree = new CommandTree(
globalCommands.stream()
.filter(it -> it.getCommandType() == Command.Type.SLASH)
.map(it -> (SlashCommandDefinition) it)
.collect(Collectors.toSet())
);
log.debug("Generated slash command tree:\n" + tree);

Set<CommandData> result = new HashSet<>();
result.addAll(tree.getCommands());
result.addAll(globalCommands.stream().
filter(it -> (it.getCommandType() == Command.Type.USER || it.getCommandType() == Command.Type.MESSAGE))
.map(GenericCommandDefinition::toCommandData)
.collect(Collectors.toSet())
);
log.debug("Registering commands: " + result.stream().map(CommandData::getName).collect(Collectors.toSet()));

jdaContext.performTask(jda -> jda.updateCommands().addCommands(result).queue());
log.debug("Done!");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.kaktushose.jda.commands.annotations.interactions;

import com.github.kaktushose.jda.commands.dispatching.interactions.buttons.ButtonEvent;
import com.github.kaktushose.jda.commands.dispatching.interactions.components.ComponentEvent;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;

Expand All @@ -16,7 +16,7 @@
* {@link Interaction}.
* Furthermore, the method signature has to meet the following conditions:
* <ul>
* <li>First parameter must be of type {@link ButtonEvent ButtonEvent}</li>
* <li>First parameter must be of type {@link ComponentEvent ButtonEvent}</li>
* </ul>
*
* @author Kaktushose
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.github.kaktushose.jda.commands.annotations.interactions;

import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.Command;

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

/**
* Methods annotated with ContextMenu will be registered as a context menu command.
*
* <p>Therefore the method must be declared inside a class that is annotated with
* {@link com.github.kaktushose.jda.commands.annotations.interactions.Interaction}.
* Furthermore, the method signature has to meet the following conditions:
* <ul>
* <li>First parameter must be of type {@link CommandEvent}</li>
* </ul>
*
* @author Kaktushose
* @version 4.0.0
* @see com.github.kaktushose.jda.commands.annotations.interactions.Interaction Interaction
* @since 4.0.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContextCommand {

/**
* Returns the name of the command.
*
* @return the name of the command
*/
String value() default "";

/**
* Returns whether this command is only usable in a guild.
* This only has an effect if this command is registered globally.
*
* @return {@code true} if this command is only usable in a guild
*/
boolean isGuildOnly() default false;

/**
* Returns whether this command is active and thus can be executed or not.
*
* @return {@code true} if this command is active
*/
boolean isActive() default true;

/**
* Returns whether this command can only be executed in NSFW channels.
*
* @return {@code true} if this command can only be executed in NSFW channels
*/
boolean isNSFW() default false;

/**
* Returns whether this command should send ephemeral replies by default. Note that {@link Interaction#ephemeral()}
* set to {@code true} will override this value.
*
* @return {@code true} if this command should send ephemeral replies
*/
boolean ephemeral() default false;

/**
* Returns an array of {@link net.dv8tion.jda.api.Permission Permissions} this command
* should be enabled for by default. Note that guild admins can modify this at any time.
*
* @return a set of permissions this command will be enabled for by default
* @see Permissions Permission
* @see net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions DefaultMemberPermissions.ENABLED
* @see net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions DefaultMemberPermissions.DISABLED
*/
Permission[] enabledFor() default Permission.UNKNOWN;

/**
* Returns whether this command should be registered as a global or as a guild command.
*
* @return whether this command should be registered as a global or as a guild command
* @see SlashCommand.CommandScope
*/
SlashCommand.CommandScope scope() default SlashCommand.CommandScope.GLOBAL;

/**
* Gets the type of this command.
*
* @return the type of the command
*/
Command.Type type();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.kaktushose.jda.commands.annotations.interactions;

import com.github.kaktushose.jda.commands.dispatching.interactions.menus.SelectMenuEvent;
import com.github.kaktushose.jda.commands.dispatching.interactions.components.ComponentEvent;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu.SelectTarget;

Expand All @@ -17,7 +17,7 @@
* Furthermore, the method signature has to meet the following conditions:
* <ul>
* <li>First parameter must be of type
* {@link SelectMenuEvent SelectMenuEvent}</li>
* {@link ComponentEvent SelectMenuEvent}</li>
* </ul>
*
* @author Kaktushose
Expand Down
Loading

0 comments on commit 875421b

Please sign in to comment.