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;
|
2022-04-01 00:54:33 +00:00
|
|
|
import com.alttd.commandManager.commands.CommandHelp;
|
|
|
|
|
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
|
2022-03-31 20:08:50 +00:00
|
|
|
import com.alttd.database.Database;
|
|
|
|
|
import com.alttd.util.Logger;
|
2022-04-01 00:54:33 +00:00
|
|
|
import net.dv8tion.jda.api.EmbedBuilder;
|
|
|
|
|
import net.dv8tion.jda.api.JDA;
|
|
|
|
|
import net.dv8tion.jda.api.entities.TextChannel;
|
|
|
|
|
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;
|
|
|
|
|
|
2022-04-01 00:54:33 +00:00
|
|
|
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.ArrayList;
|
2022-03-09 21:11:11 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
2022-04-01 00:54:33 +00:00
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.stream.Collectors;
|
2022-03-09 21:11:11 +00:00
|
|
|
|
|
|
|
|
public class CommandManager extends ListenerAdapter {
|
|
|
|
|
|
|
|
|
|
private final List<DiscordCommand> commands;
|
2022-04-01 00:54:33 +00:00
|
|
|
private final HashMap<String, List<ScopeInfo>> commandList = new HashMap<>();
|
2022-03-09 21:11:11 +00:00
|
|
|
|
2022-04-01 00:54:33 +00:00
|
|
|
public CommandManager(JDA jda) {
|
|
|
|
|
commands = List.of(new CommandHelp(jda, this),
|
2022-04-15 19:44:26 +00:00
|
|
|
new CommandPoll(jda, this),
|
2022-04-15 20:00:33 +00:00
|
|
|
new CommandManage(jda, this));
|
2022-04-01 00:54:33 +00:00
|
|
|
loadCommands();
|
2022-03-09 21:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-04-01 00:54:33 +00:00
|
|
|
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
|
|
|
|
|
Optional<DiscordCommand> first = commands.stream()
|
|
|
|
|
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(event.getCommandString()))
|
|
|
|
|
.findFirst();
|
|
|
|
|
if (first.isEmpty()) {
|
|
|
|
|
event.replyEmbeds(new EmbedBuilder()
|
|
|
|
|
.setTitle("Invalid command")
|
|
|
|
|
.setDescription("We couldn't find this command, 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
|
|
|
|
2022-04-01 00:54:33 +00:00
|
|
|
@Override
|
|
|
|
|
public void onCommandAutoCompleteInteraction(@NotNull CommandAutoCompleteInteractionEvent event) {
|
|
|
|
|
Optional<DiscordCommand> first = commands.stream()
|
|
|
|
|
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(event.getCommandString()))
|
|
|
|
|
.findFirst();
|
|
|
|
|
if (first.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
first.get().suggest(event);
|
2022-03-09 21:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-09 21:37:44 +00:00
|
|
|
public List<DiscordCommand> getCommands() {
|
|
|
|
|
return commands;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 00:54:33 +00:00
|
|
|
public List<DiscordCommand> getCommands(TextChannel textChannel) {
|
|
|
|
|
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 (textChannel.getGuild().getIdLong() == scopeInfo.getId())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadCommands() {
|
|
|
|
|
String sql = "SELECT * FROM commands";
|
2022-04-15 19:44:26 +00:00
|
|
|
PreparedStatement statement = null;
|
2022-03-31 20:08:50 +00:00
|
|
|
|
|
|
|
|
try {
|
2022-04-15 19:44:26 +00:00
|
|
|
statement = Database.getDatabase().getConnection().prepareStatement(sql);
|
2022-03-31 20:08:50 +00:00
|
|
|
ResultSet resultSet = statement.executeQuery();
|
|
|
|
|
|
|
|
|
|
while (resultSet.next()) {
|
2022-04-01 00:54:33 +00:00
|
|
|
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")));
|
2022-04-01 00:54:33 +00:00
|
|
|
commandList.put(commandName, scopeInfoList);
|
2022-03-31 20:08:50 +00:00
|
|
|
}
|
|
|
|
|
} catch (SQLException exception) {
|
|
|
|
|
Logger.sql(exception);
|
2022-04-15 19:44:26 +00:00
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (statement != null)
|
|
|
|
|
statement.close();
|
|
|
|
|
} catch (SQLException exception) {
|
|
|
|
|
Logger.sql(exception);
|
|
|
|
|
}
|
2022-03-31 20:08:50 +00:00
|
|
|
}
|
2022-04-01 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|