Skip to content

Commit 2187430

Browse files
committed
code quality improvements, help leaderboard fixes
1 parent 7b79463 commit 2187430

19 files changed

+66
-71
lines changed

src/main/java/net/discordjug/javabot/data/h2db/commands/MigrateSubcommand.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ public MigrateSubcommand(ExecutorService asyncPool, DataSource dataSource, Syste
6464
}
6565

6666
/**
67-
* Replies with all available migrations to run.
67+
* Finds all all available migrations to run.
6868
*
69-
* @param event The {@link CommandAutoCompleteInteractionEvent} that was fired.
7069
* @return A {@link List} with all Option Choices.
7170
*/
72-
public static @NotNull List<Command.Choice> replyMigrations(CommandAutoCompleteInteractionEvent event) {
71+
public static @NotNull List<Command.Choice> getAvailableMigrations() {
7372
List<Command.Choice> choices = new ArrayList<>(25);
7473
try (Stream<Path> s = Files.list(MigrationUtils.getMigrationsDirectory())) {
7574
List<Path> paths = s.filter(path -> path.getFileName().toString().endsWith(".sql")).toList();
@@ -134,6 +133,6 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
134133

135134
@Override
136135
public void handleAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event, @NotNull AutoCompleteQuery target) {
137-
event.replyChoices(AutoCompleteUtils.filterChoices(event, replyMigrations(event))).queue();
136+
event.replyChoices(AutoCompleteUtils.filterChoices(event, getAvailableMigrations())).queue();
138137
}
139138
}

src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void synchronizeNow() {
109109
public void cache(Message message) {
110110
MessageCacheConfig config = botConfig.get(message.getGuild()).getMessageCacheConfig();
111111
if (cache.size() + 1 > config.getMaxCachedMessages()) {
112-
cache.remove(0);
112+
cache.removeFirst();
113113
}
114114
if (messageCount >= config.getMessageSynchronizationInterval()) {
115115
synchronize();
@@ -131,7 +131,7 @@ public void sendUpdatedMessageToLog(Message updated, CachedMessage before) {
131131
if (config.getMessageCacheLogChannel() == null) return;
132132
if (updated.getContentRaw().trim().equals(before.getMessageContent()) && updated.getAttachments().size() == before.getAttachments().size()) return;
133133
MessageCreateAction action = config.getMessageCacheLogChannel()
134-
.sendMessageEmbeds(buildMessageEditEmbed(updated.getGuild(), updated.getAuthor(), updated.getChannel(), before, updated))
134+
.sendMessageEmbeds(buildMessageEditEmbed(updated.getAuthor(), updated.getChannel(), before, updated))
135135
.addComponents(ActionRow.of(Button.link(updated.getJumpUrl(), "Jump to Message")));
136136
if (before.getMessageContent().length() > MessageEmbed.VALUE_MAX_LENGTH || updated.getContentRaw().length() > MessageEmbed.VALUE_MAX_LENGTH) {
137137
action.addFiles(FileUpload.fromData(buildEditedMessageFile(updated.getAuthor(), before, updated), before.getMessageId() + ".txt"));
@@ -150,7 +150,7 @@ public void sendDeletedMessageToLog(Guild guild, MessageChannel channel, CachedM
150150
MessageCacheConfig config = botConfig.get(guild).getMessageCacheConfig();
151151
if (config.getMessageCacheLogChannel() == null) return;
152152
guild.getJDA().retrieveUserById(message.getAuthorId()).queue(author -> {
153-
MessageCreateAction action = config.getMessageCacheLogChannel().sendMessageEmbeds(buildMessageDeleteEmbed(guild, author, channel, message));
153+
MessageCreateAction action = config.getMessageCacheLogChannel().sendMessageEmbeds(buildMessageDeleteEmbed(author, channel, message));
154154
if (message.getMessageContent().length() > MessageEmbed.VALUE_MAX_LENGTH) {
155155
action.addFiles(FileUpload.fromData(buildDeletedMessageFile(author, message), message.getMessageId() + ".txt"));
156156
}
@@ -198,7 +198,7 @@ private EmbedBuilder buildMessageCacheEmbed(MessageChannel channel, User author,
198198
.setFooter("ID: " + before.getMessageId());
199199
}
200200

201-
private MessageEmbed buildMessageEditEmbed(Guild guild, User author, MessageChannel channel, CachedMessage before, Message after) {
201+
private MessageEmbed buildMessageEditEmbed(User author, MessageChannel channel, CachedMessage before, Message after) {
202202
EmbedBuilder eb = buildMessageCacheEmbed(channel, author, before)
203203
.setTitle("Message Edited")
204204
.setColor(Responses.Type.WARN.getColor())
@@ -225,7 +225,7 @@ private MessageEmbed buildMessageEditEmbed(Guild guild, User author, MessageChan
225225
.build();
226226
}
227227

228-
private MessageEmbed buildMessageDeleteEmbed(Guild guild, User author, MessageChannel channel, CachedMessage message) {
228+
private MessageEmbed buildMessageDeleteEmbed(User author, MessageChannel channel, CachedMessage message) {
229229
EmbedBuilder eb = buildMessageCacheEmbed(channel, author, message)
230230
.setTitle("Message Deleted")
231231
.setColor(Responses.Type.ERROR.getColor())

src/main/java/net/discordjug/javabot/systems/configuration/ConfigCommand.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.discordjug.javabot.systems.configuration;
22

33
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
4-
import net.discordjug.javabot.data.config.BotConfig;
54
import net.discordjug.javabot.systems.moderation.CommandModerationPermissions;
65
import net.dv8tion.jda.api.interactions.commands.build.Commands;
76

@@ -12,12 +11,11 @@
1211
public class ConfigCommand extends SlashCommand implements CommandModerationPermissions {
1312
/**
1413
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
15-
* @param botConfig The main configuration of the bot
1614
* @param exportConfigSubcommand /config export
1715
* @param getConfigSubcommand /config get
1816
* @param setConfigSubcommand /config set
1917
*/
20-
public ConfigCommand(BotConfig botConfig, ExportConfigSubcommand exportConfigSubcommand, GetConfigSubcommand getConfigSubcommand, SetConfigSubcommand setConfigSubcommand) {
18+
public ConfigCommand(ExportConfigSubcommand exportConfigSubcommand, GetConfigSubcommand getConfigSubcommand, SetConfigSubcommand setConfigSubcommand) {
2119
setModerationSlashCommandData(Commands.slash("config", "Administrative Commands for managing the bot's configuration."));
2220
addSubcommands(exportConfigSubcommand, getConfigSubcommand, setConfigSubcommand);
2321
}

src/main/java/net/discordjug/javabot/systems/configuration/ConfigSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
5050
try {
5151
handleConfigSubcommand(event, botConfig.get(event.getGuild())).queue();
5252
} catch (UnknownPropertyException e) {
53-
Responses.warning(event, "Unknown Property", "The provided property could not be found.")
53+
Responses.warnin(event, "Unknown Property", "The provided property could not be found.")
5454
.queue();
5555
}
5656
}

src/main/java/net/discordjug/javabot/systems/help/commands/HelpGuidelinesSubcommand.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.discordjug.javabot.systems.help.commands;
22

3-
import net.discordjug.javabot.data.config.BotConfig;
43
import net.discordjug.javabot.util.Responses;
54
import net.discordjug.javabot.util.StringResourceCache;
65
import net.dv8tion.jda.api.EmbedBuilder;
@@ -16,9 +15,8 @@
1615
public class HelpGuidelinesSubcommand extends SlashCommand.Subcommand {
1716
/**
1817
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
19-
* @param botConfig The main configuration of the bot
2018
*/
21-
public HelpGuidelinesSubcommand(BotConfig botConfig) {
19+
public HelpGuidelinesSubcommand() {
2220
setCommandData(new SubcommandData("guidelines", "Show the server's help guidelines in a simple format."));
2321
}
2422

src/main/java/net/discordjug/javabot/systems/help/commands/UnreserveCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void onCloseRequest(Interaction interaction, IReplyCallback replyCallbac
125125
replyCallback.getUser().getIdLong() == manager.getPostThread().getOwnerIdLong(),
126126
reason);
127127
} else {
128-
Responses.warning(replyCallback, "Could not close this post", "You're not allowed to close this post.").queue();
128+
Responses.warnin(replyCallback, "Could not close this post", "You're not allowed to close this post.").queue();
129129
}
130130
}
131131

@@ -134,7 +134,7 @@ private boolean isReasonInvalid(String reason) {
134134
}
135135

136136
private void replyInvalidChannel(IReplyCallback replyCallback) {
137-
Responses.warning(replyCallback, "Invalid Channel",
137+
Responses.warnin(replyCallback, "Invalid Channel",
138138
"This command may only be used in either the text-channel-based help system, or in our new forum help system.")
139139
.queue();
140140
}

src/main/java/net/discordjug/javabot/systems/help/dao/HelpAccountRepository.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class HelpAccountRepository {
3030
* Inserts a new {@link HelpAccount}.
3131
*
3232
* @param account The account that should be inserted.
33-
* @throws SQLException If an error occurs.
33+
* @throws DataAccessException If an error occurs.
3434
*/
3535
public void insert(HelpAccount account) throws DataAccessException {
3636

@@ -44,7 +44,7 @@ public void insert(HelpAccount account) throws DataAccessException {
4444
* Updates a single {@link HelpAccount}.
4545
*
4646
* @param account The account that should be updated.
47-
* @throws SQLException If an error occurs.
47+
* @throws DataAccessException If an error occurs.
4848
*/
4949
public void update(HelpAccount account) throws DataAccessException {
5050
jdbcTemplate.update("UPDATE help_account SET experience = ? WHERE user_id = ?",
@@ -57,7 +57,7 @@ public void update(HelpAccount account) throws DataAccessException {
5757
*
5858
* @param userId The user's id.
5959
* @return An {@link HelpAccount} object, as an {@link Optional}.
60-
* @throws SQLException If an error occurs.
60+
* @throws DataAccessException If an error occurs.
6161
*/
6262
public Optional<HelpAccount> getByUserId(long userId) throws DataAccessException {
6363
try {
@@ -73,7 +73,7 @@ public Optional<HelpAccount> getByUserId(long userId) throws DataAccessException
7373
* @param page The page.
7474
* @param size The amount of {@link HelpAccount}s to return.
7575
* @return A {@link List} containing the specified amount of {@link HelpAccount}s.
76-
* @throws SQLException If an error occurs.
76+
* @throws DataAccessException If an error occurs.
7777
*/
7878
public List<HelpAccount> getAccounts(int page, int size) throws DataAccessException {
7979
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<HelpAccount> getAccounts(int page, int size) throws DataAccessExcept
8484
* Gets the total amount of {@link HelpAccount}s stored in the database, that have more than 0 experience.
8585
*
8686
* @return The amount, as an {@link Integer}.
87-
* @throws SQLException If an error occurs.
87+
* @throws DataAccessException If an error occurs.
8888
*/
8989
public int getTotalAccounts() throws DataAccessException {
9090
try {

src/main/java/net/discordjug/javabot/systems/help/dao/HelpTransactionRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class HelpTransactionRepository {
3333
*
3434
* @param transaction The transaction that should be inserted.
3535
* @return The inserted {@link HelpTransaction}.
36-
* @throws SQLException If an error occurs.
36+
* @throws DataAccessException If an error occurs.
3737
*/
3838
public HelpTransaction save(HelpTransaction transaction) throws DataAccessException {
3939
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
@@ -56,7 +56,7 @@ public HelpTransaction save(HelpTransaction transaction) throws DataAccessExcept
5656
*
5757
* @param id The transaction's id.
5858
* @return A {@link HelpTransaction} object.
59-
* @throws SQLException If an error occurs.
59+
* @throws DataAccessException If an error occurs.
6060
*/
6161
public Optional<HelpTransaction> getTransaction(long id) throws DataAccessException {
6262
try {
@@ -72,7 +72,7 @@ public Optional<HelpTransaction> getTransaction(long id) throws DataAccessExcept
7272
* @param userId The user's id.
7373
* @param count The count of transactions that should be retrieved.
7474
* @return A List with all {@link HelpTransaction}s.
75-
* @throws SQLException If an error occurs.
75+
* @throws DataAccessException If an error occurs.
7676
*/
7777
public List<HelpTransaction> getTransactions(long userId, int count) throws DataAccessException {
7878
return jdbcTemplate.query("SELECT * FROM help_transaction WHERE recipient = ? ORDER BY created_at DESC LIMIT ?", (rs, rowNumber) -> this.read(rs), userId, count);

src/main/java/net/discordjug/javabot/systems/moderation/PurgeCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ protected ReplyCallbackAction handleModerationCommand(@NotNull SlashCommandInter
7474
boolean archive = event.getOption("archive", true, OptionMapping::getAsBoolean);
7575

7676
ModerationConfig config = botConfig.get(event.getGuild()).getModerationConfig();
77-
Long amount = (amountOption == null) ? 1 : amountOption.getAsLong();
77+
long amount = (amountOption == null) ? 1 : amountOption.getAsLong();
7878
User user = (userOption == null) ? null : userOption.getAsUser();
7979
int maxAmount = config.getPurgeMaxMessageCount();
80-
if (amount == null || amount > maxAmount) {
80+
if (amount < 1 || amount > maxAmount) {
8181
return Responses.warning(event, "Invalid amount. Should be between 1 and " + maxAmount + ", inclusive.");
8282
}
8383
if (amount == 0) {

src/main/java/net/discordjug/javabot/systems/staff_commands/suggestions/AcceptSuggestionSubcommand.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.discordjug.javabot.systems.staff_commands.suggestions;
22

33
import net.discordjug.javabot.data.config.BotConfig;
4-
import net.discordjug.javabot.data.config.GuildConfig;
54
import net.discordjug.javabot.util.Responses;
65
import net.discordjug.javabot.util.UserUtils;
76
import net.dv8tion.jda.api.EmbedBuilder;
@@ -31,17 +30,17 @@ public AcceptSuggestionSubcommand(BotConfig botConfig) {
3130
}
3231

3332
@Override
34-
protected WebhookMessageCreateAction<Message> handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message, GuildConfig config) {
33+
protected WebhookMessageCreateAction<Message> handleSuggestionCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Message message) {
3534
MessageEmbed embed = message.getEmbeds().get(0);
36-
MessageEmbed declineEmbed = buildSuggestionAcceptEmbed(event.getUser(), embed, config);
35+
MessageEmbed declineEmbed = buildSuggestionAcceptEmbed(event.getUser(), embed);
3736
message.editMessageEmbeds(declineEmbed).queue(
3837
edit -> edit.addReaction(botConfig.getSystems().getEmojiConfig().getSuccessEmote(event.getJDA())).queue(),
3938
error -> Responses.error(event.getHook(), error.getMessage()).queue());
4039
return Responses.success(event.getHook(), "Suggestion Accepted", "Successfully accepted suggestion with id `%s`", message.getId())
4140
.setComponents(getJumpButton(message));
4241
}
4342

44-
private @NotNull MessageEmbed buildSuggestionAcceptEmbed(@NotNull User user, @NotNull MessageEmbed embed, @NotNull GuildConfig config) {
43+
private @NotNull MessageEmbed buildSuggestionAcceptEmbed(@NotNull User user, @NotNull MessageEmbed embed) {
4544
return new EmbedBuilder()
4645
.setColor(Responses.Type.SUCCESS.getColor())
4746
.setAuthor(embed.getAuthor().getName(), embed.getAuthor().getUrl(), embed.getAuthor().getIconUrl())

0 commit comments

Comments
 (0)