Subcommand Fix, new methods and constructor change

This commit is contained in:
unknown 2022-03-29 08:35:38 -07:00
parent eebc73ac0c
commit 3061c08774
8 changed files with 101 additions and 53 deletions

View File

@ -12,6 +12,7 @@ import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.restaction.CommandCreateAction; import net.dv8tion.jda.api.requests.restaction.CommandCreateAction;
import net.dv8tion.jda.api.utils.ChunkingFilter; import net.dv8tion.jda.api.utils.ChunkingFilter;
@ -210,6 +211,13 @@ public class Kiafumi {
//Any intelligent IDE will rage about the option not being used, it's added to the action then executed later, DO not edit this (please). //Any intelligent IDE will rage about the option not being used, it's added to the action then executed later, DO not edit this (please).
cca.addOption(ci.getOptions().get(name), name, ci.getOptionDescriptions().get(name), ci.getOptionRequirements().get(name)); cca.addOption(ci.getOptions().get(name), name, ci.getOptionDescriptions().get(name), ci.getOptionRequirements().get(name));
} }
for(String name : ci.getSubCommands().keySet()) {
CommandInfo si = ci.getSubCommands().get(name);
SubcommandData sd = new SubcommandData(si.getName(), si.getDescription());
for(String option : si.getOptions().keySet()) {
sd.addOption(si.getOptions().get(option), option, si.getOptionDescriptions().get(option), si.getOptionRequirements().get(option));
}
}
//Done //Done
cca.queue(); cca.queue();
continue; continue;

View File

@ -1,6 +1,7 @@
package moe.oko.Kiafumi.command; package moe.oko.Kiafumi.command;
import moe.oko.Kiafumi.util.CommandInfo; import moe.oko.Kiafumi.util.CommandInfo;
import moe.oko.Kiafumi.util.CommandType;
import moe.oko.Kiafumi.util.EmbedUI; import moe.oko.Kiafumi.util.EmbedUI;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
@ -70,7 +71,7 @@ public class DreidelCommand extends CommandClass {
@Override @Override
public List<CommandInfo> getSlashCommandInfo() { public List<CommandInfo> getSlashCommandInfo() {
List<CommandInfo> cil = new ArrayList<>(); List<CommandInfo> cil = new ArrayList<>();
CommandInfo ci = new CommandInfo("dreidel", "picks a random person out of 4 values."); CommandInfo ci = new CommandInfo("dreidel", "picks a random person out of 4 values.", CommandType.COMMAND);
ci.addOption("value1", "first value for spin", OptionType.USER, true); ci.addOption("value1", "first value for spin", OptionType.USER, true);
ci.addOption("value2", "second value for spin", OptionType.USER, true); ci.addOption("value2", "second value for spin", OptionType.USER, true);
ci.addOption("value3", "third value for spin", OptionType.USER, false); ci.addOption("value3", "third value for spin", OptionType.USER, false);

View File

@ -1,6 +1,7 @@
package moe.oko.Kiafumi.command; package moe.oko.Kiafumi.command;
import moe.oko.Kiafumi.util.CommandInfo; import moe.oko.Kiafumi.util.CommandInfo;
import moe.oko.Kiafumi.util.CommandType;
import moe.oko.Kiafumi.util.EmbedUI; import moe.oko.Kiafumi.util.EmbedUI;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@ -68,7 +69,7 @@ public class DuckCommand extends CommandClass{
@Override @Override
public List<CommandInfo> getSlashCommandInfo() { public List<CommandInfo> getSlashCommandInfo() {
List<CommandInfo> cil = new ArrayList<>(); List<CommandInfo> cil = new ArrayList<>();
CommandInfo ci = new CommandInfo("search", "Looks up with DuckDuckGo your query!"); CommandInfo ci = new CommandInfo("search", "Looks up with DuckDuckGo your query!", CommandType.COMMAND);
ci.addOption("query", "The query to be searched", OptionType.STRING, true); ci.addOption("query", "The query to be searched", OptionType.STRING, true);
cil.add(ci); cil.add(ci);
return cil; return cil;

View File

@ -2,6 +2,7 @@ package moe.oko.Kiafumi.command;
import moe.oko.Kiafumi.Kiafumi; import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.util.CommandInfo; import moe.oko.Kiafumi.util.CommandInfo;
import moe.oko.Kiafumi.util.CommandType;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
@ -52,7 +53,7 @@ public class InviteCommand extends CommandClass{
@Override @Override
public List<CommandInfo> getSlashCommandInfo() { public List<CommandInfo> getSlashCommandInfo() {
List<CommandInfo> cil = new ArrayList<>(); List<CommandInfo> cil = new ArrayList<>();
CommandInfo ci = new CommandInfo("invite", "Returns an invite for Kiafumi."); CommandInfo ci = new CommandInfo("invite", "Returns an invite for Kiafumi.", CommandType.COMMAND);
cil.add(ci); cil.add(ci);
return cil; return cil;
} }

View File

@ -2,6 +2,7 @@ package moe.oko.Kiafumi.command;
import moe.oko.Kiafumi.Kiafumi; import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.util.CommandInfo; import moe.oko.Kiafumi.util.CommandInfo;
import moe.oko.Kiafumi.util.CommandType;
import moe.oko.Kiafumi.util.EmbedUI; import moe.oko.Kiafumi.util.EmbedUI;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@ -72,7 +73,7 @@ public class PingCommand extends CommandClass{
@Override @Override
public List<CommandInfo> getSlashCommandInfo() { public List<CommandInfo> getSlashCommandInfo() {
List<CommandInfo> si = new ArrayList<>(); List<CommandInfo> si = new ArrayList<>();
CommandInfo ci = new CommandInfo("ping", "Returns bot latency with a twist!"); CommandInfo ci = new CommandInfo("ping", "Returns bot latency with a twist!", CommandType.COMMAND);
si.add(ci); si.add(ci);
return si; return si;
} }

View File

@ -3,6 +3,7 @@ package moe.oko.Kiafumi.command;
import moe.oko.Kiafumi.Kiafumi; import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.model.Server; import moe.oko.Kiafumi.model.Server;
import moe.oko.Kiafumi.util.CommandInfo; import moe.oko.Kiafumi.util.CommandInfo;
import moe.oko.Kiafumi.util.CommandType;
import moe.oko.Kiafumi.util.EmbedUI; import moe.oko.Kiafumi.util.EmbedUI;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@ -46,41 +47,44 @@ public class SettingCommand extends CommandClass {
e.getHook().sendMessageEmbeds(eb.build()).queue(); e.getHook().sendMessageEmbeds(eb.build()).queue();
return; return;
case "setting": case "setting":
e.deferReply().queue(); switch(e.getSubcommandName().toLowerCase()) {
String opt = e.getOption("setting_name").getAsString(); case "view":
EmbedBuilder eb1 = new EmbedBuilder() e.deferReply().queue();
.setColor(EmbedUI.INFO) String opt = e.getOption("name").getAsString();
.setTitle(opt) EmbedBuilder eb1 = new EmbedBuilder()
.setDescription("Value: `" + server.getOptionByString(opt) + '`') .setColor(EmbedUI.INFO)
.setFooter(EmbedUI.BRAND) .setTitle(opt)
.setTimestamp(ZonedDateTime.now()); .setDescription("Value: `" + server.getOptionByString(opt) + '`')
e.getHook().sendMessageEmbeds(eb1.build()).queue(); .setFooter(EmbedUI.BRAND)
return; .setTimestamp(ZonedDateTime.now());
case "setting_set": e.getHook().sendMessageEmbeds(eb1.build()).queue();
e.deferReply().queue(); return;
String opt1 = e.getOption("setting_name").getAsString(); case "set":
String opt2 = e.getOption("setting_value").getAsString(); e.deferReply().queue();
String response = server.setOptionByString(opt1, opt2); String opt1 = e.getOption("name").getAsString();
EmbedBuilder eb2 = new EmbedBuilder() String opt2 = e.getOption("value").getAsString();
.setColor(EmbedUI.SUCCESS) String response = server.setOptionByString(opt1, opt2);
.setTitle(opt1) EmbedBuilder eb2 = new EmbedBuilder()
.setDescription(response) .setColor(EmbedUI.SUCCESS)
.setFooter(EmbedUI.BRAND) .setTitle(opt1)
.setTimestamp(ZonedDateTime.now()); .setDescription(response)
e.getHook().sendMessageEmbeds(eb2.build()).queue(); .setFooter(EmbedUI.BRAND)
return; .setTimestamp(ZonedDateTime.now());
case "setting_clear": e.getHook().sendMessageEmbeds(eb2.build()).queue();
e.deferReply().queue(); return;
String opt3 = e.getOption("setting_name").getAsString(); case "clear":
String response1 = server.resetOptionByString(opt3); e.deferReply().queue();
EmbedBuilder eb3 = new EmbedBuilder() String opt3 = e.getOption("name").getAsString();
.setColor(EmbedUI.SUCCESS) String response1 = server.resetOptionByString(opt3);
.setTitle(opt3) EmbedBuilder eb3 = new EmbedBuilder()
.setDescription(response1) .setColor(EmbedUI.SUCCESS)
.setFooter(EmbedUI.BRAND) .setTitle(opt3)
.setTimestamp(ZonedDateTime.now()); .setDescription(response1)
e.getHook().sendMessageEmbeds(eb3.build()).queue(); .setFooter(EmbedUI.BRAND)
return; .setTimestamp(ZonedDateTime.now());
e.getHook().sendMessageEmbeds(eb3.build()).queue();
return;
}
} }
} }
} }
@ -98,24 +102,27 @@ public class SettingCommand extends CommandClass {
@Override @Override
public List<CommandInfo> getSlashCommandInfo() { public List<CommandInfo> getSlashCommandInfo() {
List<CommandInfo> si = new ArrayList<>(); List<CommandInfo> si = new ArrayList<>();
CommandInfo ci = new CommandInfo("setting", "displays a specific setting for the current guild");
si.add(ci);
CommandInfo ci2 = new CommandInfo("setting", "views the current value for the setting"); CommandInfo ci2 = new CommandInfo("setting", "Permits modification, viewing, and clearing of settings.", CommandType.COMMAND);
ci2.addOption("setting_name", "The name of the setting to view", OptionType.STRING, true); ci2.addOption("setting_modify", "The type of modification you would like to make.", OptionType.SUB_COMMAND, true);
si.add(ci2);
CommandInfo ci3 = new CommandInfo("setting_set", "sets a setting for the guild you are in"); CommandInfo ci = new CommandInfo("view", "Shows the current value for the setting provided.", CommandType.SUBCOMMAND);
ci3.addOption("setting_name", "The name of the setting to modify", OptionType.STRING, true); ci.addOption("name", "The name of the setting to display", OptionType.STRING, true);
ci3.addOption("setting_value", "The value to set the setting to", OptionType.STRING, true); ci2.addSubcommand(ci);
si.add(ci3);
CommandInfo ci4 = new CommandInfo("setting_clear", "reverts a setting back to its default value"); CommandInfo ci3 = new CommandInfo("set", "sets a setting for the guild you are in", CommandType.SUBCOMMAND);
ci4.addOption("setting_name", "Name of the setting to clear", OptionType.STRING, true); ci3.addOption("name", "The name of the setting to modify", OptionType.STRING, true);
si.add(ci4); ci3.addOption("value", "The value to set the setting to", OptionType.STRING, true);
ci2.addSubcommand(ci3);
CommandInfo ci5 = new CommandInfo("settings", "displays all settings available for the guild."); CommandInfo ci4 = new CommandInfo("clear", "reverts a setting back to its default value", CommandType.SUBCOMMAND);
ci4.addOption("name", "Name of the setting to clear", OptionType.STRING, true);
ci2.addSubcommand(ci4);
CommandInfo ci5 = new CommandInfo("settings", "displays all settings available for the guild.", CommandType.COMMAND);
si.add(ci5); si.add(ci5);
si.add(ci2);
return si; return si;
} }
} }

View File

@ -4,24 +4,29 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
import java.util.HashMap; import java.util.HashMap;
import static moe.oko.Kiafumi.Kiafumi.error;
/** /**
* Helpful CommandInfo class to easily make slash commands. * Helpful CommandInfo class to easily make slash commands.
*/ */
public class CommandInfo { public class CommandInfo {
private String name; private String name;
private String description; private String description;
private CommandType type;
private HashMap<String, OptionType> options; private HashMap<String, OptionType> options;
private HashMap<String, String> optionDescriptions; private HashMap<String, String> optionDescriptions;
private HashMap<String, Boolean> optionRequirements; private HashMap<String, Boolean> optionRequirements;
private HashMap<String, CommandInfo> subCommands;
/** /**
* Constructor to build CommandInfo with. * Constructor to build CommandInfo with.
* @param name - Name of the slash command (MUST BE ALL LOWERCASE) * @param name - Name of the slash command (MUST BE ALL LOWERCASE)
* @param description - Description of the slash command * @param description - Description of the slash command
*/ */
public CommandInfo(String name, String description) { public CommandInfo(String name, String description, CommandType type) {
this.name = name; this.name = name;
this.description = description; this.description = description;
this.type = type;
this.options = new HashMap<>(); this.options = new HashMap<>();
this.optionDescriptions = new HashMap<>(); this.optionDescriptions = new HashMap<>();
this.optionRequirements = new HashMap<>(); this.optionRequirements = new HashMap<>();
@ -55,6 +60,8 @@ public class CommandInfo {
return optionDescriptions; return optionDescriptions;
} }
public HashMap<String, CommandInfo> getSubCommands() { return subCommands; }
/** /**
* The way you add options to a command. Use this function for EACH argument. * The way you add options to a command. Use this function for EACH argument.
* @param name - name of the field * @param name - name of the field
@ -63,8 +70,16 @@ public class CommandInfo {
* @param required - whether the command can be run without the field or not. * @param required - whether the command can be run without the field or not.
*/ */
public void addOption(String name, String description, OptionType type, boolean required) { public void addOption(String name, String description, OptionType type, boolean required) {
if(this.type.equals(CommandType.SUBCOMMAND) && (type.equals(OptionType.SUB_COMMAND) || type.equals(OptionType.SUB_COMMAND_GROUP))) {
error("Command " + this.name + " attempted to assign a SUB_COMMAND option despite it being a subcommand itself. Check code for this command.");
return; //You cannot add a subcommand option to a subcommand.
}
options.put(name, type); options.put(name, type);
optionDescriptions.put(name, description); optionDescriptions.put(name, description);
optionRequirements.put(name, required); optionRequirements.put(name, required);
} }
public void addSubcommand(CommandInfo cmdInfo) {
subCommands.put(cmdInfo.name, cmdInfo);
}
} }

View File

@ -0,0 +1,14 @@
package moe.oko.Kiafumi.util;
public enum CommandType {
/**
* Commands that are registered using the upsertCommand method.
*/
COMMAND,
/**
* Commands that fall under above commands using the addSubcommand function.
*/
SUBCOMMAND
}