Skip to content

Commit

Permalink
Merge pull request #21 from stelitop/dev
Browse files Browse the repository at this point in the history
Release 0.0.5
  • Loading branch information
stelitop authored Aug 25, 2024
2 parents 975de08 + ff09d80 commit 26ed29d
Show file tree
Hide file tree
Showing 46 changed files with 1,417 additions and 203 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = 'net.stelitop'
version = '0.0.4'
version = '0.0.5'

java {
sourceCompatibility = '17'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.stelitop.mad4j;

import net.stelitop.mad4j.commands.SlashCommand;
import net.stelitop.mad4j.components.ComponentInteraction;
import net.stelitop.mad4j.commands.components.ComponentInteraction;
import org.springframework.stereotype.Component;

import java.lang.annotation.ElementType;
Expand Down
12 changes: 0 additions & 12 deletions library/src/main/java/net/stelitop/mad4j/Mad4jConfig.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package net.stelitop.mad4j;

import discord4j.core.GatewayDiscordClient;
import net.stelitop.mad4j.autocomplete.AutocompletionExecutor;
import net.stelitop.mad4j.listeners.CommandOptionAutocompleteListener;
import net.stelitop.mad4j.listeners.ComponentEventListener;
import net.stelitop.mad4j.listeners.SlashCommandListener;
import net.stelitop.mad4j.requirements.CommandRequirementExecutor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.List;

/**
* <p>Configuration for mad4j.</p>
Expand Down
30 changes: 30 additions & 0 deletions library/src/main/java/net/stelitop/mad4j/commands/Command.java
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 library/src/main/java/net/stelitop/mad4j/commands/CommandData.java
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.stelitop.mad4j.commands;

import net.stelitop.mad4j.autocomplete.AutocompletionExecutor;
import net.stelitop.mad4j.autocomplete.NullAutocompleteExecutor;
import net.stelitop.mad4j.commands.autocomplete.AutocompletionExecutor;
import net.stelitop.mad4j.commands.autocomplete.NullAutocompleteExecutor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.stelitop.mad4j.commands;

public enum CommandType {
Text,
Slash
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.stelitop.mad4j;
package net.stelitop.mad4j.commands;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Loading

0 comments on commit 26ed29d

Please sign in to comment.