diff --git a/src/main/java/net/discordjug/javabot/data/h2db/commands/MigrateSubcommand.java b/src/main/java/net/discordjug/javabot/data/h2db/commands/MigrateSubcommand.java index 65d947cf1..3a4a28632 100644 --- a/src/main/java/net/discordjug/javabot/data/h2db/commands/MigrateSubcommand.java +++ b/src/main/java/net/discordjug/javabot/data/h2db/commands/MigrateSubcommand.java @@ -64,12 +64,11 @@ public MigrateSubcommand(ExecutorService asyncPool, DataSource dataSource, Syste } /** - * Replies with all available migrations to run. + * Finds all all available migrations to run. * - * @param event The {@link CommandAutoCompleteInteractionEvent} that was fired. * @return A {@link List} with all Option Choices. */ - public static @NotNull List replyMigrations(CommandAutoCompleteInteractionEvent event) { + public static @NotNull List getAvailableMigrations() { List choices = new ArrayList<>(25); try (Stream s = Files.list(MigrationUtils.getMigrationsDirectory())) { List paths = s.filter(path -> path.getFileName().toString().endsWith(".sql")).toList(); @@ -134,6 +133,6 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { @Override public void handleAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event, @NotNull AutoCompleteQuery target) { - event.replyChoices(AutoCompleteUtils.filterChoices(event, replyMigrations(event))).queue(); + event.replyChoices(AutoCompleteUtils.filterChoices(event, getAvailableMigrations())).queue(); } } diff --git a/src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCache.java b/src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCache.java index 236f4631e..f94ae7d9a 100644 --- a/src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCache.java +++ b/src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCache.java @@ -109,7 +109,7 @@ public void synchronizeNow() { public void cache(Message message) { MessageCacheConfig config = botConfig.get(message.getGuild()).getMessageCacheConfig(); if (cache.size() + 1 > config.getMaxCachedMessages()) { - cache.remove(0); + cache.removeFirst(); } if (messageCount >= config.getMessageSynchronizationInterval()) { synchronize(); @@ -131,7 +131,7 @@ public void sendUpdatedMessageToLog(Message updated, CachedMessage before) { if (config.getMessageCacheLogChannel() == null) return; if (updated.getContentRaw().trim().equals(before.getMessageContent()) && updated.getAttachments().size() == before.getAttachments().size()) return; MessageCreateAction action = config.getMessageCacheLogChannel() - .sendMessageEmbeds(buildMessageEditEmbed(updated.getGuild(), updated.getAuthor(), updated.getChannel(), before, updated)) + .sendMessageEmbeds(buildMessageEditEmbed(updated.getAuthor(), updated.getChannel(), before, updated)) .addComponents(ActionRow.of(Button.link(updated.getJumpUrl(), "Jump to Message"))); if (before.getMessageContent().length() > MessageEmbed.VALUE_MAX_LENGTH || updated.getContentRaw().length() > MessageEmbed.VALUE_MAX_LENGTH) { action.addFiles(FileUpload.fromData(buildEditedMessageFile(updated.getAuthor(), before, updated), before.getMessageId() + ".txt")); @@ -150,7 +150,7 @@ public void sendDeletedMessageToLog(Guild guild, MessageChannel channel, CachedM MessageCacheConfig config = botConfig.get(guild).getMessageCacheConfig(); if (config.getMessageCacheLogChannel() == null) return; guild.getJDA().retrieveUserById(message.getAuthorId()).queue(author -> { - MessageCreateAction action = config.getMessageCacheLogChannel().sendMessageEmbeds(buildMessageDeleteEmbed(guild, author, channel, message)); + MessageCreateAction action = config.getMessageCacheLogChannel().sendMessageEmbeds(buildMessageDeleteEmbed(author, channel, message)); if (message.getMessageContent().length() > MessageEmbed.VALUE_MAX_LENGTH) { action.addFiles(FileUpload.fromData(buildDeletedMessageFile(author, message), message.getMessageId() + ".txt")); } @@ -198,7 +198,7 @@ private EmbedBuilder buildMessageCacheEmbed(MessageChannel channel, User author, .setFooter("ID: " + before.getMessageId()); } - private MessageEmbed buildMessageEditEmbed(Guild guild, User author, MessageChannel channel, CachedMessage before, Message after) { + private MessageEmbed buildMessageEditEmbed(User author, MessageChannel channel, CachedMessage before, Message after) { EmbedBuilder eb = buildMessageCacheEmbed(channel, author, before) .setTitle("Message Edited") .setColor(Responses.Type.WARN.getColor()) @@ -225,7 +225,7 @@ private MessageEmbed buildMessageEditEmbed(Guild guild, User author, MessageChan .build(); } - private MessageEmbed buildMessageDeleteEmbed(Guild guild, User author, MessageChannel channel, CachedMessage message) { + private MessageEmbed buildMessageDeleteEmbed(User author, MessageChannel channel, CachedMessage message) { EmbedBuilder eb = buildMessageCacheEmbed(channel, author, message) .setTitle("Message Deleted") .setColor(Responses.Type.ERROR.getColor()) diff --git a/src/main/java/net/discordjug/javabot/systems/configuration/ConfigCommand.java b/src/main/java/net/discordjug/javabot/systems/configuration/ConfigCommand.java index 3ee642974..03948913c 100644 --- a/src/main/java/net/discordjug/javabot/systems/configuration/ConfigCommand.java +++ b/src/main/java/net/discordjug/javabot/systems/configuration/ConfigCommand.java @@ -1,7 +1,6 @@ package net.discordjug.javabot.systems.configuration; import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand; -import net.discordjug.javabot.data.config.BotConfig; import net.discordjug.javabot.systems.moderation.CommandModerationPermissions; import net.dv8tion.jda.api.interactions.commands.build.Commands; @@ -12,12 +11,11 @@ public class ConfigCommand extends SlashCommand implements CommandModerationPermissions { /** * The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}. - * @param botConfig The main configuration of the bot * @param exportConfigSubcommand /config export * @param getConfigSubcommand /config get * @param setConfigSubcommand /config set */ - public ConfigCommand(BotConfig botConfig, ExportConfigSubcommand exportConfigSubcommand, GetConfigSubcommand getConfigSubcommand, SetConfigSubcommand setConfigSubcommand) { + public ConfigCommand(ExportConfigSubcommand exportConfigSubcommand, GetConfigSubcommand getConfigSubcommand, SetConfigSubcommand setConfigSubcommand) { setModerationSlashCommandData(Commands.slash("config", "Administrative Commands for managing the bot's configuration.")); addSubcommands(exportConfigSubcommand, getConfigSubcommand, setConfigSubcommand); } diff --git a/src/main/java/net/discordjug/javabot/systems/configuration/ConfigSubcommand.java b/src/main/java/net/discordjug/javabot/systems/configuration/ConfigSubcommand.java index ce90afa32..792c7eee2 100644 --- a/src/main/java/net/discordjug/javabot/systems/configuration/ConfigSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/configuration/ConfigSubcommand.java @@ -50,7 +50,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { try { handleConfigSubcommand(event, botConfig.get(event.getGuild())).queue(); } catch (UnknownPropertyException e) { - Responses.warning(event, "Unknown Property", "The provided property could not be found.") + Responses.warnin(event, "Unknown Property", "The provided property could not be found.") .queue(); } } diff --git a/src/main/java/net/discordjug/javabot/systems/help/commands/HelpGuidelinesSubcommand.java b/src/main/java/net/discordjug/javabot/systems/help/commands/HelpGuidelinesSubcommand.java index c17b1e46a..088306081 100644 --- a/src/main/java/net/discordjug/javabot/systems/help/commands/HelpGuidelinesSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/help/commands/HelpGuidelinesSubcommand.java @@ -1,6 +1,5 @@ package net.discordjug.javabot.systems.help.commands; -import net.discordjug.javabot.data.config.BotConfig; import net.discordjug.javabot.util.Responses; import net.discordjug.javabot.util.StringResourceCache; import net.dv8tion.jda.api.EmbedBuilder; @@ -16,9 +15,8 @@ public class HelpGuidelinesSubcommand extends SlashCommand.Subcommand { /** * The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}. - * @param botConfig The main configuration of the bot */ - public HelpGuidelinesSubcommand(BotConfig botConfig) { + public HelpGuidelinesSubcommand() { setCommandData(new SubcommandData("guidelines", "Show the server's help guidelines in a simple format.")); } diff --git a/src/main/java/net/discordjug/javabot/systems/help/commands/UnreserveCommand.java b/src/main/java/net/discordjug/javabot/systems/help/commands/UnreserveCommand.java index 29c2666f0..432a70c28 100644 --- a/src/main/java/net/discordjug/javabot/systems/help/commands/UnreserveCommand.java +++ b/src/main/java/net/discordjug/javabot/systems/help/commands/UnreserveCommand.java @@ -125,7 +125,7 @@ private void onCloseRequest(Interaction interaction, IReplyCallback replyCallbac replyCallback.getUser().getIdLong() == manager.getPostThread().getOwnerIdLong(), reason); } else { - Responses.warning(replyCallback, "Could not close this post", "You're not allowed to close this post.").queue(); + Responses.warnin(replyCallback, "Could not close this post", "You're not allowed to close this post.").queue(); } } @@ -134,7 +134,7 @@ private boolean isReasonInvalid(String reason) { } private void replyInvalidChannel(IReplyCallback replyCallback) { - Responses.warning(replyCallback, "Invalid Channel", + Responses.warnin(replyCallback, "Invalid Channel", "This command may only be used in either the text-channel-based help system, or in our new forum help system.") .queue(); } diff --git a/src/main/java/net/discordjug/javabot/systems/help/dao/HelpAccountRepository.java b/src/main/java/net/discordjug/javabot/systems/help/dao/HelpAccountRepository.java index 518be7244..0d230ee3b 100644 --- a/src/main/java/net/discordjug/javabot/systems/help/dao/HelpAccountRepository.java +++ b/src/main/java/net/discordjug/javabot/systems/help/dao/HelpAccountRepository.java @@ -30,7 +30,7 @@ public class HelpAccountRepository { * Inserts a new {@link HelpAccount}. * * @param account The account that should be inserted. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public void insert(HelpAccount account) throws DataAccessException { @@ -44,7 +44,7 @@ public void insert(HelpAccount account) throws DataAccessException { * Updates a single {@link HelpAccount}. * * @param account The account that should be updated. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public void update(HelpAccount account) throws DataAccessException { jdbcTemplate.update("UPDATE help_account SET experience = ? WHERE user_id = ?", @@ -57,7 +57,7 @@ public void update(HelpAccount account) throws DataAccessException { * * @param userId The user's id. * @return An {@link HelpAccount} object, as an {@link Optional}. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public Optional getByUserId(long userId) throws DataAccessException { try { @@ -73,7 +73,7 @@ public Optional getByUserId(long userId) throws DataAccessException * @param page The page. * @param size The amount of {@link HelpAccount}s to return. * @return A {@link List} containing the specified amount of {@link HelpAccount}s. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public List getAccounts(int page, int size) throws DataAccessException { return jdbcTemplate.query("SELECT * FROM help_account WHERE experience > 0 ORDER BY experience DESC LIMIT ? OFFSET ?", (rs, row)->this.read(rs), @@ -84,7 +84,7 @@ public List getAccounts(int page, int size) throws DataAccessExcept * Gets the total amount of {@link HelpAccount}s stored in the database, that have more than 0 experience. * * @return The amount, as an {@link Integer}. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public int getTotalAccounts() throws DataAccessException { try { diff --git a/src/main/java/net/discordjug/javabot/systems/help/dao/HelpTransactionRepository.java b/src/main/java/net/discordjug/javabot/systems/help/dao/HelpTransactionRepository.java index 2446fc72f..3619467f3 100644 --- a/src/main/java/net/discordjug/javabot/systems/help/dao/HelpTransactionRepository.java +++ b/src/main/java/net/discordjug/javabot/systems/help/dao/HelpTransactionRepository.java @@ -33,7 +33,7 @@ public class HelpTransactionRepository { * * @param transaction The transaction that should be inserted. * @return The inserted {@link HelpTransaction}. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public HelpTransaction save(HelpTransaction transaction) throws DataAccessException { SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate) @@ -56,7 +56,7 @@ public HelpTransaction save(HelpTransaction transaction) throws DataAccessExcept * * @param id The transaction's id. * @return A {@link HelpTransaction} object. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public Optional getTransaction(long id) throws DataAccessException { try { @@ -72,7 +72,7 @@ public Optional getTransaction(long id) throws DataAccessExcept * @param userId The user's id. * @param count The count of transactions that should be retrieved. * @return A List with all {@link HelpTransaction}s. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public List getTransactions(long userId, int count) throws DataAccessException { return jdbcTemplate.query("SELECT * FROM help_transaction WHERE recipient = ? ORDER BY created_at DESC LIMIT ?", (rs, rowNumber) -> this.read(rs), userId, count); diff --git a/src/main/java/net/discordjug/javabot/systems/moderation/PurgeCommand.java b/src/main/java/net/discordjug/javabot/systems/moderation/PurgeCommand.java index 5fab74220..9abfcc65b 100644 --- a/src/main/java/net/discordjug/javabot/systems/moderation/PurgeCommand.java +++ b/src/main/java/net/discordjug/javabot/systems/moderation/PurgeCommand.java @@ -74,10 +74,10 @@ protected ReplyCallbackAction handleModerationCommand(@NotNull SlashCommandInter boolean archive = event.getOption("archive", true, OptionMapping::getAsBoolean); ModerationConfig config = botConfig.get(event.getGuild()).getModerationConfig(); - Long amount = (amountOption == null) ? 1 : amountOption.getAsLong(); + long amount = (amountOption == null) ? 1 : amountOption.getAsLong(); User user = (userOption == null) ? null : userOption.getAsUser(); int maxAmount = config.getPurgeMaxMessageCount(); - if (amount == null || amount > maxAmount) { + if (amount < 1 || amount > maxAmount) { return Responses.warning(event, "Invalid amount. Should be between 1 and " + maxAmount + ", inclusive."); } if (amount == 0) { diff --git a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/AcceptSuggestionSubcommand.java b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/AcceptSuggestionSubcommand.java index 683985c0a..29ab4ce34 100644 --- a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/AcceptSuggestionSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/AcceptSuggestionSubcommand.java @@ -1,7 +1,6 @@ package net.discordjug.javabot.systems.staff_commands.suggestions; import net.discordjug.javabot.data.config.BotConfig; -import net.discordjug.javabot.data.config.GuildConfig; import net.discordjug.javabot.util.Responses; import net.discordjug.javabot.util.UserUtils; import net.dv8tion.jda.api.EmbedBuilder; @@ -31,9 +30,9 @@ public AcceptSuggestionSubcommand(BotConfig botConfig) { } @Override - protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message, GuildConfig config) { + protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message) { MessageEmbed embed = message.getEmbeds().get(0); - MessageEmbed declineEmbed = buildSuggestionAcceptEmbed(event.getUser(), embed, config); + MessageEmbed declineEmbed = buildSuggestionAcceptEmbed(event.getUser(), embed); message.editMessageEmbeds(declineEmbed).queue( edit -> edit.addReaction(botConfig.getSystems().getEmojiConfig().getSuccessEmote(event.getJDA())).queue(), error -> Responses.error(event.getHook(), error.getMessage()).queue()); @@ -41,7 +40,7 @@ protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull S .setComponents(getJumpButton(message)); } - private @NotNull MessageEmbed buildSuggestionAcceptEmbed(@NotNull User user, @NotNull MessageEmbed embed, @NotNull GuildConfig config) { + private @NotNull MessageEmbed buildSuggestionAcceptEmbed(@NotNull User user, @NotNull MessageEmbed embed) { return new EmbedBuilder() .setColor(Responses.Type.SUCCESS.getColor()) .setAuthor(embed.getAuthor().getName(), embed.getAuthor().getUrl(), embed.getAuthor().getIconUrl()) diff --git a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/ClearSuggestionSubcommand.java b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/ClearSuggestionSubcommand.java index c0ec2afb4..a531c5cce 100644 --- a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/ClearSuggestionSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/ClearSuggestionSubcommand.java @@ -1,7 +1,6 @@ package net.discordjug.javabot.systems.staff_commands.suggestions; import net.discordjug.javabot.data.config.BotConfig; -import net.discordjug.javabot.data.config.GuildConfig; import net.discordjug.javabot.data.config.SystemsConfig; import net.discordjug.javabot.util.Responses; import net.dv8tion.jda.api.EmbedBuilder; @@ -30,9 +29,9 @@ public ClearSuggestionSubcommand(BotConfig botConfig) { } @Override - protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message, GuildConfig config) { + protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message) { MessageEmbed embed = message.getEmbeds().get(0); - MessageEmbed clearEmbed = buildSuggestionClearEmbed(embed, config); + MessageEmbed clearEmbed = buildSuggestionClearEmbed(embed); SystemsConfig.EmojiConfig emojiConfig = botConfig.getSystems().getEmojiConfig(); message.editMessageEmbeds(clearEmbed).queue( edit -> { @@ -44,7 +43,7 @@ protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull S .setComponents(getJumpButton(message)); } - private @NotNull MessageEmbed buildSuggestionClearEmbed(@NotNull MessageEmbed embed, @NotNull GuildConfig config) { + private @NotNull MessageEmbed buildSuggestionClearEmbed(@NotNull MessageEmbed embed) { return new EmbedBuilder() .setColor(Responses.Type.DEFAULT.getColor()) .setAuthor(embed.getAuthor().getName(), embed.getAuthor().getUrl(), embed.getAuthor().getIconUrl()) diff --git a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/DeclineSuggestionSubcommand.java b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/DeclineSuggestionSubcommand.java index 19d29ce21..6784edbe2 100644 --- a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/DeclineSuggestionSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/DeclineSuggestionSubcommand.java @@ -1,7 +1,6 @@ package net.discordjug.javabot.systems.staff_commands.suggestions; import net.discordjug.javabot.data.config.BotConfig; -import net.discordjug.javabot.data.config.GuildConfig; import net.discordjug.javabot.util.Responses; import net.discordjug.javabot.util.UserUtils; import net.dv8tion.jda.api.EmbedBuilder; @@ -32,7 +31,7 @@ public DeclineSuggestionSubcommand(BotConfig botConfig) { } @Override - protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message, GuildConfig config) { + protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message) { String reason = event.getOption("reason", null, OptionMapping::getAsString); MessageEmbed embed = message.getEmbeds().get(0); MessageEmbed declineEmbed = buildSuggestionDeclineEmbed(event.getUser(), embed, reason); diff --git a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/OnHoldSuggestionSubcommand.java b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/OnHoldSuggestionSubcommand.java index 5e43cebec..abc6e0b77 100644 --- a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/OnHoldSuggestionSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/OnHoldSuggestionSubcommand.java @@ -1,7 +1,6 @@ package net.discordjug.javabot.systems.staff_commands.suggestions; import net.discordjug.javabot.data.config.BotConfig; -import net.discordjug.javabot.data.config.GuildConfig; import net.discordjug.javabot.util.Responses; import net.discordjug.javabot.util.UserUtils; import net.dv8tion.jda.api.EmbedBuilder; @@ -32,9 +31,9 @@ public OnHoldSuggestionSubcommand(BotConfig botConfig) { } @Override - protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message, GuildConfig config) { + protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message) { MessageEmbed embed = message.getEmbeds().get(0); - MessageEmbed onHoldEmbed = buildSuggestionAcceptEmbed(event.getUser(), embed, config); + MessageEmbed onHoldEmbed = buildSuggestionAcceptEmbed(event.getUser(), embed); message.editMessageEmbeds(onHoldEmbed).queue( edit -> edit.addReaction(botConfig.getSystems().getEmojiConfig().getClockEmoji()).queue(), error -> Responses.error(event.getHook(), error.getMessage()).queue()); @@ -42,7 +41,7 @@ protected WebhookMessageCreateAction handleSuggestionCommand(@NotNull S .setComponents(getJumpButton(message)); } - private @NotNull MessageEmbed buildSuggestionAcceptEmbed(@NotNull User user, @NotNull MessageEmbed embed, @NotNull GuildConfig config) { + private @NotNull MessageEmbed buildSuggestionAcceptEmbed(@NotNull User user, @NotNull MessageEmbed embed) { return new EmbedBuilder() .setColor(Responses.Type.WARN.getColor()) .setAuthor(embed.getAuthor().getName(), embed.getAuthor().getUrl(), embed.getAuthor().getIconUrl()) diff --git a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/SuggestionSubcommand.java b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/SuggestionSubcommand.java index 049cf386b..8d30a43db 100644 --- a/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/SuggestionSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/SuggestionSubcommand.java @@ -54,12 +54,12 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { long messageId = messageIdMapping.getAsLong(); event.deferReply(true).queue(); event.getMessageChannel().retrieveMessageById(messageId).queue( - message -> message.clearReactions().queue(s -> handleSuggestionCommand(event, message, config).queue()), + message -> message.clearReactions().queue(s -> handleSuggestionCommand(event, message).queue()), e -> Responses.error(event.getHook(), "Could not find suggestion message with id " + messageId).queue()); } - protected abstract WebhookMessageCreateAction handleSuggestionCommand(@Nonnull SlashCommandInteractionEvent event, @Nonnull Message message, GuildConfig config); + protected abstract WebhookMessageCreateAction handleSuggestionCommand(@Nonnull SlashCommandInteractionEvent event, @Nonnull Message message); protected ActionRow getJumpButton(@NotNull Message m) { return ActionRow.of(Button.link(m.getJumpUrl(), "Jump to Message")); diff --git a/src/main/java/net/discordjug/javabot/systems/staff_commands/tags/CustomTagManager.java b/src/main/java/net/discordjug/javabot/systems/staff_commands/tags/CustomTagManager.java index 4c2f731dc..fd699cbc7 100644 --- a/src/main/java/net/discordjug/javabot/systems/staff_commands/tags/CustomTagManager.java +++ b/src/main/java/net/discordjug/javabot/systems/staff_commands/tags/CustomTagManager.java @@ -121,7 +121,7 @@ public void init(JDA jda) throws SQLException { * * @param guildId The guilds' id. * @return A {@link Set} of all {@link CustomTag}s for the current guild. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ @Contract("_ -> new") private @NotNull Set getCustomTags(long guildId) throws DataAccessException { @@ -176,7 +176,7 @@ public boolean addCommand(@NotNull Guild guild, @NotNull CustomTag tag) throws D * @param guildId The guilds' id. * @param tag The {@link CustomTag} to delete. * @return Whether the command was successfully deleted. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public boolean removeCommand(long guildId, @NotNull CustomTag tag) throws DataAccessException { if (!doesTagExist(guildId, tag.getName())) { @@ -195,7 +195,7 @@ public boolean removeCommand(long guildId, @NotNull CustomTag tag) throws DataAc * @param old The "old" {@link CustomTag}. * @param update The "new" and updated {@link CustomTag} object. * @return Whether the command was successfully edited. - * @throws SQLException If an error occurs. + * @throws DataAccessException If an error occurs. */ public boolean editCommand(long guildId, @NotNull CustomTag old, @NotNull CustomTag update) throws DataAccessException { if (!doesTagExist(guildId, old.getName())) { diff --git a/src/main/java/net/discordjug/javabot/systems/user_commands/MoveConversationCommand.java b/src/main/java/net/discordjug/javabot/systems/user_commands/MoveConversationCommand.java index 2279ba892..5a6ad9c58 100644 --- a/src/main/java/net/discordjug/javabot/systems/user_commands/MoveConversationCommand.java +++ b/src/main/java/net/discordjug/javabot/systems/user_commands/MoveConversationCommand.java @@ -61,19 +61,19 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { } GuildMessageChannel channel = channelMapping.getAsChannel().asGuildMessageChannel(); if (event.getChannel().getIdLong() == channel.getIdLong()) { - Responses.warning(event, "Invalid Channel", "You cannot move the conversation to the same channel!").queue(); + Responses.warnin(event, "Invalid Channel", "You cannot move the conversation to the same channel!").queue(); return; } if (channelMapping.getAsChannel().getType() == ChannelType.TEXT && channelMapping.getAsChannel().asTextChannel().getSlowmode() > 0) { - Responses.warning(event, "Invalid Channel", "You cannot move the conversation to a channel that has slowmode enabled!").queue(); + Responses.warnin(event, "Invalid Channel", "You cannot move the conversation to a channel that has slowmode enabled!").queue(); return; } if (isInvalidChannel(event.getMember(), channel)) { - Responses.warning(event, "Invalid Channel", "You're not allowed to move the conversation to %s", channel.getAsMention()).queue(); + Responses.warnin(event, "Invalid Channel", "You're not allowed to move the conversation to %s", channel.getAsMention()).queue(); return; } if (isInvalidChannel(event.getGuild().getSelfMember(), channel)) { - Responses.error(event, "Insufficient Permissions", "I'm not allowed to sent messages to %s", channel.getAsMention()).queue(); + Responses.errorWithTitle(event, "Insufficient Permissions", "I'm not allowed to sent messages to %s", channel.getAsMention()).queue(); return; } event.deferReply(true).queue(); diff --git a/src/main/java/net/discordjug/javabot/systems/user_commands/leaderboard/ExperienceLeaderboardSubcommand.java b/src/main/java/net/discordjug/javabot/systems/user_commands/leaderboard/ExperienceLeaderboardSubcommand.java index 794d0d04c..373452337 100644 --- a/src/main/java/net/discordjug/javabot/systems/user_commands/leaderboard/ExperienceLeaderboardSubcommand.java +++ b/src/main/java/net/discordjug/javabot/systems/user_commands/leaderboard/ExperienceLeaderboardSubcommand.java @@ -4,7 +4,6 @@ import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand; import xyz.dynxsty.dih4jda.interactions.components.ButtonHandler; import net.discordjug.javabot.annotations.AutoDetectableComponentHandler; -import net.discordjug.javabot.data.config.BotConfig; import net.discordjug.javabot.systems.help.dao.HelpAccountRepository; import net.discordjug.javabot.systems.help.dao.HelpTransactionRepository; import net.discordjug.javabot.util.ExceptionLogger; @@ -17,7 +16,6 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.MessageEmbed; -import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.interactions.commands.OptionMapping; @@ -36,6 +34,7 @@ import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.function.BiFunction; +import java.util.function.Function; /** *

This class represents the /leaderboard help-experience command.

@@ -48,20 +47,17 @@ public class ExperienceLeaderboardSubcommand extends SlashCommand.Subcommand imp public static final String CACHE_PREFIX = "xp_leaderboard"; private static final int PAGE_SIZE = 10; - private final BotConfig botConfig; private final ExecutorService asyncPool; private final HelpAccountRepository helpAccountRepository; private final HelpTransactionRepository helpTransactionRepository; /** * The constructor of this class, which sets the corresponding {@link SubcommandData}. - * @param botConfig main configuration of the bot. * @param helpAccountRepository Dao object that represents the HELP_ACCOUNT SQL Table. * @param asyncPool the main thread pool for asynchronous operations * @param helpTransactionRepository Dao object that represents the HELP_TRANSACTIONS SQL Table. */ - public ExperienceLeaderboardSubcommand(BotConfig botConfig, HelpAccountRepository helpAccountRepository, ExecutorService asyncPool, HelpTransactionRepository helpTransactionRepository) { - this.botConfig = botConfig; + public ExperienceLeaderboardSubcommand(HelpAccountRepository helpAccountRepository, ExecutorService asyncPool, HelpTransactionRepository helpTransactionRepository) { this.asyncPool = asyncPool; this.helpAccountRepository = helpAccountRepository; this.helpTransactionRepository = helpTransactionRepository; @@ -96,7 +92,7 @@ public void handleButton(@NotNull ButtonInteractionEvent event, Button button) { case MONTH -> helpTransactionRepository.getNumberOfUsersWithHelpXPInLastMonth(); case TOTAL -> helpAccountRepository.getTotalAccounts(); }; - int maxPage = totalAccounts / PAGE_SIZE; + int maxPage = getMaxPage(totalAccounts); if (page <= 0) { page = maxPage; } @@ -113,29 +109,32 @@ public void handleButton(@NotNull ButtonInteractionEvent event, Button button) { }); } + private int getMaxPage(int totalAccounts) { + return (int)Math.ceil(totalAccounts / (double)PAGE_SIZE); + } + private @NotNull Pair buildExperienceLeaderboard(Guild guild, int page, LeaderboardType type) throws DataAccessException, IOException { return switch (type) { case TOTAL -> buildGenericExperienceLeaderboard(page, helpAccountRepository.getTotalAccounts(), "total Leaderboard of help experience", - helpAccountRepository::getAccounts, (position, account) -> { - Pair currentRole = account.getCurrentExperienceGoal(botConfig, guild); - return createUserData(guild, position, account.getExperience(), account.getUserId(), currentRole.first() != null ? currentRole.first().getAsMention() + ": " : ""); + helpAccountRepository::getAccounts, account -> { + return createUserData(guild, account.getExperience(), account.getUserId()); }); case MONTH -> buildGenericExperienceLeaderboard(page, helpTransactionRepository.getNumberOfUsersWithHelpXPInLastMonth(), """ help experience leaderboard from the last 30 days This leaderboard does not include experience decay. """, - helpTransactionRepository::getTotalTransactionWeightsInLastMonth, (position, xpInfo) -> { - return createUserData(guild, position, (double) xpInfo.second(), xpInfo.first(), ""); + helpTransactionRepository::getTotalTransactionWeightsInLastMonth, xpInfo -> { + return createUserData(guild, (double) xpInfo.second(), xpInfo.first()); }); }; } private @NotNull Pair buildGenericExperienceLeaderboard(int page, int totalAccounts, String description, - BiFunction> accountsReader, BiFunction fieldExtractor) throws DataAccessException, IOException { + BiFunction> accountsReader, Function fieldExtractor) throws DataAccessException, IOException { - int maxPage = totalAccounts / PAGE_SIZE; + int maxPage = getMaxPage(totalAccounts); int actualPage = Math.max(1, Math.min(page, maxPage)); List accounts = accountsReader.apply(actualPage, PAGE_SIZE); @@ -151,7 +150,7 @@ public void handleButton(@NotNull ButtonInteractionEvent event, Button button) { try (LeaderboardCreator creator = new LeaderboardCreator(accounts.size(), null)){ for (int i = 0; i < accounts.size(); i++) { int position = (i + 1) + (actualPage - 1) * PAGE_SIZE; - UserData userInfo = fieldExtractor.apply(position, accounts.get(i)); + UserData userInfo = fieldExtractor.apply(accounts.get(i)); creator.drawLeaderboardEntry(userInfo.member(), userInfo.displayName(), userInfo.xp(), position); } return creator.getImageBytes(cacheName, pageCachePrefix); @@ -161,8 +160,8 @@ public void handleButton(@NotNull ButtonInteractionEvent event, Button button) { return new Pair(builder.build(), FileUpload.fromData(bytes, "leaderboard.png")); } - private UserData createUserData(Guild guild, Integer position, double experience, long userId, String prefix) { - Member member = guild.retrieveMemberById(userId).onErrorMap(e -> null).complete(); + private UserData createUserData(Guild guild, double experience, long userId) { + Member member = guild.retrieveMemberById(userId).onErrorMap(_ -> null).complete(); String displayName; if (member == null) { displayName = String.valueOf(userId); diff --git a/src/main/java/net/discordjug/javabot/systems/user_commands/search/SearchWebMessageContext.java b/src/main/java/net/discordjug/javabot/systems/user_commands/search/SearchWebMessageContext.java index ce4cff601..cd0e0e37a 100644 --- a/src/main/java/net/discordjug/javabot/systems/user_commands/search/SearchWebMessageContext.java +++ b/src/main/java/net/discordjug/javabot/systems/user_commands/search/SearchWebMessageContext.java @@ -34,7 +34,7 @@ public SearchWebMessageContext(SystemsConfig systemsConfig) { public void execute(@NotNull MessageContextInteractionEvent event) { String query = event.getTarget().getContentDisplay(); if (query.isEmpty() || query.isBlank()) { - Responses.warning(event, "No Content", "The message doesn't have any content to search for").queue(); + Responses.warnin(event, "No Content", "The message doesn't have any content to search for").queue(); return; } event.deferReply().queue(); diff --git a/src/main/java/net/discordjug/javabot/util/Responses.java b/src/main/java/net/discordjug/javabot/util/Responses.java index 78cbaef7d..b48de0085 100644 --- a/src/main/java/net/discordjug/javabot/util/Responses.java +++ b/src/main/java/net/discordjug/javabot/util/Responses.java @@ -56,6 +56,11 @@ private Responses() { public static @NotNull ReplyCallbackAction error(IReplyCallback event, String message, Object... args) { return reply(event, "An Error Occurred", String.format(message, args), Type.ERROR.getColor(), true); } + + @CheckReturnValue + public static @NotNull ReplyCallbackAction errorWithTitle(IReplyCallback event, String title, String message, Object... args) { + return reply(event, title, String.format(message, args), Type.ERROR.getColor(), true); + } @CheckReturnValue public static @NotNull WebhookMessageCreateAction error(InteractionHook hook, String message, Object... args) { @@ -64,7 +69,7 @@ private Responses() { @CheckReturnValue public static @NotNull ReplyCallbackAction warning(IReplyCallback event, String message, Object... args) { - return warning(event, null, String.format(message, args)); + return warnin(event, null, String.format(message, args)); } @CheckReturnValue @@ -73,7 +78,7 @@ private Responses() { } @CheckReturnValue - public static @NotNull ReplyCallbackAction warning(IReplyCallback event, String title, String message, Object... args) { + public static @NotNull ReplyCallbackAction warnin(IReplyCallback event, String title, String message, Object... args) { return reply(event, title, String.format(message, args), Type.WARN.getColor(), true); }