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-09-15 20:03:36 +00:00
|
|
|
import com.alttd.commandManager.commands.*;
|
2022-04-01 00:54:33 +00:00
|
|
|
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
|
2022-09-16 19:34:57 +00:00
|
|
|
import com.alttd.contextMenuManager.ContextMenuManager;
|
2022-03-31 20:08:50 +00:00
|
|
|
import com.alttd.database.Database;
|
2023-03-02 23:48:30 +00:00
|
|
|
import com.alttd.listeners.LockedChannel;
|
2022-09-14 19:36:59 +00:00
|
|
|
import com.alttd.modalManager.ModalManager;
|
2022-10-21 00:59:00 +00:00
|
|
|
import com.alttd.selectMenuManager.SelectMenuManager;
|
2022-03-31 20:08:50 +00:00
|
|
|
import com.alttd.util.Logger;
|
2022-04-01 00:54:33 +00:00
|
|
|
import net.dv8tion.jda.api.JDA;
|
2022-09-10 03:20:24 +00:00
|
|
|
import net.dv8tion.jda.api.entities.Guild;
|
2022-04-01 00:54:33 +00:00
|
|
|
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-03-31 20:08:50 +00:00
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.SQLException;
|
2022-09-15 19:34:27 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
2022-03-09 21:11:11 +00:00
|
|
|
import java.util.List;
|
2022-09-15 19:34:27 +00:00
|
|
|
import java.util.Optional;
|
2022-04-01 00:54:33 +00:00
|
|
|
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
|
|
|
|
2023-03-02 23:48:30 +00:00
|
|
|
public CommandManager(JDA jda, ModalManager modalManager, ContextMenuManager contextMenuManager, LockedChannel lockedChannel, SelectMenuManager selectMenuManager) {
|
2022-09-10 03:20:24 +00:00
|
|
|
commandList.put("manage", new ArrayList<>(List.of(new ScopeInfo(CommandScope.GLOBAL, 0))));
|
2022-04-22 19:20:24 +00:00
|
|
|
loadCommands();
|
2022-09-10 03:20:24 +00:00
|
|
|
Logger.info("Loading commands...");
|
2022-09-16 20:52:31 +00:00
|
|
|
CommandSetToggleableRoles commandSetToggleableRoles = new CommandSetToggleableRoles(jda, this);
|
2022-09-10 03:20:24 +00:00
|
|
|
commands = List.of(
|
2022-09-16 19:34:57 +00:00
|
|
|
new CommandManage(jda, this, contextMenuManager),
|
2022-09-10 03:20:24 +00:00
|
|
|
new CommandHelp(jda, this),
|
2022-09-14 19:36:59 +00:00
|
|
|
new CommandPoll(jda, this),
|
2022-09-15 19:34:27 +00:00
|
|
|
new CommandSuggestion(jda, modalManager, this),
|
2022-10-01 01:39:51 +00:00
|
|
|
new CommandSuggestCrateItem(jda, modalManager, this),
|
2022-09-15 19:40:44 +00:00
|
|
|
new CommandSetOutputChannel(jda, this),
|
2022-09-15 20:03:36 +00:00
|
|
|
new CommandUpdateCommands(jda, this),
|
2022-09-16 00:11:49 +00:00
|
|
|
new CommandEvidence(jda, modalManager, this),
|
2022-09-16 00:42:16 +00:00
|
|
|
new CommandFlag(jda, this),
|
2022-09-29 11:01:14 +00:00
|
|
|
new CommandHistory(jda, this),
|
2022-09-29 17:06:30 +00:00
|
|
|
new CommandSeen(jda, this),
|
2022-09-16 20:52:31 +00:00
|
|
|
commandSetToggleableRoles,
|
2022-09-21 19:49:42 +00:00
|
|
|
new CommandToggleRole(commandSetToggleableRoles, jda, this),
|
2022-10-03 14:58:20 +00:00
|
|
|
new CommandRemindMe(jda, this, modalManager),
|
2023-03-02 23:48:30 +00:00
|
|
|
new CommandSoftLock(jda, this, lockedChannel),
|
2022-10-21 00:59:00 +00:00
|
|
|
new CommandAuction(jda, this, selectMenuManager));
|
2022-03-09 21:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-04-01 00:54:33 +00:00
|
|
|
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
|
2022-09-10 03:20:24 +00:00
|
|
|
String commandName = event.getName();
|
2022-04-01 00:54:33 +00:00
|
|
|
Optional<DiscordCommand> first = commands.stream()
|
2022-09-10 03:20:24 +00:00
|
|
|
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(commandName))
|
2022-04-01 00:54:33 +00:00
|
|
|
.findFirst();
|
|
|
|
|
if (first.isEmpty()) {
|
2023-01-04 01:28:49 +00:00
|
|
|
// 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();
|
2022-04-01 00:54:33 +00:00
|
|
|
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()
|
2022-09-10 03:20:24 +00:00
|
|
|
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(event.getName()))
|
2022-04-01 00:54:33 +00:00
|
|
|
.findFirst();
|
|
|
|
|
if (first.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
first.get().suggest(event);
|
2022-03-09 21:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-24 00:33:24 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-10 03:20:24 +00:00
|
|
|
public List<DiscordCommand> getCommands(Guild guild) {
|
2022-04-01 00:54:33 +00:00
|
|
|
return commands.stream().filter(command -> {
|
|
|
|
|
List<ScopeInfo> scopeInfoList = commandList.get(command.getName());
|
2022-09-15 19:34:27 +00:00
|
|
|
if (scopeInfoList == null)
|
|
|
|
|
return false;
|
2022-04-01 00:54:33 +00:00
|
|
|
for (ScopeInfo scopeInfo : scopeInfoList) {
|
|
|
|
|
switch (scopeInfo.getScope()) {
|
|
|
|
|
case GLOBAL -> {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case GUILD -> {
|
2022-09-10 03:20:24 +00:00
|
|
|
if (guild.getIdLong() == scopeInfo.getId())
|
2022-04-01 00:54:33 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 00:33:24 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-10 03:20:24 +00:00
|
|
|
synchronized private void loadCommands() {
|
2022-04-01 00:54:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2022-09-10 03:20:24 +00:00
|
|
|
synchronized public List<ScopeInfo> getActiveLocations(String command) {
|
2022-04-01 00:54:33 +00:00
|
|
|
return commandList.getOrDefault(command, new ArrayList<>());
|
2022-03-09 21:37:44 +00:00
|
|
|
}
|
2022-03-09 21:11:11 +00:00
|
|
|
}
|