Skip to content

Commit

Permalink
Merge pull request #69 from pitzzahh/patch{#65}
Browse files Browse the repository at this point in the history
Fixed code scalability
  • Loading branch information
Peter John Arao authored Feb 26, 2023
2 parents ad3d441 + dfd7dd4 commit abcb1a4
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import java.util.ArrayList;
import lombok.Getter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;

@Getter
@Configuration
public class CommandsConfig {

@Value("${bot.commands.verification.verify-command}")
private String verifyCommand;

@Value("${bot.commands.confessions.confess-command}")
private String confessCommand;

Expand All @@ -49,14 +48,17 @@ public class CommandsConfig {
@Value("${bot.commands.prefix}")
private String prefix;

@Value("${bot.commands.rules}")
private String rulesCommand;

@Bean
public List<ChatCommand> getChatCommands() {
return new ArrayList<>();
}

@Bean
public List<SlashCommand> getSlashCommands() {
return new ArrayList<>();
public Map<String, SlashCommand> getSlashCommands() {
return new HashMap<>();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,41 @@ public record FormatChatCommand(
CommandsService commandsService
) implements ChatCommand {


/**
* Contains the process to be handled.
* @param context a {@code CommandContext}.
* @see CommandContext
*/
public void process(@NonNull CommandContext context) {
final var ARGS = context.getArgs();
final var CHANNEL = context.getEvent().getChannel();
final var contextArgs = context.getArgs();
final var channel = context.getEvent().getChannel();

if (ARGS.size() < 2) {
if (contextArgs.size() < 2) {
final var BUTTON = primary("ok", "okay");
messageUtilService.getEmbedBuilder().clear()
.clearFields()
.setColor(RED)
.setTitle("MISSING CONTENT");
messageUtilService.getMessageBuilder().clear()

messageUtilService.generateAutoDeleteMessage(
context.event(),
RED,
"Missing Content",
"Please provide the language and the content to format."
);

messageUtilService
.getMessageBuilder()
.clear()
.setEmbeds(messageUtilService.getEmbedBuilder().build())
.setActionRows(of(BUTTON));
CHANNEL.sendMessage(messageUtilService.getMessageBuilder().build()).queue();

channel.sendMessage(messageUtilService.getMessageBuilder().build()).queue();
return;
}
final var LANGUAGE = ARGS.get(0);
final var MESSAGE = context.getEvent().getMessage().getContentRaw();
final var INDEX = MESSAGE.indexOf(LANGUAGE) + LANGUAGE.length();
final var CONTENT = MESSAGE.substring(INDEX).trim();
context.getEvent().getMessage()
.reply("```" + LANGUAGE + "\n" + CONTENT + "```")
final var language = contextArgs.get(0);
final var contentRaw = context.getEvent().getMessage().getContentRaw();
final var index = contentRaw.indexOf(language) + language.length();
final var content = contentRaw.substring(index).trim();
context.getEvent()
.getMessage()
.reply("```" + language + "\n" + content + "```")
.queue(
message -> context.event()
.getMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import tech.araopj.springpitzzahhbot.commands.chat_command.ChatCommand;
import tech.araopj.springpitzzahhbot.commands.service.CommandsService;
import org.springframework.stereotype.Component;
import static java.lang.String.format;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.awt.*;

Expand All @@ -47,40 +50,53 @@ public record HelpChatCommand(
* @see CommandContext
*/
public void process(CommandContext context) {
final var ARGS = context.getArgs();
final var CHANNEL = context.getEvent().getChannel();
final var contextArgs = context.getArgs();
final var channel = context.getEvent().getChannel();

if (ARGS.isEmpty()) {
messageUtilService.getEmbedBuilder().clear()
.clearFields()
.setColor(Color.BLUE)
.setTitle("List of Commands")
.setFooter("Created by pitzzahh-bot#3464", context.getGuild().getIconUrl());
if (contextArgs.isEmpty()) {
messageUtilService.generateBotSentMessage(
context.getEvent(),
Color.BLUE,
"List of Commands",
null,
LocalDateTime.now(ZoneId.of("UTC")),
format("Created by %s", context.getGuild().getJDA().getSelfUser().getAsTag())
);
commandsService
.chatCommands()
.forEach(
c -> messageUtilService.getEmbedBuilder()
.addField(
commandsService.getPrefix().concat(c.name().get()),
c.description().get(),
true
)
chatCommand -> messageUtilService
.getEmbedBuilder()
.clear()
.clearFields()
.addField(commandsService.getPrefix().concat(chatCommand.name().get()), chatCommand.description().get(), true)
);
CHANNEL.sendMessageEmbeds(messageUtilService.getEmbedBuilder().build()).queue();
channel.sendMessageEmbeds(messageUtilService.getEmbedBuilder().build()).queue();
return;
}
final var SEARCH = ARGS.get(0);
final var COMMAND = chatCommandManager.getChatCommandByName(SEARCH);
if (COMMAND.isEmpty()) CHANNEL.sendMessageFormat("Nothing found on chat_command: %s", SEARCH).queue();
final var chatCommandByName = contextArgs.get(0);
final var COMMAND = chatCommandManager.getChatCommandByName(chatCommandByName);
if (COMMAND.isEmpty()) {
messageUtilService.generateBotSentMessage(
context.getEvent(),
Color.YELLOW.brighter(),
format("No chat_command found for %s", chatCommandByName),
null,
LocalDateTime.now(ZoneId.of("UTC")),
format("Created by %s", context.getGuild().getJDA().getSelfUser().getAsTag())
);
}
else {
messageUtilService.getEmbedBuilder().clear()
.clearFields()
.setColor(Color.CYAN.brighter())
.setTitle(COMMAND.get().name().get())
.setDescription(COMMAND.get().description().get())
.setFooter("Created by pitzzahh-bot#3464", context.getGuild().getIconUrl());
CHANNEL.sendMessageEmbeds(messageUtilService.getEmbedBuilder().build()).queue();
messageUtilService.generateBotSentMessage(
context.getEvent(),
Color.CYAN.brighter(),
COMMAND.get().name().get(),
COMMAND.get().description().get(),
LocalDateTime.now(ZoneId.of("UTC")),
format("Created by %s", context.getGuild().getJDA().getSelfUser().getAsTag())
);
}
channel.sendMessageEmbeds(messageUtilService.getEmbedBuilder().build()).queue();
}

/**
Expand Down Expand Up @@ -122,6 +138,6 @@ public Supplier<String> description() {
*/
@Override
public Supplier<List<String>> aliases() {
return () -> List.of("commands", "chat_command", "chat_command list", "com");
return () -> List.of("commands", "chat commands", "chat_command list", "com");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import tech.araopj.springpitzzahhbot.commands.CommandsConfig;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public record CommandsService(CommandsConfig commandsConfig) {

public String getVerifyCommand() {
return commandsConfig.getVerifyCommand();
public String getRulesCommand() {
return commandsConfig.getRulesCommand();
}

public String getConfessCommand() {
Expand All @@ -29,7 +30,7 @@ public List<ChatCommand> chatCommands() {
return commandsConfig.getChatCommands();
}

public List<SlashCommand> slashCommands() {
public Map<String, SlashCommand> slashCommands() {
return commandsConfig.getSlashCommands();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,75 +24,54 @@

package tech.araopj.springpitzzahhbot.commands.slash_command;

import tech.araopj.springpitzzahhbot.commands.slash_command.commands.confessions.service.ConfessionService;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.approveJoke.ApproveJoke;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.submitJoke.SubmitJoke;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.service.JokesService;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.game.service.GameService;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.confessions.Confession;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.GetJoke;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.game.Game;
import tech.araopj.springpitzzahhbot.exceptions.CommandAlreadyExistException;
import tech.araopj.springpitzzahhbot.config.channels.service.ChannelService;
import tech.araopj.springpitzzahhbot.utilities.service.MessageUtilService;
import tech.araopj.springpitzzahhbot.config.HttpConfig;
import tech.araopj.springpitzzahhbot.commands.service.CommandsService;
import org.springframework.stereotype.Component;
import org.springframework.lang.NonNull;
import java.util.function.Consumer;
import java.util.function.Supplier;
import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
import java.util.HashMap;
import java.util.Arrays;
import java.util.Map;

@Slf4j
@Component
public class SlashCommandManager {
public record SlashCommandManager(CommandsService commandsService) {

private final Map<String, SlashCommand> COMMANDS = new HashMap<>();

public SlashCommandManager(
MessageUtilService messageUtilService,
ConfessionService confessionService,
ChannelService channelService,
JokesService jokesService,
GameService gameService,
HttpConfig httpConfig
) {
addCommands(
new Confession(messageUtilService, confessionService, channelService),
new Game(messageUtilService, gameService),
new GetJoke(messageUtilService, jokesService, httpConfig),
new SubmitJoke(messageUtilService, jokesService, httpConfig),
new ApproveJoke(messageUtilService, jokesService, httpConfig)
);
}

Consumer<SlashCommand> addCommand = command -> {
var found = this.COMMANDS.values()
public void addCommand(@NonNull SlashCommand slashCommand) {
var found = commandsService
.slashCommands()
.values()
.stream()
.anyMatch(c -> c.name().get().equalsIgnoreCase(command.name().get()));
.anyMatch(c -> c.name().get().equalsIgnoreCase(slashCommand.name().get()));
if (found) throw new CommandAlreadyExistException("A Command With this name is already present!");
this.COMMANDS.put(command.name().get(), command);
};

private void addCommands(@NonNull SlashCommand... commands) {
Arrays.stream(commands).forEachOrdered(e -> this.addCommand.accept(e));
commandsService
.slashCommands().put(slashCommand.name().get(), slashCommand);
}

public void handle(@NonNull SlashCommandInteractionEvent event) {
var commandName = event.getName();
var COMMAND_CONTEXT = new CommandContext(event);
if (getCommands().containsKey(commandName)) getCommands().get(commandName).execute().accept(COMMAND_CONTEXT);
var COM = COMMANDS.values()
if (commandsService.slashCommands().containsKey(commandName)) {
commandsService
.slashCommands()
.get(commandName)
.execute()
.accept(COMMAND_CONTEXT);
}
var commandData = commandsService
.slashCommands()
.values()
.stream()
.map(SlashCommand::getCommandData)
.map(Supplier::get)
.toList();
if (!COM.isEmpty()) Objects.requireNonNull(event.getGuild()).updateCommands().addCommands(COM).queue();
if (!commandData.isEmpty()) {
Objects.requireNonNull(
event.getGuild()
).updateCommands()
.addCommands(commandData)
.queue();
}
}

public Map<String, SlashCommand> getCommands() {
return this.COMMANDS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.springframework.stereotype.Component;
import tech.araopj.springpitzzahhbot.commands.slash_command.CommandContext;
Expand All @@ -44,7 +43,6 @@
import static java.awt.Color.GREEN;
import static java.lang.String.format;
import static java.time.LocalDateTime.now;
import static java.time.ZoneId.of;
import static java.util.concurrent.TimeUnit.MINUTES;

/**
Expand Down Expand Up @@ -83,7 +81,7 @@ private void process(CommandContext context) {
.setColor(Color.RED)
.setDescription(SECRET_MESSAGE)
.setFooter("anonymous 👀")
.setTimestamp(now(of("UTC")));
.setTimestamp(now(ZoneId.of("UTC")));
CONFESSIONS.ifPresent(c -> c.sendMessageEmbeds(messageUtilService.getEmbedBuilder().build()).queue(message -> confirmationMessage(context)));
log.info("Sent confession message to {} channel", confessionService.sentSecretChannelName());
} else {
Expand Down Expand Up @@ -121,7 +119,7 @@ private void message(String title, String description) {
.setColor(GREEN)
.setTitle(title)
.setDescription(description)
.setTimestamp(now(ZoneId.systemDefault()).plusMinutes(messageUtilService.getReplyDeletionDelayInMinutes()))
.setTimestamp(now(ZoneId.of("UTC")).plusMinutes(messageUtilService.getReplyDeletionDelayInMinutes()))
.setFooter("This message will be automatically deleted on");
messageUtilService.getMessageBuilder()
.clear()
Expand Down Expand Up @@ -177,12 +175,11 @@ public Supplier<CommandData> getCommandData() {
return () -> new CommandDataImpl(
name().get(),
description().get()
).addOptions(
new OptionData(
OptionType.STRING,
name().get().concat("ion"),
"Enter your confession",
true)
).addOption(
OptionType.STRING,
name().get().concat("ion"),
"Enter your confession",
true
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

@Slf4j
@Service
public record Game(
public record Play(
MessageUtilService messageUtilService,
GameService gameService
) implements SlashCommand {
Expand Down
Loading

0 comments on commit abcb1a4

Please sign in to comment.