-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from stelitop/dev
Release 0.0.5
- Loading branch information
Showing
46 changed files
with
1,417 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
library/src/main/java/net/stelitop/mad4j/DiscordEventsComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
library/src/main/java/net/stelitop/mad4j/commands/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.stelitop.mad4j.commands; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* <p>Annotation for all commands to be used by the bot.</p> | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Command { | ||
/** | ||
* The name of the command. | ||
*/ | ||
String name(); | ||
/** | ||
* The description of the command. | ||
*/ | ||
String description(); | ||
/** | ||
* The types this command registers as. Can be multiple types. The existing types | ||
* are "text" for commands from messages used a prefix, and "slash" for slash commands. | ||
* If no types are given, then the default type is slash commands only, unless changed | ||
* in the properties file. | ||
*/ | ||
// TODO: Replace these with enum types | ||
CommandType[] types() default {}; | ||
} |
115 changes: 115 additions & 0 deletions
115
library/src/main/java/net/stelitop/mad4j/commands/CommandData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package net.stelitop.mad4j.commands; | ||
|
||
import discord4j.core.GatewayDiscordClient; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import net.stelitop.mad4j.DiscordEventsComponent; | ||
import net.stelitop.mad4j.listeners.CommandOptionAutocompleteListener; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
@Order(0) | ||
public class CommandData implements ApplicationRunner { | ||
|
||
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); | ||
|
||
private final GatewayDiscordClient gatewayDiscordClient; | ||
private final CommandOptionAutocompleteListener commandOptionAutocompleteListener; | ||
private final ApplicationContext applicationContext; | ||
private final Environment environment; | ||
private final List<Entry> commandsInfo = new ArrayList<>(); | ||
|
||
private static final Set<CommandType> DEFAULT_COMMAND_TYPES = Set.of(CommandType.Slash); | ||
|
||
@Builder | ||
@Getter | ||
public static class Entry { | ||
private String name; | ||
private String description; | ||
private Set<CommandType> types; | ||
private Object bean; | ||
private Method method; | ||
} | ||
|
||
@Autowired | ||
public CommandData( | ||
GatewayDiscordClient gatewayDiscordClient, | ||
CommandOptionAutocompleteListener commandOptionAutocompleteListener, | ||
ApplicationContext applicationContext, | ||
Environment environment | ||
) { | ||
this.gatewayDiscordClient = gatewayDiscordClient; | ||
this.commandOptionAutocompleteListener = commandOptionAutocompleteListener; | ||
this.applicationContext = applicationContext; | ||
this.environment = environment; | ||
} | ||
|
||
@Override | ||
public void run(ApplicationArguments args) { | ||
Collection<Object> commandBeans = applicationContext.getBeansWithAnnotation(DiscordEventsComponent.class).values(); | ||
|
||
commandsInfo.clear(); | ||
for (var bean : commandBeans) { | ||
for (var method : bean.getClass().getMethods()) { | ||
Entry data = getCommandData(bean, method); | ||
if (data == null) continue; | ||
commandsInfo.add(data); | ||
} | ||
} | ||
// TODO: Verify no overlap | ||
} | ||
|
||
private Entry getCommandData(Object bean, Method method) { | ||
if (method.isAnnotationPresent(Command.class)) { | ||
Command c = method.getAnnotation(Command.class); | ||
return Entry.builder() | ||
.name(c.name().toLowerCase()) | ||
.description(c.description()) | ||
.types(getCommandTypes(c.types())) | ||
.bean(bean) | ||
.method(method) | ||
.build(); | ||
} else if (method.isAnnotationPresent(SlashCommand.class)) { | ||
SlashCommand sc = method.getAnnotation(SlashCommand.class); | ||
return Entry.builder() | ||
.name(sc.name().toLowerCase()) | ||
.description(sc.description()) | ||
.types(Set.of(CommandType.Slash)) | ||
.bean(bean) | ||
.method(method) | ||
.build(); | ||
} | ||
return null; | ||
} | ||
|
||
private Set<CommandType> getCommandTypes(CommandType[] types) { | ||
if (types.length == 0) return DEFAULT_COMMAND_TYPES; | ||
else return Arrays.stream(types).collect(Collectors.toSet()); | ||
} | ||
|
||
public @Nullable Entry get(String commandName, CommandType type) { | ||
String nameLower = commandName.toLowerCase(); | ||
return commandsInfo.stream() | ||
.filter(x -> x.name.toLowerCase().equals(nameLower) && x.types.contains(type)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
public List<Entry> getFromType(CommandType type) { | ||
return commandsInfo.stream() | ||
.filter(x -> x.types.contains(type)) | ||
.toList(); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
library/src/main/java/net/stelitop/mad4j/commands/CommandParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
library/src/main/java/net/stelitop/mad4j/commands/CommandType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package net.stelitop.mad4j.commands; | ||
|
||
public enum CommandType { | ||
Text, | ||
Slash | ||
} |
2 changes: 1 addition & 1 deletion
2
.../net/stelitop/mad4j/InteractionEvent.java → ...itop/mad4j/commands/InteractionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.