Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;

import javax.sql.DataSource;
Expand Down Expand Up @@ -51,9 +51,9 @@ public void execute(SlashCommandInteractionEvent event) {
boolean includeData = event.getOption("include-data", false, OptionMapping::getAsBoolean);
event.deferReply(false).queue();
asyncPool.submit(() -> {
try (Connection con = dataSource.getConnection()) {
PreparedStatement stmt = con.prepareStatement(String.format("SCRIPT %s TO '%s';", includeData ? "" : "NODATA", SCHEMA_FILE));
boolean success = stmt.execute();
try (Connection con = dataSource.getConnection();
Statement stmt = con.createStatement()) {
boolean success = stmt.execute(String.format("SCRIPT %s TO '%s';", includeData ? "" : "NODATA", SCHEMA_FILE));
if (!success) {
event.getHook().sendMessage("Exporting the schema was not successful.").queue();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;

import javax.sql.DataSource;
Expand Down Expand Up @@ -71,9 +71,9 @@ public void execute(SlashCommandInteractionEvent event) {
}
event.deferReply(false).queue();
asyncPool.submit(() -> {
try (Connection con = dataSource.getConnection()) {
PreparedStatement stmt = con.prepareStatement(String.format("SCRIPT %s TO '%s' TABLE %s;", includeData ? "COLUMNS" : "NODATA", TABLE_FILE, tableOption.getAsString()));
boolean success = stmt.execute();
try (Connection con = dataSource.getConnection();
Statement stmt = con.createStatement()) {
boolean success = stmt.execute(String.format("SCRIPT %s TO '%s' TABLE %s;", includeData ? "COLUMNS" : "NODATA", TABLE_FILE, tableOption.getAsString()));
if (!success) {
event.getHook().sendMessage("Exporting the table was not successful.").queue();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private MessageEmbed buildInfoEmbed(GuildConfig config, User author) {
.setTitle("Message Cache Info")
.setColor(Responses.Type.DEFAULT.getColor())
.addField("Table Size", dbActions.getLogicalSize("message_cache") + " bytes", false)
.addField("Message Count", String.valueOf(messageCache.messageCount), true)
.addField("Message Count", String.valueOf(messageCache.getMessageCount()), true)
.addField("Cached (Memory)", String.format("%s/%s (%.2f%%)", messageCache.cache.size(), maxMessages, ((float) messageCache.cache.size() / maxMessages) * 100), true)
.addField("Cached (Database)", String.format("%s/%s (%.2f%%)", messages, maxMessages, ((float) messages / maxMessages) * 100), true)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.discordjug.javabot.data.h2db.message_cache;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.data.config.guild.MessageCacheConfig;
Expand Down Expand Up @@ -48,7 +49,8 @@ public class MessageCache {
* If a certain threshold is reached, messages will be synchronized to reduce the chances of loosing
* messages during an unexpected shutdown.
*/
public int messageCount = 0;
@Getter
private int messageCount = 0;

private final ExecutorService asyncPool;
private final BotConfig botConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ public WebhookMessageCreateAction<?> handleSubmission(@NotNull ButtonInteraction
thread.sendMessage(member.getAsMention())
.setEmbeds(buildSubmissionThreadEmbed(event.getUser(), questionOptional.get(), config))
.setComponents(ActionRow.of(Button.danger("qotw-submission:delete", "Delete Submission")))
.queue(s -> {
}, err -> ExceptionLogger.capture(err, getClass().getSimpleName()));
QOTWSubmission submission = new QOTWSubmission(thread);
submission.setAuthor(member.getUser());
.queue(s -> {},
err -> ExceptionLogger.capture(err, getClass().getSimpleName()));
} else {
thread.sendMessage("Could not retrieve current QOTW Question. Please contact an Administrator if you think that this is a mistake.")
.queue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public void execute(SlashCommandInteractionEvent event) {
//CHECKSTYLE:OFF This is a handler for all sort of failures that could possibly happen
}catch (RuntimeException e) {
//CHECKSTYLE:ON
Responses.error(event, "Task failed with an exception", e.getClass().getName() + (e.getMessage() == null ? "" : ": "+e.getMessage()));
Responses.error(event,
"Task failed with an exception: %s",
e.getClass().getName() + (e.getMessage() == null ? "" : ": "+e.getMessage()))
.queue();
}
}, () -> {
Responses.error(event, "Cannot find task `%s`", name).queue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
handleCustomTagsSubcommand(event).queue();
} catch (SQLException e) {
ExceptionLogger.capture(e, getClass().getSimpleName());
Responses.error(event, "An error occurred while executing this command.");
Responses.error(event, "An error occurred while executing this command.").queue();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,6 @@ private static boolean isEscaped(StringBuilder builder, int index) {
for (--index;index > 0 && index < builder.length() && builder.charAt(index) == '\\'; index--) {
numberOfCharacters++;
}
return numberOfCharacters % 2 == 1;
return numberOfCharacters % 2 != 0;
}
}
4 changes: 0 additions & 4 deletions src/main/java/net/discordjug/javabot/util/Plotter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Creates diagrams.
Expand Down Expand Up @@ -75,8 +73,6 @@ private void plotEntries(Graphics2D g2d, int x, int y, int width, int height) {
Bar bar = entry.second();
int totalBarHeight = 0;

Map<String, Color> colors = new HashMap<>();

double barSum = 0;

for (Pair<Color, Double> barSquares : bar.elements()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ private StringResourceCache() {
* @return The resources' content as a String.
*/
public static String load(String resourceName) {
String sql = CACHE.get(resourceName);
if (sql == null) {
InputStream is = StringResourceCache.class.getResourceAsStream(resourceName);
if (is == null) throw new RuntimeException("Could not load " + resourceName);
try {
sql = new String(is.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
String content = CACHE.get(resourceName);
if (content == null) {
try(InputStream is = StringResourceCache.class.getResourceAsStream(resourceName)){
if (is == null) throw new RuntimeException("Could not load " + resourceName);
content = new String(is.readAllBytes(), StandardCharsets.UTF_8);
}catch (IOException e) {
ExceptionLogger.capture(e, StringResourceCache.class.getSimpleName());
throw new UncheckedIOException(e);
}
CACHE.put(resourceName, sql);

CACHE.put(resourceName, content);
}
return sql;
return content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,29 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Test for the {@link IndentationHelper} class.
*/
public class IndentationHelperTest {
/**
* Tests the {@link IndentationHelper#formatIndentation(String, IndentationHelper.IndentationType)} method.
* @throws IOException if any I/O error occurs indicating an issue with the test
*/
@Test
public void testFormatIndentation() {
public void testFormatIndentation() throws IOException {
String[] unformatted = null;
String[] formatted = null;
try {
unformatted = Files.readString(Path.of(IndentationHelper.class.getResource("/Unformatted Strings.txt").toURI())).split("----");
formatted = Files.readString(Path.of(IndentationHelper.class.getResource("/Formatted Strings.txt").toURI())).split("----");
} catch (NullPointerException | URISyntaxException e) {
fail("Files to run the test not present", e);
} catch (IOException e) {
e.printStackTrace();
}
unformatted = StringResourceCache.load("/Unformatted Strings.txt").split("----");
formatted = StringResourceCache.load("/Formatted Strings.txt").split("----");

for (int i = 0, k = 0; i < unformatted.length; i++) {
assertEquals(formatted[k++], IndentationHelper.formatIndentation(unformatted[i], IndentationHelper.IndentationType.FOUR_SPACES), "Method failed to format a text with four spaces correctly");
assertEquals(formatted[k++], IndentationHelper.formatIndentation(unformatted[i], IndentationHelper.IndentationType.TWO_SPACES), "Method failed to format a text with two spaces correctly");
assertEquals(formatted[k++], IndentationHelper.formatIndentation(unformatted[i], IndentationHelper.IndentationType.TABS), "Method failed to format a text with tabs correctly.");
assertEquals(formatted[k++], IndentationHelper.formatIndentation(unformatted[i], IndentationHelper.IndentationType.NULL), "Method returned a String not matching the input");
}

}
}