Added button manager and some buttons for suggestions
Added a way to reload commands Added a way to set output channels
This commit is contained in:
@@ -2,7 +2,9 @@ package com.alttd.commandManager;
|
||||
|
||||
import com.alttd.commandManager.commands.AddCommand.CommandManage;
|
||||
import com.alttd.commandManager.commands.CommandHelp;
|
||||
import com.alttd.commandManager.commands.CommandSetOutputChannel;
|
||||
import com.alttd.commandManager.commands.CommandSuggestion;
|
||||
import com.alttd.commandManager.commands.CommandUpdateCommands;
|
||||
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
|
||||
import com.alttd.database.Database;
|
||||
import com.alttd.modalManager.ModalManager;
|
||||
@@ -19,8 +21,10 @@ import java.awt.*;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandManager extends ListenerAdapter {
|
||||
@@ -36,7 +40,9 @@ public class CommandManager extends ListenerAdapter {
|
||||
new CommandManage(jda, this),
|
||||
new CommandHelp(jda, this),
|
||||
new CommandPoll(jda, this),
|
||||
new CommandSuggestion(jda, modalManager, this));
|
||||
new CommandSuggestion(jda, modalManager, this),
|
||||
new CommandSetOutputChannel(),
|
||||
new CommandUpdateCommands(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,6 +89,8 @@ public class CommandManager extends ListenerAdapter {
|
||||
public List<DiscordCommand> getCommands(Guild guild) {
|
||||
return commands.stream().filter(command -> {
|
||||
List<ScopeInfo> scopeInfoList = commandList.get(command.getName());
|
||||
if (scopeInfoList == null)
|
||||
return false;
|
||||
for (ScopeInfo scopeInfo : scopeInfoList) {
|
||||
switch (scopeInfo.getScope()) {
|
||||
case GLOBAL -> {
|
||||
|
||||
@@ -7,8 +7,10 @@ import com.alttd.commandManager.SubOption;
|
||||
import com.alttd.util.Logger;
|
||||
import com.alttd.util.Util;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
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.DefaultMemberPermissions;
|
||||
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.Commands;
|
||||
@@ -29,7 +31,7 @@ public class CommandManage extends DiscordCommand {
|
||||
new SubcommandData("disable", "Disable a command")
|
||||
.addOption(OptionType.STRING, "command", "Name of the command to disable", true, true)
|
||||
);
|
||||
commandData.setDefaultEnabled(true);
|
||||
commandData.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR));
|
||||
Util.registerSubOptions(subOptionsMap,
|
||||
new SubCommandEnable(commandManager, null, this),
|
||||
new SubCommandEnable(commandManager, null, this)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandEvidence {
|
||||
}
|
||||
@@ -26,7 +26,7 @@ public class CommandHelp extends DiscordCommand {
|
||||
private final CommandManager commandManager;
|
||||
private final CommandData commandData;
|
||||
|
||||
public CommandHelp(JDA jda, CommandManager commandManager) {
|
||||
public CommandHelp(JDA jda, CommandManager commandManager) { //TODO make this work without specifying a command...
|
||||
this.commandManager = commandManager;
|
||||
|
||||
commandData = Commands.slash(getName(), "Show info about all commands or a specific command.")
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
import com.alttd.commandManager.DiscordCommand;
|
||||
import com.alttd.database.queries.commandOutputChannels.CommandOutputChannels;
|
||||
import com.alttd.database.queries.commandOutputChannels.OutputType;
|
||||
import com.alttd.util.Util;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
|
||||
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
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.Commands;
|
||||
import net.dv8tion.jda.api.requests.RestAction;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandSetOutputChannel extends DiscordCommand {
|
||||
|
||||
private final CommandData commandData;
|
||||
|
||||
public CommandSetOutputChannel() {
|
||||
commandData = Commands.slash(getName(), "Set up output channels")
|
||||
.addOption(OptionType.STRING, "type", "The type of output channel", true, true)
|
||||
.addOption(OptionType.CHANNEL, "channel", "The channel the specified output should go into", true)
|
||||
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "setoutputchannel";
|
||||
}
|
||||
|
||||
@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")).setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
OptionMapping option = event.getOption("type");
|
||||
if (option == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to find type parameter.")).setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
String type = option.getAsString().toUpperCase();
|
||||
OutputType outputType;
|
||||
try {
|
||||
outputType = OutputType.valueOf(type);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "Not a valid output type.")).setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
option = event.getOption("channel");
|
||||
if (option == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to find channel parameter.")).setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
GuildChannelUnion channel = option.getAsChannel();
|
||||
switch (channel.getType()) {
|
||||
case TEXT, NEWS, GUILD_NEWS_THREAD, GUILD_PUBLIC_THREAD, GUILD_PRIVATE_THREAD -> {
|
||||
boolean success = CommandOutputChannels.setOutputChannel(guild.getIdLong(), outputType, channel.getIdLong());
|
||||
if (success)
|
||||
event.replyEmbeds(Util.genericSuccessEmbed("Success", "Set channel " + channel.getAsMention() + " as the output channel for " + outputType.name() + "."))
|
||||
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
||||
else
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to store the new channel output in the database"))
|
||||
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
||||
}
|
||||
default -> event.replyEmbeds(Util.genericErrorEmbed("Error", "The channel type " + channel.getType().name() + " is not a valid output channel type"))
|
||||
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||
AutoCompleteQuery focusedOption = event.getFocusedOption();
|
||||
if (!focusedOption.getType().equals(OptionType.STRING) || !focusedOption.getName().equalsIgnoreCase("type"))
|
||||
event.replyChoices(Collections.emptyList()).queue();
|
||||
String value = focusedOption.getValue().toLowerCase();
|
||||
List<String> collect = Arrays.stream(OutputType.values())
|
||||
.map(Enum::name)
|
||||
.filter(type -> type.toLowerCase().startsWith(value.toLowerCase()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (int i = collect.size(); i > 25; i--) //Can only have 25 options
|
||||
collect.remove(i - 1);
|
||||
event.replyChoiceStrings(collect).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getCommandData() {
|
||||
return commandData;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
import net.dv8tion.jda.api.interactions.components.Modal;
|
||||
import net.dv8tion.jda.api.requests.RestAction;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@@ -41,7 +42,7 @@ public class CommandSuggestion extends DiscordCommand {
|
||||
"Unable to find suggestion modal, please report this issue to Teri")).queue();
|
||||
return;
|
||||
}
|
||||
event.replyModal(modal).queue();
|
||||
event.replyModal(modal).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
import com.alttd.commandManager.CommandManager;
|
||||
import com.alttd.commandManager.DiscordCommand;
|
||||
import com.alttd.util.Util;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
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.DefaultMemberPermissions;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
import net.dv8tion.jda.api.requests.RestAction;
|
||||
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandUpdateCommands extends DiscordCommand {
|
||||
|
||||
private final CommandData commandData;
|
||||
private final CommandManager commandManager;
|
||||
|
||||
public CommandUpdateCommands(CommandManager commandManager) {
|
||||
this.commandManager = commandManager;
|
||||
this.commandData = Commands.slash(getName(), "Updates all commands for this bot in this guild")
|
||||
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "updatecommands";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
Guild guild = event.getGuild();
|
||||
if (guild == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "This command can only be ran from a guild"))
|
||||
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
||||
return;
|
||||
}
|
||||
|
||||
ReplyCallbackAction replyCallbackAction = event.deferReply(true);
|
||||
List<CommandData> commandDataList = commandManager.getCommands(guild).stream().map(DiscordCommand::getCommandData).collect(Collectors.toList());
|
||||
guild.updateCommands().addCommands(commandDataList).queue(success -> replyCallbackAction.setEmbeds(Util.genericSuccessEmbed("Success", "Updated the commands in this guild"))
|
||||
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure), Util::handleFailure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||
event.replyChoices(Collections.emptyList()).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getCommandData() {
|
||||
return commandData;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.alttd.commandManager.SubOption;
|
||||
import com.alttd.util.Logger;
|
||||
import com.alttd.util.Util;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
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.DefaultMemberPermissions;
|
||||
@@ -53,7 +54,7 @@ public class CommandPoll extends DiscordCommand {
|
||||
new SubcommandData("results", "Get the results for a poll")
|
||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true)
|
||||
.addOption(OptionType.STRING, "message_id", "Id of the poll you want the results for", true));
|
||||
commandData.setDefaultPermissions(DefaultMemberPermissions.ENABLED);
|
||||
commandData.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR));
|
||||
Util.registerSubOptions(subOptionsMap,
|
||||
new SubCommandAdd(null,this),
|
||||
new SubCommandAddButton(null, this),
|
||||
|
||||
+1
-1
@@ -70,7 +70,7 @@ public class SubCommandAddButton extends SubCommand {
|
||||
.queue();
|
||||
return;
|
||||
}
|
||||
event.deferReply().queue(hook ->
|
||||
event.deferReply(true).queue(hook ->
|
||||
channel.retrieveMessageById(messageId).queue(
|
||||
message -> updatePoll(channel, rowId, buttonName, message, hook),
|
||||
throwable -> failedToGetMessage(throwable, hook)));
|
||||
|
||||
Reference in New Issue
Block a user