AltitudeBot/src/main/java/com/alttd/commandManager/CommandManager.java

152 lines
5.7 KiB
Java
Raw Normal View History

2022-03-09 21:11:11 +00:00
package com.alttd.commandManager;
2022-04-15 20:00:33 +00:00
import com.alttd.commandManager.commands.AddCommand.CommandManage;
import com.alttd.commandManager.commands.CommandHelp;
import com.alttd.commandManager.commands.CommandSuggestion;
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
2022-03-31 20:08:50 +00:00
import com.alttd.database.Database;
import com.alttd.modalManager.ModalManager;
2022-03-31 20:08:50 +00:00
import com.alttd.util.Logger;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
2022-03-31 20:08:50 +00:00
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
2022-03-09 21:11:11 +00:00
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
2022-03-31 20:08:50 +00:00
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
2022-03-09 21:11:11 +00:00
import java.util.List;
import java.util.stream.Collectors;
2022-03-09 21:11:11 +00:00
public class CommandManager extends ListenerAdapter {
private final List<DiscordCommand> commands;
private final HashMap<String, List<ScopeInfo>> commandList = new HashMap<>();
2022-03-09 21:11:11 +00:00
public CommandManager(JDA jda, ModalManager modalManager) {
commandList.put("manage", new ArrayList<>(List.of(new ScopeInfo(CommandScope.GLOBAL, 0))));
loadCommands();
Logger.info("Loading commands...");
commands = List.of(
new CommandManage(jda, this),
new CommandHelp(jda, this),
new CommandPoll(jda, this),
new CommandSuggestion(jda, modalManager, this));
2022-03-09 21:11:11 +00:00
}
@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
String commandName = event.getName();
Optional<DiscordCommand> first = commands.stream()
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(commandName))
.findFirst();
if (first.isEmpty()) {
event.replyEmbeds(new EmbedBuilder()
.setTitle("Invalid command")
.setDescription("We couldn't find a command called [" + commandName + "], please report this issue to a Teri")
.setColor(Color.RED)
.build())
.setEphemeral(true)
.queue();
return;
}
first.get().execute(event);
}
2022-03-31 20:08:50 +00:00
@Override
public void onCommandAutoCompleteInteraction(@NotNull CommandAutoCompleteInteractionEvent event) {
Optional<DiscordCommand> first = commands.stream()
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(event.getName()))
.findFirst();
if (first.isEmpty())
return;
first.get().suggest(event);
2022-03-09 21:11:11 +00:00
}
public DiscordCommand getCommand(String name) {
for (DiscordCommand command : commands) {
if (command.getName().equalsIgnoreCase(name))
return command;
}
return null;
}
2022-03-09 21:37:44 +00:00
public List<DiscordCommand> getCommands() {
return commands;
}
public List<DiscordCommand> getCommands(Guild guild) {
return commands.stream().filter(command -> {
List<ScopeInfo> scopeInfoList = commandList.get(command.getName());
for (ScopeInfo scopeInfo : scopeInfoList) {
switch (scopeInfo.getScope()) {
case GLOBAL -> {
return true;
}
case GUILD -> {
if (guild.getIdLong() == scopeInfo.getId())
return true;
}
}
}
return false;
}).collect(Collectors.toList());
}
public boolean enableCommand(String commandName, ScopeInfo scopeInfo) {
List<ScopeInfo> scopeInfoList = commandList.getOrDefault(commandName, new ArrayList<>());
scopeInfoList.add(scopeInfo);
commandList.put(commandName, scopeInfoList);
return true;
}
public boolean disableCommand(String commandName, ScopeInfo scopeInfo) {
List<ScopeInfo> scopeInfoList = commandList.get(commandName);
if (scopeInfoList == null)
return false;
if (!scopeInfoList.contains(scopeInfo))
return false;
scopeInfoList.remove(scopeInfo);
if (scopeInfoList.isEmpty())
commandList.remove(commandName);
return true;
}
synchronized private void loadCommands() {
String sql = "SELECT * FROM commands";
PreparedStatement statement = null;
2022-03-31 20:08:50 +00:00
try {
statement = Database.getDatabase().getConnection().prepareStatement(sql);
2022-03-31 20:08:50 +00:00
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String commandName = resultSet.getString("command_name");
List<ScopeInfo> scopeInfoList = commandList.getOrDefault(commandName, new ArrayList<>());
2022-03-31 20:08:50 +00:00
scopeInfoList.add(new ScopeInfo(
CommandScope.valueOf(resultSet.getString("scope")),
resultSet.getLong("location_id")));
commandList.put(commandName, scopeInfoList);
2022-03-31 20:08:50 +00:00
}
} catch (SQLException exception) {
Logger.sql(exception);
} finally {
try {
if (statement != null)
statement.close();
} catch (SQLException exception) {
Logger.sql(exception);
}
2022-03-31 20:08:50 +00:00
}
}
synchronized public List<ScopeInfo> getActiveLocations(String command) {
return commandList.getOrDefault(command, new ArrayList<>());
2022-03-09 21:37:44 +00:00
}
2022-03-09 21:11:11 +00:00
}