kiafumi/src/main/java/moe/oko/Kiafumi/command/utility/SettingCommand.java

137 lines
6.7 KiB
Java
Raw Normal View History

package moe.oko.Kiafumi.command.utility;
2022-03-28 15:56:45 +00:00
import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.command.CommandClass;
2022-03-28 15:56:45 +00:00
import moe.oko.Kiafumi.model.Server;
import moe.oko.Kiafumi.util.CommandInfo;
import moe.oko.Kiafumi.util.CommandType;
import moe.oko.Kiafumi.util.EmbedUI;
2022-03-28 15:56:45 +00:00
import net.dv8tion.jda.api.EmbedBuilder;
2022-03-29 20:52:51 +00:00
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
2022-03-28 15:56:45 +00:00
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
import static moe.oko.Kiafumi.util.LoggingManager.slashResponse;
2022-04-04 07:27:20 +00:00
/**
* Permits modification of server settings, critical class to functionality.
* @author Kay
*/
public class SettingCommand extends CommandClass {
@Override
public boolean isEnabled() {
return true; // Another non-disable command
}
@Override
public String getName() {
return "Settings";
}
@Override
public void newCommand(String name, SlashCommandInteractionEvent e) {
if(e.getGuild() != null) {
2022-03-28 15:56:45 +00:00
Server server = Kiafumi.instance.getServerManager().getOrCreateServer(e.getGuild());
switch (name) {
2022-04-03 08:33:18 +00:00
case "settings" -> {
2022-04-04 07:27:20 +00:00
slashLog(e);
2022-03-28 15:56:45 +00:00
e.deferReply().queue();
// No options, just fire an embed off...
2022-03-28 15:56:45 +00:00
EmbedBuilder eb = new EmbedBuilder()
.setColor(EmbedUI.INFO)
.setTitle("Kiafumi Settings")
.setDescription(server.getOpts())
.setFooter(EmbedUI.BRAND)
.setTimestamp(ZonedDateTime.now());
2022-03-28 15:56:45 +00:00
e.getHook().sendMessageEmbeds(eb.build()).queue();
2022-04-03 08:33:18 +00:00
}
case "setting" -> {
// User is attempting a settings modification. Check if admin.
2022-04-04 07:27:20 +00:00
if(!e.getMember().hasPermission(Permission.ADMINISTRATOR) && !e.getMember().isOwner()) {
slashLog(e, EmbedUI.RESPONSE_PRIVILEGES);
2022-03-29 20:52:51 +00:00
e.deferReply(true).queue();
// Private reply, other people can't see this if ephemeral.
e.getHook().sendMessage("**You cannot run this command.**").queue();
2022-03-29 20:59:41 +00:00
return;
2022-03-29 20:52:51 +00:00
}
2022-04-03 08:33:18 +00:00
switch (e.getSubcommandName().toLowerCase()) {
case "view" -> {
e.deferReply().queue();
String opt = e.getOption("name").getAsString();
slashLog(e, "with subcommand \"" + e.getSubcommandName().toLowerCase() + "\" for setting \"" + opt + "\".");
EmbedBuilder eb1 = new EmbedBuilder()
2022-04-03 08:33:18 +00:00
.setColor(EmbedUI.INFO)
.setTitle(opt)
.setDescription("Value: `" + server.getOptionByString(opt) + '`')
.setFooter(EmbedUI.BRAND)
.setTimestamp(ZonedDateTime.now());
e.getHook().sendMessageEmbeds(eb1.build()).queue();
2022-04-03 08:33:18 +00:00
}
case "set" -> {
e.deferReply().queue();
String opt1 = e.getOption("name").getAsString();
String opt2 = e.getOption("value").getAsString();
slashLog(e, "with subcommand \"" + e.getSubcommandName().toLowerCase() + "\" for setting \"" + opt1 + "\" to \"" + opt2 + "\".");
String response = server.setOptionByString(opt1, opt2);
2022-04-04 07:27:20 +00:00
slashResponse(e, response);
EmbedBuilder eb2 = new EmbedBuilder()
.setColor(EmbedUI.SUCCESS)
.setTitle(opt1)
.setDescription(response)
.setFooter(EmbedUI.BRAND)
.setTimestamp(ZonedDateTime.now());
e.getHook().sendMessageEmbeds(eb2.build()).queue();
2022-04-03 08:33:18 +00:00
}
case "clear" -> {
e.deferReply().queue();
String opt3 = e.getOption("name").getAsString();
slashLog(e, "with subcommand \"" + e.getSubcommandName().toLowerCase() + "\" for setting \"" + opt3 + "\".");
String response1 = server.resetOptionByString(opt3);
2022-04-04 07:27:20 +00:00
slashResponse(e, response1);
EmbedBuilder eb3 = new EmbedBuilder()
.setColor(EmbedUI.SUCCESS)
.setTitle(opt3)
.setDescription(response1)
.setFooter(EmbedUI.BRAND)
.setTimestamp(ZonedDateTime.now());
e.getHook().sendMessageEmbeds(eb3.build()).queue();
2022-04-03 08:33:18 +00:00
}
}
2022-04-03 08:33:18 +00:00
}
2022-03-28 15:56:45 +00:00
}
}
}
@Override
public List<CommandInfo> getSlashCommandInfo() {
List<CommandInfo> si = new ArrayList<>();
CommandInfo ci2 = new CommandInfo("setting", "Permits modification, viewing, and clearing of settings.", CommandType.COMMAND);
// For those looking here for inspiration, you CANNOT mix options and subcommands. You can only have one or the other.
CommandInfo ci = new CommandInfo("view", "Shows the current value for the setting provided.", CommandType.SUBCOMMAND);
ci.addOption("name", "The name of the setting to display", OptionType.STRING, true);
ci2.addSubcommand(ci);
CommandInfo ci3 = new CommandInfo("set", "sets a setting for the guild you are in", CommandType.SUBCOMMAND);
ci3.addOption("name", "The name of the setting to modify", OptionType.STRING, true);
ci3.addOption("value", "The value to set the setting to", OptionType.STRING, true);
ci2.addSubcommand(ci3);
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);
2022-03-28 19:38:25 +00:00
CommandInfo ci5 = new CommandInfo("settings", "displays all settings available for the guild.", CommandType.COMMAND);
2022-03-28 19:38:25 +00:00
si.add(ci5);
si.add(ci2);
return si;
}
}