diff --git a/build.gradle b/build.gradle index be591cc80..b6724dd2c 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ dependencies { testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' implementation 'net.dv8tion:JDA:4.3.0_309' +// implementation 'com.github.dv8fromtheworld:jda:feature~threads' implementation 'org.mongodb:mongodb-driver:3.12.10' implementation 'com.google.code.gson:gson:2.8.7' implementation 'org.yaml:snakeyaml:1.29' diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/Config.java b/src/main/java/com/javadiscord/javabot/commands/configuation/Config.java new file mode 100644 index 000000000..b94c66389 --- /dev/null +++ b/src/main/java/com/javadiscord/javabot/commands/configuation/Config.java @@ -0,0 +1,16 @@ +package com.javadiscord.javabot.commands.configuation; + +import com.javadiscord.javabot.commands.DelegatingCommandHandler; + +/** + * The main command for interacting with the bot's configuration at runtime via + * slash commands. + */ +public class Config extends DelegatingCommandHandler { + public Config() { + addSubcommand("list", new ListSubcommand()); + addSubcommand("get", new GetSubcommand()); + addSubcommand("set", new SetSubcommand()); + } +} + diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/GetSubcommand.java b/src/main/java/com/javadiscord/javabot/commands/configuation/GetSubcommand.java new file mode 100644 index 000000000..b72c1ede0 --- /dev/null +++ b/src/main/java/com/javadiscord/javabot/commands/configuation/GetSubcommand.java @@ -0,0 +1,25 @@ +package com.javadiscord.javabot.commands.configuation; + +import com.javadiscord.javabot.Bot; +import com.javadiscord.javabot.commands.Responses; +import com.javadiscord.javabot.commands.SlashCommandHandler; +import com.javadiscord.javabot.properties.config.UnknownPropertyException; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; + +public class GetSubcommand implements SlashCommandHandler { + @Override + public ReplyAction handle(SlashCommandEvent event) { + var propertyOption = event.getOption("property"); + if (propertyOption == null) { + return Responses.warning(event, "Missing required property argument."); + } + String property = propertyOption.getAsString().trim(); + try { + Object value = Bot.config.get(event.getGuild()).resolve(property); + return Responses.info(event, "Configuration Property", String.format("The value of the property `%s` is `%s`.", property, value)); + } catch (UnknownPropertyException e) { + return Responses.warning(event, "Unknown Property", "The property `" + property + "` could not be found."); + } + } +} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/ListSubcommand.java b/src/main/java/com/javadiscord/javabot/commands/configuation/ListSubcommand.java new file mode 100644 index 000000000..96b795d9c --- /dev/null +++ b/src/main/java/com/javadiscord/javabot/commands/configuation/ListSubcommand.java @@ -0,0 +1,47 @@ +package com.javadiscord.javabot.commands.configuation; + +import com.javadiscord.javabot.Bot; +import com.javadiscord.javabot.commands.Responses; +import com.javadiscord.javabot.commands.SlashCommandHandler; +import com.javadiscord.javabot.properties.config.GuildConfig; +import com.javadiscord.javabot.properties.config.ReflectionUtils; +import com.javadiscord.javabot.properties.config.UnknownPropertyException; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; + +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Shows a list of all known configuration properties, their type, and their + * current value. + */ +public class ListSubcommand implements SlashCommandHandler { + @Override + public ReplyAction handle(SlashCommandEvent event) { + try { + var results = ReflectionUtils.getFields(null, GuildConfig.class); + String msg = results.entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .map(entry -> { + Object propertyValue = null; + try { + propertyValue = Bot.config.get(event.getGuild()).resolve(entry.getKey()); + } catch (UnknownPropertyException e) { + e.printStackTrace(); + } + return String.format( + "**%s** `%s` = `%s`", + entry.getValue().getSimpleName(), + entry.getKey(), + propertyValue + ); + }) + .collect(Collectors.joining("\n")); + return Responses.info(event, "Configuration Properties", msg); + } catch (IllegalAccessException e) { + e.printStackTrace(); + return Responses.error(event, e.getMessage()); + } + } +} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/SetSubcommand.java b/src/main/java/com/javadiscord/javabot/commands/configuation/SetSubcommand.java new file mode 100644 index 000000000..8c8fccebf --- /dev/null +++ b/src/main/java/com/javadiscord/javabot/commands/configuation/SetSubcommand.java @@ -0,0 +1,27 @@ +package com.javadiscord.javabot.commands.configuation; + +import com.javadiscord.javabot.Bot; +import com.javadiscord.javabot.commands.Responses; +import com.javadiscord.javabot.commands.SlashCommandHandler; +import com.javadiscord.javabot.properties.config.UnknownPropertyException; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; + +public class SetSubcommand implements SlashCommandHandler { + @Override + public ReplyAction handle(SlashCommandEvent event) { + var propertyOption = event.getOption("property"); + var valueOption = event.getOption("value"); + if (propertyOption == null || valueOption == null) { + return Responses.warning(event, "Missing required arguments."); + } + String property = propertyOption.getAsString().trim(); + String valueString = valueOption.getAsString().trim(); + try { + Bot.config.get(event.getGuild()).set(property, valueString); + return Responses.success(event, "Configuration Updated", String.format("The property `%s` has been set to `%s`.", property, valueString)); + } catch (UnknownPropertyException e) { + return Responses.warning(event, "Unknown Property", "The property `" + property + "` could not be found."); + } + } +} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/Config.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/Config.java deleted file mode 100644 index 369a9f627..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/Config.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config; - -import com.javadiscord.javabot.commands.DelegatingCommandHandler; -import com.javadiscord.javabot.commands.Responses; -import com.javadiscord.javabot.commands.configuation.config.subcommands.*; -import com.javadiscord.javabot.other.Constants; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.MessageEmbed; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -import java.util.Date; - -// TODO: Replace with file-based config or at least something much less convoluted. -@Deprecated -public class Config extends DelegatingCommandHandler { - public Config() { - addSubcommand("list", new GetList()); - addSubcommand("stats-category", new SetStatsCategory()); - addSubcommand("stats-message", new SetStatsMessage()); - addSubcommand("report-channel", new SetReportChannel()); - addSubcommand("starboard-channel", new SetStarboardChannel()); - addSubcommand("log-channel", new SetLogChannel()); - addSubcommand("suggestion-channel", new SetSuggestionChannel()); - addSubcommand("submission-channel", new SetSubmissionChannel()); - addSubcommand("mute-role", new SetMuteRole()); - addSubcommand("staff-role", new SetStaffRole()); - addSubcommand("dm-qotw", new SetDMQOTWStatus()); - addSubcommand("lock", new SetLockStatus()); - - addSubcommand("jam-admin-role", new SetJamAdminRole()); - addSubcommand("jam-ping-role", new SetJamPingRole()); - addSubcommand("jam-vote-channel", new SetJamVoteChannel()); - addSubcommand("jam-announcement-channel", new SetJamAnnouncementChannel()); - } - - @Override - public ReplyAction handle(SlashCommandEvent event) { - - return Responses.error(event, "This command is currently not available."); - - //try { return super.handle(event); - //} catch (Exception e) { return Responses.error(event, "```" + e.getMessage() + "```"); } - } - - public MessageEmbed configEmbed (String configName, String newValue) { - var embed = new EmbedBuilder() - .setColor(Constants.GRAY) - .setAuthor("Config: " + configName) - .setDescription("Successfully set ``" + configName + "`` to " + newValue) - .setTimestamp(new Date().toInstant()) - .build(); - return embed; - } -} - diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/GetList.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/GetList.java deleted file mode 100644 index 065376026..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/GetList.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.Bot; -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.other.Constants; -import com.javadiscord.javabot.other.Database; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class GetList implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - - Database db = new Database(); - - var c = Bot.config.get(event.getGuild()); - var eb = new EmbedBuilder() - .setColor(Constants.GRAY) - .setTitle("Bot Configuration") - - //.addField("Lock Status", "Locked: `" + db.getConfigBoolean(event.getGuild(), "other.server_lock.lock_status") + "`" + - // "\nCount: `" + db.getConfigInt(event.getGuild(), "other.server_lock.lock_count") + "/5`", true) - - .addField("Question of the Week", "Submission Channel: " + c.getQotw().getSubmissionChannel().getAsMention() - + "\nSubmission-Status: `" + c.getQotw().isDmEnabled() + "`", true) - - .addField("Stats-Category", "Category-ID: `" + c.getStats().getCategoryId() + "`" + - "\nText: `" + c.getStats().getMemberCountMessageTemplate() + "`", false) - - .addField("Channel & Roles", "Report Channel: " + c.getModeration().getReportChannel().getAsMention() + - ", Log Channel: " + c.getModeration().getLogChannel().getAsMention() + - "\nSuggestion Channel: " + c.getModeration().getSuggestionChannel().getAsMention() + - ", Starboard Channel: " + c.getStarBoard().getChannel().getAsMention() + - "\nJam Announcement Channel: " + c.getJam().getAnnouncementChannel().getAsMention() + - ", Jam Vote Channel: " + c.getJam().getVotingChannel().getAsMention() + - "\n\nMute Role: " + c.getModeration().getMuteRole().getAsMention() + - ", Staff Role: " + c.getModeration().getStaffRole().getAsMention() + - ", Jam-Admin Role: " + c.getJam().getAdminRole().getAsMention() + - ", Jam-Ping Role: " + c.getJam().getPingRole().getAsMention(), false) - .build(); - - return event.replyEmbeds(eb); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetDMQOTWStatus.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetDMQOTWStatus.java deleted file mode 100644 index df2115f1d..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetDMQOTWStatus.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetDMQOTWStatus implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - boolean status = event.getOption("enabled").getAsBoolean(); - //new Database().queryConfig(event.getGuild().getId(), "other.qotw.dm-qotw", status); - return event.replyEmbeds(new Config().configEmbed( - "DM-QOTW Status", - "`" + status + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamAdminRole.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamAdminRole.java deleted file mode 100644 index 628fdabd1..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamAdminRole.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetJamAdminRole implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - Role role = event.getOption("role").getAsRole(); - //new Database().queryConfig(event.getGuild().getId(), "roles.jam_admin_rid", role.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Jam Admin Role", - role.getAsMention() - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamAnnouncementChannel.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamAnnouncementChannel.java deleted file mode 100644 index d54ddca99..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamAnnouncementChannel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetJamAnnouncementChannel implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - MessageChannel channel = event.getOption("channel").getAsMessageChannel(); - //new Database().queryConfig(event.getGuild().getId(), "channels.jam_announcement_cid", channel.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Jam Announcement Channel", - "<#" + channel.getId() + ">" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamPingRole.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamPingRole.java deleted file mode 100644 index cd84739fc..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamPingRole.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetJamPingRole implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - Role role = event.getOption("role").getAsRole(); - //new Database().queryConfig(event.getGuild().getId(), "roles.jam_ping_rid", role.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Jam Ping Role", - role.getAsMention() - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamVoteChannel.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamVoteChannel.java deleted file mode 100644 index b8bd04bf3..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamVoteChannel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetJamVoteChannel implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - MessageChannel channel = event.getOption("channel").getAsMessageChannel(); - //new Database().queryConfig(event.getGuild().getId(), "channels.jam_vote_cid", channel.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Jam Vote Channel", - "<#" + channel.getId() + ">" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetLockStatus.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetLockStatus.java deleted file mode 100644 index add0e8786..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetLockStatus.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetLockStatus implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - boolean status = event.getOption("locked").getAsBoolean(); - //new Database().queryConfig(event.getGuild().getId(), "other.server_lock.lock_status", status); - return event.replyEmbeds(new Config().configEmbed( - "Lock Status", - "`" + status + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetLogChannel.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetLogChannel.java deleted file mode 100644 index 96c035fe8..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetLogChannel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetLogChannel implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - MessageChannel channel = event.getOption("channel").getAsMessageChannel(); - //new Database().queryConfig(event.getGuild().getId(), "channels.log_cid", channel.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Log Channel", - "<#" + channel.getId() + ">" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetMuteRole.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetMuteRole.java deleted file mode 100644 index b2744b3cf..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetMuteRole.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetMuteRole implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - Role role = event.getOption("role").getAsRole(); - //new Database().queryConfig(event.getGuild().getId(), "roles.mute_rid", role.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Mute Role", - role.getAsMention() - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetReportChannel.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetReportChannel.java deleted file mode 100644 index 2ab7fa06c..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetReportChannel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetReportChannel implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - MessageChannel channel = event.getOption("channel").getAsMessageChannel(); - //new Database().queryConfig(event.getGuild().getId(), "channels.report_cid", channel.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Report Channel", - "<#" + channel.getId() + ">" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStaffRole.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStaffRole.java deleted file mode 100644 index 2bd489246..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStaffRole.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetStaffRole implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - Role role = event.getOption("role").getAsRole(); - //new Database().queryConfig(event.getGuild().getId(), "roles.staff_rid", role.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Staff Role", - role.getAsMention() - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStarboardChannel.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStarboardChannel.java deleted file mode 100644 index 592051004..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStarboardChannel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetStarboardChannel implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - MessageChannel channel = event.getOption("channel").getAsMessageChannel(); - //new Database().queryConfig(event.getGuild().getId(), "other.starboard.starboard_cid", channel.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Starboard Channel", - "<#" + channel.getId() + ">" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStatsCategory.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStatsCategory.java deleted file mode 100644 index 9024c013b..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStatsCategory.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetStatsCategory implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - String id = event.getOption("id").getAsString(); - //new Database().queryConfig(event.getGuild().getId(), "other.stats_category.stats_cid", id); - return event.replyEmbeds(new Config().configEmbed( - "Stats Category ID", - "`" + id + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStatsMessage.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStatsMessage.java deleted file mode 100644 index f509d4cf3..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetStatsMessage.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetStatsMessage implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - String message = event.getOption("message").getAsString(); - //new Database().queryConfig(event.getGuild().getId(), "other.stats_category.stats_text", message); - return event.replyEmbeds(new Config().configEmbed( - "Stats Category Message", - "`" + message + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetSubmissionChannel.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetSubmissionChannel.java deleted file mode 100644 index 9c0f6bc53..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetSubmissionChannel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetSubmissionChannel implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - MessageChannel channel = event.getOption("channel").getAsMessageChannel(); - //new Database().queryConfig(event.getGuild().getId(), "channels.submission_cid", channel.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Submission Channel", - "<#" + channel.getId() + ">" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetSuggestionChannel.java b/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetSuggestionChannel.java deleted file mode 100644 index 245eedfb7..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetSuggestionChannel.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.config.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetSuggestionChannel implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - MessageChannel channel = event.getOption("channel").getAsMessageChannel(); - //new Database().queryConfig(event.getGuild().getId(), "channels.suggestion_cid", channel.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Suggestion Channel", - "<#" + channel.getId() + ">" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/WelcomeSystem.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/WelcomeSystem.java deleted file mode 100644 index 8c86c33cf..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/WelcomeSystem.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system; - -import com.javadiscord.javabot.commands.DelegatingCommandHandler; -import com.javadiscord.javabot.commands.Responses; -import com.javadiscord.javabot.commands.configuation.welcome_system.subcommands.*; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - - -public class WelcomeSystem extends DelegatingCommandHandler { - public WelcomeSystem() { - addSubcommand("list", new GetList()); - addSubcommand("leave-msg", new SetLeaveMessage()); - addSubcommand("join-msg", new SetJoinMessage()); - addSubcommand("channel",new SetWelcomeChannel()); - addSubcommand("image-width", new SetImageWidth()); - addSubcommand("image-height", new SetImageHeight()); - addSubcommand("overlay-url", new SetOverlayUrl()); - addSubcommand("background-url", new SetBackgroundUrl()); - addSubcommand("primary-color", new SetPrimaryColor()); - addSubcommand("secondary-color", new SetSecondaryColor()); - addSubcommand("avatar-height", new SetAvatarHeight()); - addSubcommand("avatar-width", new SetAvatarWidth()); - addSubcommand("avatar-x", new SetAvatarX()); - addSubcommand("avatar-y", new SetAvatarY()); - addSubcommand("status", new SetStatus()); - } - - @Override - public ReplyAction handle(SlashCommandEvent event) { - - try { return super.handle(event); - } catch (Exception e) { return Responses.error(event, "```" + e.getMessage() + "```"); } - } -} - diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/GetList.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/GetList.java deleted file mode 100644 index 136a2e6e8..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/GetList.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.Bot; -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.events.UserJoin; -import com.javadiscord.javabot.other.Constants; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -import java.io.ByteArrayInputStream; - -public class GetList implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - Bot.asyncPool.submit(() -> this.send(event)); - return event.deferReply(); - } - - private void send(SlashCommandEvent event) { - var config = Bot.config.get(event.getGuild()).getWelcome(); - String status = config.isEnabled() ? "enabled" : "disabled"; - - var eb = new EmbedBuilder() - .setTitle("Welcome System Configuration") - .setColor(Constants.GRAY) - - .addField("Image", "Width, Height: `" + config.getImageConfig().getWidth() + - "`, `" + config.getImageConfig().getHeight() + - "`\n[Overlay](" + config.getImageConfig().getOverlayImageUrl() + - "), [Background](" + config.getImageConfig().getBackgroundImageUrl() + ")", false) - - .addField("Color", "Primary Color: `#" + Integer.toHexString(config.getImageConfig().getPrimaryColor()) + - "`\nSecondary Color: `#" + Integer.toHexString(config.getImageConfig().getSecondaryColor()) + "`", true) - - .addField("Avatar Image", "Width, Height: `" + config.getImageConfig().getAvatarConfig().getWidth() + - "`,`" + config.getImageConfig().getAvatarConfig().getHeight() + - "`\nX, Y: `" + config.getImageConfig().getAvatarConfig().getX() + - "`, `" + config.getImageConfig().getAvatarConfig().getY() + "`", true) - - .addField("Messages", "Join: `" + config.getJoinMessageTemplate() + - "`\nLeave: `" + config.getLeaveMessageTemplate() + "`", false) - - .addField("Channel", config.getChannel().getAsMention(), true) - .addField("Status", "``" + status + "``", true); - - try { - event.getHook().sendMessageEmbeds(eb.build()) - .addFile( - new ByteArrayInputStream( - new UserJoin().generateImage(event.getGuild(), event.getMember())), - event.getMember().getId() + ".png") - .queue(); - } catch (Exception e) { e.printStackTrace(); } - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarHeight.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarHeight.java deleted file mode 100644 index 04e844823..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarHeight.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetAvatarHeight implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - int height = (int) event.getOption("height").getAsLong(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.avatar.avH", height); - return event.replyEmbeds(new Config().configEmbed( - "Avatar Image Height", - "`" + height + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarWidth.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarWidth.java deleted file mode 100644 index f7b285a67..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarWidth.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetAvatarWidth implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - int width = (int) event.getOption("width").getAsLong(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.avatar.avW", width); - return event.replyEmbeds(new Config().configEmbed( - "Avatar Image Width", - "`" + width + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarX.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarX.java deleted file mode 100644 index b979a5206..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarX.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetAvatarX implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - int x = (int) event.getOption("x").getAsLong(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.avatar.avX", x); - return event.replyEmbeds(new Config().configEmbed( - "Avatar Image X-Pos", - "`" + x + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarY.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarY.java deleted file mode 100644 index 183946d14..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetAvatarY.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetAvatarY implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - int y = (int) event.getOption("y").getAsLong(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.avatar.avY", y); - return event.replyEmbeds(new Config().configEmbed( - "Avatar Image Y-Pos", - "`" + y + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetBackgroundUrl.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetBackgroundUrl.java deleted file mode 100644 index 535108cc5..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetBackgroundUrl.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.Responses; -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import com.javadiscord.javabot.other.Misc; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetBackgroundUrl implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - String url = event.getOption("url").getAsString(); - if (Misc.isImage(url)) return event.replyEmbeds(new Config().configEmbed( - "Welcome Image Background URL", - "`" + url + "`" - )); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.bgURL", url); - else return Responses.error(event, "```URL must be a valid HTTP(S) or Attachment URL.```"); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetImageHeight.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetImageHeight.java deleted file mode 100644 index 11a16525b..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetImageHeight.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetImageHeight implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - int height = (int) event.getOption("height").getAsLong(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.imgH", height); - return event.replyEmbeds(new Config().configEmbed( - "Image Height", - "`" + height + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetImageWidth.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetImageWidth.java deleted file mode 100644 index ca68b1fbd..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetImageWidth.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetImageWidth implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - int width = (int) event.getOption("width").getAsLong(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.imgW", width); - return event.replyEmbeds(new Config().configEmbed( - "Image Width", - "`" + width + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetJoinMessage.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetJoinMessage.java deleted file mode 100644 index 7eb1549df..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetJoinMessage.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetJoinMessage implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - String message = event.getOption("message").getAsString(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.join_msg", message); - return event.replyEmbeds(new Config().configEmbed( - "Join Message", - "`" + message + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetLeaveMessage.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetLeaveMessage.java deleted file mode 100644 index 0a7645cf1..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetLeaveMessage.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetLeaveMessage implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - String message = event.getOption("message").getAsString(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.leave_msg", message); - return event.replyEmbeds(new Config().configEmbed( - "Leave Message", - "`" + message + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetOverlayUrl.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetOverlayUrl.java deleted file mode 100644 index 5ed09b2d9..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetOverlayUrl.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.Responses; -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import com.javadiscord.javabot.other.Misc; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetOverlayUrl implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - String url = event.getOption("url").getAsString(); - if (Misc.isImage(url)) return event.replyEmbeds(new Config().configEmbed( - "Welcome Image Overlay URL", - "`" + url + "`" - )); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.overlayURL", url); - else return Responses.error(event, "```URL must be a valid HTTP(S) or Attachment URL.```"); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetPrimaryColor.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetPrimaryColor.java deleted file mode 100644 index 2f67279a1..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetPrimaryColor.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetPrimaryColor implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - String color = event.getOption("color").getAsString(); - color = color.replace("#", ""); - long l = Long.parseLong(color, 16); - int i = (int) l; - - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.primCol", i); - return event.replyEmbeds(new Config().configEmbed( - "Primary Color", - "`#" + Integer.toHexString(i) + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetSecondaryColor.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetSecondaryColor.java deleted file mode 100644 index c51191a5c..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetSecondaryColor.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetSecondaryColor implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - String color = event.getOption("color").getAsString(); - color = color.replace("#", ""); - long l = Long.parseLong(color, 16); - int i = (int) l; - - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.image.secCol", i); - return event.replyEmbeds(new Config().configEmbed( - "Secondary Color", - "`#" + Integer.toHexString(i) + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetStatus.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetStatus.java deleted file mode 100644 index 1cad53529..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetStatus.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetStatus implements SlashCommandHandler { - - @Override - public ReplyAction handle(SlashCommandEvent event) { - boolean status = event.getOption("status").getAsBoolean(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.welcome_status", status); - return event.replyEmbeds(new Config().configEmbed( - "Welcome System Status", - "`" + status + "`" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetWelcomeChannel.java b/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetWelcomeChannel.java deleted file mode 100644 index aaa83f438..000000000 --- a/src/main/java/com/javadiscord/javabot/commands/configuation/welcome_system/subcommands/SetWelcomeChannel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadiscord.javabot.commands.configuation.welcome_system.subcommands; - -import com.javadiscord.javabot.commands.SlashCommandHandler; -import com.javadiscord.javabot.commands.configuation.config.Config; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; - -public class SetWelcomeChannel implements SlashCommandHandler { - @Override - public ReplyAction handle(SlashCommandEvent event) { - MessageChannel channel = event.getOption("channel").getAsMessageChannel(); - //new Database().queryConfig(event.getGuild().getId(), "welcome_system.welcome_cid", channel.getId()); - return event.replyEmbeds(new Config().configEmbed( - "Welcome Channel", - "<#" + channel.getId() + ">" - )); - } -} diff --git a/src/main/java/com/javadiscord/javabot/properties/command/CommandPrivilegeConfig.java b/src/main/java/com/javadiscord/javabot/properties/command/CommandPrivilegeConfig.java index 7c2e77ff3..92b86c753 100644 --- a/src/main/java/com/javadiscord/javabot/properties/command/CommandPrivilegeConfig.java +++ b/src/main/java/com/javadiscord/javabot/properties/command/CommandPrivilegeConfig.java @@ -1,7 +1,9 @@ package com.javadiscord.javabot.properties.command; import com.javadiscord.javabot.properties.config.BotConfig; +import com.javadiscord.javabot.properties.config.UnknownPropertyException; import lombok.Data; +import lombok.extern.slf4j.Slf4j; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege; @@ -9,6 +11,7 @@ import java.util.concurrent.CompletableFuture; @Data +@Slf4j public class CommandPrivilegeConfig { private String type; private boolean enabled = true; @@ -22,7 +25,12 @@ public CompletableFuture toData(Guild guild, BotConfig botConf return CompletableFuture.completedFuture(new CommandPrivilege(CommandPrivilege.Type.USER, this.enabled, user.getIdLong())); }); } else if (this.type.equalsIgnoreCase(CommandPrivilege.Type.ROLE.name())) { - Long roleId = botConfig.get(guild).resolve(this.id); + Long roleId = null; + try { + roleId = (Long) botConfig.get(guild).resolve(this.id); + } catch (UnknownPropertyException e) { + log.error("Unknown property while resolving role id.", e); + } if (roleId == null) return CompletableFuture.failedFuture(new IllegalArgumentException("Missing role id.")); Role role = guild.getRoleById(roleId); if (role == null) return CompletableFuture.failedFuture(new IllegalArgumentException("Role could not be found for id " + roleId)); diff --git a/src/main/java/com/javadiscord/javabot/properties/config/GuildConfig.java b/src/main/java/com/javadiscord/javabot/properties/config/GuildConfig.java index 057f776a5..085fb324d 100644 --- a/src/main/java/com/javadiscord/javabot/properties/config/GuildConfig.java +++ b/src/main/java/com/javadiscord/javabot/properties/config/GuildConfig.java @@ -11,10 +11,8 @@ import javax.annotation.Nullable; import java.io.IOException; import java.io.UncheckedIOException; -import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; /** * A collection of guild-specific configuration items, each of which represents @@ -112,43 +110,36 @@ public synchronized void flush() { * of {@link GuildConfig} followed by the pingRoleId field from * {@link JamConfig}. * @param propertyName The name of the property. - * @param The type of the property. * @return The value of the property, if found, or null otherwise. */ @Nullable - public T resolve(String propertyName) { - return this.resolveRecursive(propertyName.split("\\."), this); + public Object resolve(String propertyName) throws UnknownPropertyException { + var result = ReflectionUtils.resolveField(propertyName, this); + return result.map(pair -> { + try { + return pair.getFirst().get(pair.getSecond()); + } catch (IllegalAccessException e) { + e.printStackTrace(); + return null; + } + }).orElse(null); } /** - * Recursively resolves a property based on a sequential array of field - * names to traverse. If there's only one field name present, we will try - * to find the field in the given parent object and return its value. If - * there are more field names, however, get the value of the first field - * from the parent, and recursively check the fields of that value. - * @param fieldNames The array of field names. - * @param parent The object in which to look for root-level fields. - * @param The expected return type. - * @return The object which was resolved, or null otherwise. + * Attempts to set a configuration property's value by its name, using '.' + * to concatenate property names, similar to {@link GuildConfig#resolve(String)}. + * @param propertyName The name of the property to set. + * @param value The value to set. */ - @Nullable - @SuppressWarnings("unchecked") - private T resolveRecursive(String[] fieldNames, Object parent) { - if (fieldNames.length == 0) return null; - try { - Field field = parent.getClass().getDeclaredField(fieldNames[0]); - field.setAccessible(true); - Object value = field.get(parent); - if (fieldNames.length == 1) { - return (T) value; - } else if (value != null) { - return resolveRecursive(Arrays.copyOfRange(fieldNames, 1, fieldNames.length), value); - } else { - return null; + public void set(String propertyName, String value) throws UnknownPropertyException { + var result = ReflectionUtils.resolveField(propertyName, this); + result.ifPresent(pair -> { + try { + ReflectionUtils.set(pair.getFirst(), pair.getSecond(), value); + this.flush(); + } catch (IllegalAccessException e) { + e.printStackTrace(); } - } catch (NoSuchFieldException | IllegalAccessException e) { - log.warn("Reflection error occurred while resolving property " + Arrays.toString(fieldNames) + " of object of type " + parent.getClass().getSimpleName(), e); - return null; - } + }); } } diff --git a/src/main/java/com/javadiscord/javabot/properties/config/InvalidPropertyValueException.java b/src/main/java/com/javadiscord/javabot/properties/config/InvalidPropertyValueException.java new file mode 100644 index 000000000..2f61733f0 --- /dev/null +++ b/src/main/java/com/javadiscord/javabot/properties/config/InvalidPropertyValueException.java @@ -0,0 +1,4 @@ +package com.javadiscord.javabot.properties.config; + +public class InvalidPropertyValueException extends Exception { +} diff --git a/src/main/java/com/javadiscord/javabot/properties/config/ReflectionUtils.java b/src/main/java/com/javadiscord/javabot/properties/config/ReflectionUtils.java new file mode 100644 index 000000000..7e6dbe552 --- /dev/null +++ b/src/main/java/com/javadiscord/javabot/properties/config/ReflectionUtils.java @@ -0,0 +1,108 @@ +package com.javadiscord.javabot.properties.config; + +import com.javadiscord.javabot.other.Pair; +import lombok.extern.slf4j.Slf4j; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; + +@Slf4j +public class ReflectionUtils { + public static Optional> resolveField(String propertyName, Object parent) throws UnknownPropertyException { + return Optional.ofNullable(resolveField(propertyName.split("\\."), parent)); + } + + /** + * Finds a field and the object whose type contains it, for a given parent + * object. + * @param fieldNames An array containing an ordered list of the names of all + * fields to traverse. + * @param parent The object whose fields to traverse. + * @return The field and the object upon which it can be applied. + * @throws UnknownPropertyException If no field could be resolved. + */ + public static Pair resolveField(String[] fieldNames, Object parent) throws UnknownPropertyException { + if (fieldNames.length == 0) return null; + try { + Field field = parent.getClass().getDeclaredField(fieldNames[0]); + // Transient fields should not exist in the context of property resolution, treat them as unknown. + if (Modifier.isTransient(field.getModifiers())) throw new UnknownPropertyException(fieldNames[0], parent.getClass()); + field.setAccessible(true); + Object value = field.get(parent); + if (fieldNames.length == 1) { + return new Pair<>(field, parent); + } else if (value != null) { + return resolveField(Arrays.copyOfRange(fieldNames, 1, fieldNames.length), value); + } else { + return null; + } + } catch (NoSuchFieldException e) { + throw new UnknownPropertyException(fieldNames[0], parent.getClass()); + } catch (IllegalAccessException e) { + log.warn("Reflection error occurred while resolving property " + Arrays.toString(fieldNames) + " of object of type " + parent.getClass().getSimpleName(), e); + return null; + } + } + + /** + * Gets a mapping of properties and their type, recursively for the given + * type. + * @param parentPropertyName The root property name to append child field + * names to. This is null for the base case. + * @param parentClass The class to search for properties in. + * @return The map of properties and their types. + * @throws IllegalAccessException If a field cannot have its value obtained. + */ + public static Map> getFields(String parentPropertyName, Class parentClass) throws IllegalAccessException { + Map> fieldsMap = new HashMap<>(); + for (var field : parentClass.getDeclaredFields()) { + // Skip transient fields. + if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) continue; + field.setAccessible(true); + String fieldPropertyName = parentPropertyName == null ? field.getName() : parentPropertyName + "." + field.getName(); + // Check if the field represents a "leaf" property, one which does not have any children. + if (propertyTypeParsers.containsKey(field.getType())) { + fieldsMap.put(fieldPropertyName, field.getType()); + } else { + var childFieldsMap = getFields(fieldPropertyName, field.getType()); + fieldsMap.putAll(childFieldsMap); + } + } + return fieldsMap; + } + + private static final Map, Function> propertyTypeParsers = new HashMap<>(); + static { + propertyTypeParsers.put(Integer.class, Integer::parseInt); + propertyTypeParsers.put(int.class, Integer::parseInt); + propertyTypeParsers.put(Long.class, Long::parseLong); + propertyTypeParsers.put(long.class, Long::parseLong); + propertyTypeParsers.put(Float.class, Float::parseFloat); + propertyTypeParsers.put(float.class, Float::parseFloat); + propertyTypeParsers.put(Double.class, Double::parseDouble); + propertyTypeParsers.put(double.class, Double::parseDouble); + propertyTypeParsers.put(Boolean.class, Boolean::parseBoolean); + propertyTypeParsers.put(String.class, s -> s); + } + + /** + * Sets the value of a field to a certain value, using {@link ReflectionUtils#propertyTypeParsers} + * to try and parse the correct value. + * @param field The field to set. + * @param parent The object whose property value to set. + * @param s The string representation of the value. + * @throws IllegalAccessException If the field cannot be set. + */ + public static void set(Field field, Object parent, String s) throws IllegalAccessException { + var parser = propertyTypeParsers.get(field.getType()); + if (parser == null) { + throw new IllegalArgumentException("No supported property type parser for the type " + field.getType().getSimpleName()); + } + field.set(parent, parser.apply(s)); + } +} diff --git a/src/main/java/com/javadiscord/javabot/properties/config/UnknownPropertyException.java b/src/main/java/com/javadiscord/javabot/properties/config/UnknownPropertyException.java new file mode 100644 index 000000000..4c598cdc2 --- /dev/null +++ b/src/main/java/com/javadiscord/javabot/properties/config/UnknownPropertyException.java @@ -0,0 +1,15 @@ +package com.javadiscord.javabot.properties.config; + +import lombok.Getter; + +@Getter +public class UnknownPropertyException extends Exception { + private final String propertyName; + private final Object parentClass; + + public UnknownPropertyException(String propertyName, Class parentClass) { + super(String.format("No property named \"%s\" could be found for class %s.", propertyName, parentClass)); + this.propertyName = propertyName; + this.parentClass = parentClass; + } +} diff --git a/src/main/resources/commands.yaml b/src/main/resources/commands.yaml index edd64e843..81744f0fb 100644 --- a/src/main/resources/commands.yaml +++ b/src/main/resources/commands.yaml @@ -255,150 +255,39 @@ options: - {description: 'If given, shows the warn count of the given user', name: user, required: false, type: USER} -- description: Shows the config for the current guild + +# Configuration +- description: Manage configuration for this guild. name: config - handler: com.javadiscord.javabot.commands.configuation.config.Config + handler: com.javadiscord.javabot.commands.configuation.Config enabledByDefault: false privileges: - type: ROLE id: moderation.staffRoleId options: [] subCommands: - - description: Shows the current config - name: list - options: [] - - description: Changes the id of the stats category - name: stats-category - options: - - {description: ID of the new stats category, name: id, required: true, type: STRING} - - description: Changes the message of the stats category - name: stats-message - options: - - {description: New text of the stats category, name: message, required: true, - type: STRING} - - description: Changes the report channel - name: report-channel - options: - - {description: New report channel, name: channel, required: true, type: CHANNEL} - - description: Changes the report channel - name: starboard-channel - options: - - { description: New report channel, name: channel, required: true, type: CHANNEL } - - description: Changes the log channel - name: log-channel - options: - - {description: the new log channel, name: channel, required: true, type: CHANNEL} - - description: Changes the suggestion channel - name: suggestion-channel - options: - - {description: New suggestion channel, name: channel, required: true, type: CHANNEL} - - description: Changes the submission channel - name: submission-channel - options: - - {description: New submission channel, name: channel, required: true, type: CHANNEL} - - description: Changes the mute role - name: mute-role - options: - - {description: New mute role, name: role, required: true, type: ROLE} - - description: Changes the Staff role - name: staff-role - options: - - { description: New Staff role, name: role, required: true, type: ROLE } - - description: Changes the state of dm-qotw - name: dm-qotw - options: - - {description: State of dm-qotw, name: enabled, required: true, type: BOOLEAN} - - description: Changes the state of the server lock - name: lock - options: - - {description: State of the server lock, name: locked, required: true, type: BOOLEAN} - - description: Changes the Jam Admin Role - name: jam-admin-role - options: - - {description: New Jam Admin role, name: role, required: true, type: ROLE} - - description: Changes the Jam Ping Role - name: jam-ping-role - options: - - { description: New Jam Ping role, name: role, required: true, type: ROLE } - - description: Changes the Jam Announcement Channel - name: jam-announcement-channel - options: - - { description: New Jam Announcement Channel, name: channel, required: true, type: CHANNEL } - - description: Changes the Jam Vote Channel - name: jam-vote-channel - options: - - { description: New Jam Vote Channel, name: channel, required: true, type: CHANNEL } -- description: edits the welcome image config - name: welcome - handler: com.javadiscord.javabot.commands.configuation.welcome_system.WelcomeSystem - enabledByDefault: false - privileges: - - type: ROLE - id: moderation.staffRoleId - options: [] - subCommands: - - description: Sends the current welcome system config - name: list + - name: list + description: Get a list of all configuration properties, and their current values. options: [] - - description: Changes the leave message - name: leave-msg + - name: get + description: Get the current value of a configuration property. options: - - {description: New leave message, name: message, required: true, type: STRING} - - description: Changes the welcome message - name: join-msg - options: - - {description: New welcome message, name: message, required: true, type: STRING} - - description: Changes the Welcome-System status - name: status - options: - - {description: Status of the Welcome-System, name: status, required: true, type: BOOLEAN} - - description: Changes the welcome channel - name: channel - options: - - {description: New welcome channel, name: channel, required: true, type: CHANNEL} - - description: Changes the welcome image width - name: image-width - options: - - {description: New welcome image width, name: width, required: true, type: INTEGER} - - description: Changes the welcome image height - name: image-height - options: - - {description: New welcome image height, name: height, required: true, type: INTEGER} - - description: Changes the welcome image overlay url - name: overlay-url - options: - - {description: New welcome image url, name: url, required: true, type: STRING} - - description: Changes the welcome image background url - name: background-url - options: - - {description: New welcome image background url, name: url, required: true, type: STRING} - - description: Changes the primary color (tag) - name: primary-color - options: - - {description: New primary color (e.g. ff0000), name: color, required: true, - type: STRING} - - description: Changes the secondary color (member count) - name: secondary-color - options: - - {description: New secondary color (e.g. ff0000), name: color, required: true, - type: STRING} - - description: Changes the x-position of the avatar image - name: avatar-x - options: - - {description: New x-position of the avatar image, name: x, required: true, type: INTEGER} - - description: Changes the y-position of the avatar image - name: avatar-y - options: - - {description: New y-position of the avatar image, name: y, required: true, type: INTEGER} - - description: Changes the width of the avatar image - name: avatar-width - options: - - {description: New width of the avatar image, name: width, required: true, type: INTEGER} - - description: Changes the height of the avatar image - name: avatar-height + - name: property + description: The name of a property. + required: true + type: STRING + - name: set + description: Sets the value of a configuration property. options: - - {description: New height of the avatar image, name: height, required: true, - type: INTEGER} + - name: property + description: The name of a property. + required: true + type: STRING + - name: value + description: The value to set for the property. + required: true + type: STRING + - description: creates, edits or deletes custom slash commands name: customcommand handler: com.javadiscord.javabot.commands.custom_commands.CustomCommands