Skip to content

Commit

Permalink
Turn all components into autoconfigurations.
Browse files Browse the repository at this point in the history
  • Loading branch information
stelitop committed Aug 29, 2023
1 parent ff9b47b commit d8fb30c
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 18 deletions.
28 changes: 28 additions & 0 deletions src/main/java/stelitop/mad4j/Mad4jAutoConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package stelitop.mad4j;

import discord4j.core.GatewayDiscordClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import stelitop.mad4j.listeners.CommandOptionAutocompleteListener;

@Configuration
public class Mad4jAutoConfiguration {

@Bean
public SlashCommandRegistrar slashCommandRegistrar(
GatewayDiscordClient gatewayDiscordClient,
CommandOptionAutocompleteListener commandOptionAutocompleteListener,
ApplicationContext applicationContext,
Environment environment
) {
return new SlashCommandRegistrar(
gatewayDiscordClient,
commandOptionAutocompleteListener,
applicationContext,
environment
);
}
}
26 changes: 17 additions & 9 deletions src/main/java/stelitop/mad4j/SlashCommandRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import discord4j.discordjson.json.ApplicationCommandRequest;
import discord4j.discordjson.json.ImmutableApplicationCommandOptionData;
import discord4j.rest.RestClient;
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
import lombok.AllArgsConstructor;
import lombok.ToString;
import stelitop.mad4j.autocomplete.NullAutocompleteExecutor;
Expand Down Expand Up @@ -41,19 +42,26 @@
* to false in the application.properties file. This can be used because sometimes discord will
* "outdate" the commands and require you to wait.</p>
*/
@Component
public class SlashCommandRegistrar implements ApplicationRunner {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@Autowired
private GatewayDiscordClient gatewayDiscordClient;
@Autowired
private CommandOptionAutocompleteListener commandOptionAutocompleteListener;
@Autowired
private ApplicationContext applicationContext;
@Autowired
private Environment environment;
private final GatewayDiscordClient gatewayDiscordClient;
private final CommandOptionAutocompleteListener commandOptionAutocompleteListener;
private final ApplicationContext applicationContext;
private final Environment environment;

public SlashCommandRegistrar(
GatewayDiscordClient gatewayDiscordClient,
CommandOptionAutocompleteListener commandOptionAutocompleteListener,
ApplicationContext applicationContext,
Environment environment
) {
this.gatewayDiscordClient = gatewayDiscordClient;
this.commandOptionAutocompleteListener = commandOptionAutocompleteListener;
this.applicationContext = applicationContext;
this.environment = environment;
}

/**
* <p>Registers all properly annotated slash commands into the discord bot.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class CommandOptionAutocompleteListener implements ApplicationRunner {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
Expand All @@ -31,7 +30,6 @@ public class CommandOptionAutocompleteListener implements ApplicationRunner {
private final Map<Class<? extends AutocompletionExecutor>, AutocompletionExecutor> autocompletionExecutorBeans;
private final Map<Pair<String, String>, Class<? extends AutocompletionExecutor>> commandNameParamToExecutor;

@Autowired
public CommandOptionAutocompleteListener(
GatewayDiscordClient client,
List<AutocompletionExecutor> autocompletionExecutors
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/stelitop/mad4j/listeners/ComponentEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@
import java.util.*;
import java.util.stream.Collectors;

@Component
public class ComponentEventListener implements ApplicationRunner {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@Autowired
private GatewayDiscordClient client;
@Autowired
private ApplicationContext applicationContext;
private final GatewayDiscordClient client;
private final ApplicationContext applicationContext;

public ComponentEventListener(
GatewayDiscordClient client,
ApplicationContext applicationContext
) {
this.client = client;
this.applicationContext = applicationContext;
}

/**
* Loaded data for the classes that can contain the implementation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package stelitop.mad4j.listeners;

import discord4j.core.GatewayDiscordClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import stelitop.mad4j.autocomplete.AutocompletionExecutor;
import stelitop.mad4j.requirements.CommandRequirementExecutor;

import java.util.List;

@Configuration
public class Mad4jListenersAutoConfiguration {

@Bean
public SlashCommandListener slashCommandListener(
ApplicationContext applicationContext,
GatewayDiscordClient client,
List<CommandRequirementExecutor> possibleRequirements
) {
return new SlashCommandListener(
applicationContext,
client,
possibleRequirements
);
}

@Bean
public CommandOptionAutocompleteListener commandOptionAutocompleteListener(
GatewayDiscordClient client,
List<AutocompletionExecutor> autocompletionExecutors
) {
return new CommandOptionAutocompleteListener(client, autocompletionExecutors);
}

@Bean
public ComponentEventListener componentEventListener(
GatewayDiscordClient client,
ApplicationContext applicationContext
) {
return new ComponentEventListener(client, applicationContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
* These requirements are first checked against and if any of them are unfulfilled, the execution
* is cancelled and an error message is returned instead.</p>
*/
@Component
public class SlashCommandListener implements ApplicationRunner {

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
Expand All @@ -56,7 +55,6 @@ public class SlashCommandListener implements ApplicationRunner {
private final Map<Class<? extends CommandRequirementExecutor>, CommandRequirementExecutor> possibleRequirements;
private List<SlashCommandEntry> slashCommands;

@Autowired
public SlashCommandListener(
ApplicationContext applicationContext,
GatewayDiscordClient client,
Expand Down

0 comments on commit d8fb30c

Please sign in to comment.