Added untested Disable and Enable sub command
This commit is contained in:
parent
83af33dcc4
commit
45e7d9994b
|
|
@ -1,15 +1,12 @@
|
|||
package com.alttd;
|
||||
|
||||
import com.alttd.commandManager.CommandManager;
|
||||
import com.alttd.commandManager.listeners.JDAListener;
|
||||
import com.alttd.config.SettingsConfig;
|
||||
import com.alttd.config.MessagesConfig;
|
||||
import com.alttd.config.SettingsConfig;
|
||||
import com.alttd.console.ConsoleCommandManager;
|
||||
import com.alttd.database.Database;
|
||||
import com.alttd.database.DatabaseTables;
|
||||
import com.alttd.permissions.PermissionManager;
|
||||
import com.alttd.util.Logger;
|
||||
import com.mysql.cj.log.Log;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.OnlineStatus;
|
||||
|
|
@ -18,7 +15,6 @@ import net.dv8tion.jda.api.entities.Activity;
|
|||
import javax.security.auth.login.LoginException;
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static java.lang.System.exit;
|
||||
|
||||
|
|
@ -43,8 +39,8 @@ public class AltitudeBot {
|
|||
jda = JDABuilder.createDefault(SettingsConfig.TOKEN).build();
|
||||
} catch (LoginException e) {
|
||||
Logger.info("Unable to log in, shutting down (check token in settings.yml).");
|
||||
exit(1);
|
||||
Logger.exception(e);
|
||||
exit(1);
|
||||
}
|
||||
DatabaseTables.createTables(Database.getDatabase().getConnection());
|
||||
ConsoleCommandManager.startConsoleCommands(jda);
|
||||
|
|
|
|||
|
|
@ -63,6 +63,14 @@ public class CommandManager extends ListenerAdapter {
|
|||
first.get().suggest(event);
|
||||
}
|
||||
|
||||
public DiscordCommand getCommand(String name) {
|
||||
for (DiscordCommand command : commands) {
|
||||
if (command.getName().equalsIgnoreCase(name))
|
||||
return command;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<DiscordCommand> getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
|
@ -85,6 +93,25 @@ public class CommandManager extends ListenerAdapter {
|
|||
}).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;
|
||||
}
|
||||
|
||||
private void loadCommands() {
|
||||
String sql = "SELECT * FROM commands";
|
||||
PreparedStatement statement = null;
|
||||
|
|
|
|||
|
|
@ -17,4 +17,6 @@ public class ScopeInfo {
|
|||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
//TODO check if equals needs to be overwritten for it to work as expected
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,10 +46,8 @@ public class CommandManage extends DiscordCommand {
|
|||
)
|
||||
.addSubcommands(
|
||||
new SubcommandData("enable", "Enable a command in a channel")
|
||||
.addOption(OptionType.CHANNEL, "channel", "Channel to enable this command in", true)
|
||||
.addOption(OptionType.STRING, "command", "Name of the command to enable", true, true),
|
||||
new SubcommandData("disable", "Disable a command")
|
||||
.addOption(OptionType.CHANNEL, "channel", "Channel to disable this command in", true)
|
||||
.addOption(OptionType.STRING, "command", "Name of the command to disable", true, true)
|
||||
);
|
||||
slashCommandData.setDefaultEnabled(true);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,20 @@
|
|||
package com.alttd.commandManager.commands.AddCommand;
|
||||
|
||||
import com.alttd.commandManager.CommandManager;
|
||||
import com.alttd.commandManager.DiscordCommand;
|
||||
import com.alttd.commandManager.SubCommand;
|
||||
import com.alttd.commandManager.SubCommandGroup;
|
||||
import com.alttd.commandManager.*;
|
||||
import com.alttd.database.Database;
|
||||
import com.alttd.templates.Parser;
|
||||
import com.alttd.templates.Template;
|
||||
import com.alttd.util.Logger;
|
||||
import com.alttd.util.Util;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SubCommandDisable extends SubCommand {
|
||||
|
||||
|
|
@ -23,12 +32,87 @@ public class SubCommandDisable extends SubCommand {
|
|||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
Guild guild = event.getGuild();
|
||||
if (guild == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "This command can only be used within guilds")).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
OptionMapping option = event.getOption("command");
|
||||
if (option == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to find command parameter.")).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
String commandName = option.getAsString();
|
||||
DiscordCommand command = commandManager.getCommand(commandName);
|
||||
if (command == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to find a command with that name.")).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
if (disableCommand(command, guild.getIdLong())) {
|
||||
event.replyEmbeds(Util.genericSuccessEmbed("Disabled command",
|
||||
Parser.parse("Successfully disabled <command> in <guild>!",
|
||||
Template.of("command", commandName.toLowerCase()),
|
||||
Template.of("guild", guild.getName())
|
||||
))).queue();
|
||||
|
||||
} else {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Failed to disable command",
|
||||
Parser.parse("Unable to disable <command> in <guild>, is it already disabled?",
|
||||
Template.of("command", commandName.toLowerCase()),
|
||||
Template.of("guild", guild.getName())
|
||||
))).queue();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean disableCommand(DiscordCommand command, long guildId) {
|
||||
if (!commandManager.disableCommand(command.getName(), new ScopeInfo(CommandScope.GUILD, guildId)))
|
||||
return false;
|
||||
String sql = "REMOVE FROM commands WHERE command_name = ? AND scope = ? and location_id = ?";
|
||||
PreparedStatement statement = null;
|
||||
|
||||
try {
|
||||
statement = Database.getDatabase().getConnection().prepareStatement(sql);
|
||||
statement.setString(1, command.getName());
|
||||
statement.setString(2, "GUILD");
|
||||
statement.setLong(3, guildId);
|
||||
if (!statement.execute()) {
|
||||
Logger.warning("Unable to disable command: % for guild: %", command.getName(), String.valueOf(guildId));
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
Logger.sql(exception);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (statement != null)
|
||||
statement.close();
|
||||
} catch (SQLException exception) {
|
||||
Logger.sql(exception);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||
|
||||
OptionMapping option = event.getOption("command");
|
||||
Guild guild = event.getGuild();
|
||||
if (guild == null || option == null) {
|
||||
event.replyChoiceStrings(new ArrayList<>()).queue();
|
||||
return;
|
||||
}
|
||||
String commandName = option.getAsString().toLowerCase();
|
||||
ScopeInfo scopeInfo = new ScopeInfo(CommandScope.GLOBAL, event.getGuild().getIdLong());
|
||||
event.replyChoiceStrings(commandManager.getCommands().stream()
|
||||
.map(DiscordCommand::getName)
|
||||
.filter(name -> name.toLowerCase().startsWith(commandName))
|
||||
.filter(name -> commandManager.getActiveLocations(name).contains(scopeInfo))
|
||||
.limit(25)
|
||||
.collect(Collectors.toList()))
|
||||
.queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,11 +1,24 @@
|
|||
package com.alttd.commandManager.commands.AddCommand;
|
||||
|
||||
import com.alttd.commandManager.CommandManager;
|
||||
import com.alttd.commandManager.DiscordCommand;
|
||||
import com.alttd.commandManager.SubCommand;
|
||||
import com.alttd.commandManager.SubCommandGroup;
|
||||
import com.alttd.commandManager.*;
|
||||
import com.alttd.database.Database;
|
||||
import com.alttd.templates.Parser;
|
||||
import com.alttd.templates.Template;
|
||||
import com.alttd.util.Logger;
|
||||
import com.alttd.util.Util;
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
import com.mysql.cj.log.Log;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SubCommandEnable extends SubCommand {
|
||||
|
||||
|
|
@ -23,12 +36,87 @@ public class SubCommandEnable extends SubCommand {
|
|||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
Guild guild = event.getGuild();
|
||||
if (guild == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "This command can only be used within guilds")).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
OptionMapping option = event.getOption("command");
|
||||
if (option == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to find command parameter.")).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
String commandName = option.getAsString();
|
||||
DiscordCommand command = commandManager.getCommand(commandName);
|
||||
if (command == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to find a command with that name.")).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
if (enableCommand(command, guild.getIdLong())) {
|
||||
event.replyEmbeds(Util.genericSuccessEmbed("Enabled command",
|
||||
Parser.parse("Successfully enabled <command> in <guild>!",
|
||||
Template.of("command", commandName.toLowerCase()),
|
||||
Template.of("guild", guild.getName())
|
||||
))).queue();
|
||||
|
||||
} else {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Failed to enable command",
|
||||
Parser.parse("Unable to enabled <command> in <guild>, is it already enabled?",
|
||||
Template.of("command", commandName.toLowerCase()),
|
||||
Template.of("guild", guild.getName())
|
||||
))).queue();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean enableCommand(DiscordCommand command, long guildId) {
|
||||
if (!commandManager.enableCommand(command.getName(), new ScopeInfo(CommandScope.GUILD, guildId)))
|
||||
return false;
|
||||
String sql = "INSERT INTO commands (command_name, scope, location_id) VALUES(?, ?, ?)";
|
||||
PreparedStatement statement = null;
|
||||
|
||||
try {
|
||||
statement = Database.getDatabase().getConnection().prepareStatement(sql);
|
||||
statement.setString(1, command.getName());
|
||||
statement.setString(2, "GUILD");
|
||||
statement.setLong(3, guildId);
|
||||
if (!statement.execute()) {
|
||||
Logger.warning("Unable to enable command: % for guild: %", command.getName(), String.valueOf(guildId));
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
Logger.sql(exception);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (statement != null)
|
||||
statement.close();
|
||||
} catch (SQLException exception) {
|
||||
Logger.sql(exception);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||
|
||||
OptionMapping option = event.getOption("command");
|
||||
Guild guild = event.getGuild();
|
||||
if (guild == null || option == null) {
|
||||
event.replyChoiceStrings(new ArrayList<>()).queue();
|
||||
return;
|
||||
}
|
||||
String commandName = option.getAsString().toLowerCase();
|
||||
ScopeInfo scopeInfo = new ScopeInfo(CommandScope.GLOBAL, event.getGuild().getIdLong());
|
||||
event.replyChoiceStrings(commandManager.getCommands().stream()
|
||||
.map(DiscordCommand::getName)
|
||||
.filter(name -> name.toLowerCase().startsWith(commandName))
|
||||
.filter(name -> !commandManager.getActiveLocations(name).contains(scopeInfo))
|
||||
.limit(25)
|
||||
.collect(Collectors.toList()))
|
||||
.queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandRoles {
|
||||
//TODO allow users to do /roles and get a dropdown that has all the roles they can add to themselves (mayb /roles notifications and /roles display or something idk)
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ public class JDAListener extends ListenerAdapter {
|
|||
|
||||
@Override
|
||||
public void onReady(@NotNull ReadyEvent event) {
|
||||
Logger.info("JDA ready registering commands.");
|
||||
Logger.info("JDA ready to register commands.");
|
||||
jda.addEventListener(new CommandManager(jda));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user