cleanup and reformat
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-09-05 00:06:35 +02:00
parent 14b54501fd
commit fd2970fa59
81 changed files with 1245 additions and 766 deletions

View File

@@ -14,12 +14,20 @@ public enum ConfigurationEntry
private String path;
private Object defaultValue;
ConfigurationEntry(String path, Object defaultValue)
{
this.path = path;
this.defaultValue = defaultValue;
}
public String getPath() { return path; }
public Object getDefaultValue() { return defaultValue; }
public String getPath()
{
return path;
}
public Object getDefaultValue()
{
return defaultValue;
}
}

View File

@@ -32,12 +32,12 @@ public class ConfigurationSource
* we used to have a config.yml file in the "resources" folder, but that is no longer necessary.
*/
LinkedHashMap<String, Object> internalConfigContents = new LinkedHashMap<>(); // map holding all file entries
for(ConfigurationEntry entry : ConfigurationEntry.values())
for (ConfigurationEntry entry : ConfigurationEntry.values())
{
internalConfigContents.put(entry.getPath(), entry.getDefaultValue());
}
if(internalConfigContents.isEmpty())
if (internalConfigContents.isEmpty())
{
LOGGER.error("Error reading internal configuration!");
HidekoBot.shutdown();
@@ -46,18 +46,19 @@ public class ConfigurationSource
// check if config files exists in filesystem
File fsConfigFile = new File(configFilePath);
if(!fsConfigFile.exists())
if (!fsConfigFile.exists())
{
// try to create config file
try {
if(!fsConfigFile.createNewFile())
try
{
if (!fsConfigFile.createNewFile())
{
LOGGER.error("We tried creating a file that already exists!");
HidekoBot.shutdown();
return;
}
}
catch (IOException e) {
} catch (IOException e)
{
LOGGER.error("Error creating configuration file!", e);
HidekoBot.shutdown();
return;
@@ -68,11 +69,15 @@ public class ConfigurationSource
Yaml fsConfigYaml = new Yaml(new SafeConstructor(options));
LinkedHashMap<String, Object> fsConfigContents = null; // map holding all file entries
try (InputStream fsConfigStream = new FileInputStream(fsConfigFile))
{ fsConfigContents = fsConfigYaml.load(fsConfigStream); }
catch (IOException e) { LOGGER.error(e.getMessage()); }
{
fsConfigContents = fsConfigYaml.load(fsConfigStream);
} catch (IOException e)
{
LOGGER.error(e.getMessage());
}
if(fsConfigContents == null) // if file contents are empty or corrupted...
if (fsConfigContents == null) // if file contents are empty or corrupted...
{
// "clean" them (this effectively forces a config file reset)
fsConfigContents = new LinkedHashMap<>();
@@ -80,10 +85,10 @@ public class ConfigurationSource
// check for missing keys
boolean missingKeys = false;
for(String key : internalConfigContents.keySet())
for (String key : internalConfigContents.keySet())
{
// if key is missing
if(!fsConfigContents.containsKey(key))
if (!fsConfigContents.containsKey(key))
{
// quit and flag it, as we need to complete the file with the missing ones
missingKeys = true;
@@ -92,27 +97,29 @@ public class ConfigurationSource
}
// if keys are missing
if(missingKeys)
if (missingKeys)
{
// create a new mixed map that will take existing values from the non-missing keys
// and fill everything else with the default values
LinkedHashMap<String, Object> filledEntries = new LinkedHashMap<>();
for(Map.Entry<String, Object> entry : internalConfigContents.entrySet())
for (Map.Entry<String, Object> entry : internalConfigContents.entrySet())
{
String key = entry.getKey();
if(fsConfigContents.containsKey(key))
if (fsConfigContents.containsKey(key))
{
// if the key already exists, copy the original value
filledEntries.put(key, fsConfigContents.get(key));
} else {
} else
{
// else, copy the value from the example config file
filledEntries.put(key, entry.getValue());
}
}
try {
try
{
// new writer to actually write the contents to the file
PrintWriter missingKeysWriter = new PrintWriter(fsConfigFile);
@@ -125,7 +132,8 @@ public class ConfigurationSource
// create the yaml object and dump the values to filesystem
Yaml yaml = new Yaml(dumperOptions);
yaml.dump(filledEntries, missingKeysWriter);
} catch (FileNotFoundException e) {
} catch (FileNotFoundException e)
{
LOGGER.error(e.getMessage());
HidekoBot.shutdown();
return;
@@ -133,7 +141,8 @@ public class ConfigurationSource
// finally, dump all entries to cache.
loadConfig(filledEntries);
} else {
} else
{
// if no key is missing, just cache all entries and values from filesystem.
loadConfig(fsConfigContents);
}
@@ -144,6 +153,7 @@ public class ConfigurationSource
{
this.configurationEntries.putAll(configurationEntries);
}
public Object getConfigValue(ConfigurationEntry key)
{
return configurationEntries.get(key.getPath());

View File

@@ -12,31 +12,37 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class DatabaseSource {
public class DatabaseSource
{
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DatabaseSource.class);
private static final String JDBC_URL = "jdbc:sqlite:%path%";
private Connection dbConnection = null;
private final String dbPath;
public DatabaseSource(String dbPath) {
public DatabaseSource(String dbPath)
{
this.dbPath = dbPath;
}
private void logException(SQLException e) {
private void logException(SQLException e)
{
LOGGER.error("Database Exception", e);
}
public boolean connect() {
public boolean connect()
{
String url = JDBC_URL.replace("%path%", dbPath);
if (!close()) return false;
try {
try
{
dbConnection = DriverManager.getConnection(url);
LOGGER.info("Database connection established!");
return true;
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
return false;
}
@@ -44,13 +50,18 @@ public class DatabaseSource {
}
public boolean close() {
if (dbConnection != null) {
try {
if (!dbConnection.isClosed()) {
public boolean close()
{
if (dbConnection != null)
{
try
{
if (!dbConnection.isClosed())
{
dbConnection.close();
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
return false;
}
@@ -87,43 +98,47 @@ public class DatabaseSource {
*/
//todo: javadocs
public boolean initDb() {
public boolean initDb()
{
List<String> newTables = new ArrayList<>();
newTables.add("""
CREATE TABLE IF NOT EXISTS pending_disabled_messages (
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
message_id TEXT NOT NULL,
expiry_timestamp TEXT NOT NULL);
""");
CREATE TABLE IF NOT EXISTS pending_disabled_messages (
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
message_id TEXT NOT NULL,
expiry_timestamp TEXT NOT NULL);
""");
newTables.add("""
CREATE TABLE IF NOT EXISTS command_runners (
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
message_id TEXT NOT NULL,
user_id TEXT NOT NULL,
channel_type TEXT NOT NULL);
""");
CREATE TABLE IF NOT EXISTS command_runners (
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
message_id TEXT NOT NULL,
user_id TEXT NOT NULL,
channel_type TEXT NOT NULL);
""");
newTables.add("""
CREATE TABLE IF NOT EXISTS urban_dictionary (
message_id TEXT NOT NULL,
page INTEGER NOT NULL,
meanings TEXT NOT NULL,
examples TEXT NOT NULL,
contributors TEXT NOT NULL,
dates TEXT NOT NULL,
term TEXT NOT NULL
);
""");
CREATE TABLE IF NOT EXISTS urban_dictionary (
message_id TEXT NOT NULL,
page INTEGER NOT NULL,
meanings TEXT NOT NULL,
examples TEXT NOT NULL,
contributors TEXT NOT NULL,
dates TEXT NOT NULL,
term TEXT NOT NULL
);
""");
for (String sql : newTables) {
try (Statement stmt = dbConnection.createStatement()) {
for (String sql : newTables)
{
try (Statement stmt = dbConnection.createStatement())
{
// execute the statement
stmt.execute(sql);
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
return false;
}
@@ -132,14 +147,17 @@ public class DatabaseSource {
return true;
}
public boolean trackRanCommandReply(Message message, User user) {
public boolean trackRanCommandReply(Message message, User user)
{
String userId = user.getId();
String guildId;
ChannelType channelType = message.getChannelType();
if (!(channelType.isGuild())) {
if (!(channelType.isGuild()))
{
guildId = userId;
} else {
} else
{
guildId = message.getGuild().getId();
}
@@ -153,7 +171,8 @@ public class DatabaseSource {
(?, ?, ?, ?, ?);
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, guildId);
preparedStatement.setString(2, channelId);
preparedStatement.setString(3, messageId);
@@ -163,36 +182,42 @@ public class DatabaseSource {
preparedStatement.executeUpdate();
return true;
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return false;
}
public boolean isUserTrackedFor(String userId, String messageId) {
public boolean isUserTrackedFor(String userId, String messageId)
{
String trackedUserId = getTrackedReplyUserId(messageId);
if (trackedUserId == null) return false;
return userId.equals(trackedUserId);
}
public ChannelType getTrackedMessageChannelType(String messageId) {
public ChannelType getTrackedMessageChannelType(String messageId)
{
String query = """
SELECT channel_type
FROM command_runners
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
String channelTypeName = resultSet.getString("channel_type");
return ChannelType.valueOf(channelTypeName);
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
@@ -200,37 +225,44 @@ public class DatabaseSource {
}
public String getTrackedReplyUserId(String messageId) {
public String getTrackedReplyUserId(String messageId)
{
String query = """
SELECT user_id
FROM command_runners
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("user_id");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return null;
}
public boolean queueDisabling(Message message) {
public boolean queueDisabling(Message message)
{
String messageId = message.getId();
String channelId = message.getChannel().getId();
String guildId;
ChannelType channelType = message.getChannelType();
if (!(channelType.isGuild())) {
if (!(channelType.isGuild()))
{
guildId = "PRIVATE";
} else {
} else
{
guildId = message.getGuild().getId();
}
@@ -245,7 +277,8 @@ public class DatabaseSource {
(?, ?, ?, ?);
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, guildId);
preparedStatement.setString(2, channelId);
preparedStatement.setString(3, messageId);
@@ -254,14 +287,16 @@ public class DatabaseSource {
preparedStatement.executeUpdate();
return true;
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return false;
}
public List<String> getQueuedExpiringMessages() {
public List<String> getQueuedExpiringMessages()
{
List<String> messages = new ArrayList<>();
String query = """
@@ -269,44 +304,54 @@ public class DatabaseSource {
FROM pending_disabled_messages;
""";
try (Statement statement = dbConnection.createStatement()) {
try (Statement statement = dbConnection.createStatement())
{
ResultSet resultSet = statement.executeQuery(query);
if (resultSet.isClosed()) return messages;
while (resultSet.next()) {
while (resultSet.next())
{
messages.add(resultSet.getString("message_id"));
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return messages;
}
public boolean untrackExpiredMessage(String messageId) {
public boolean untrackExpiredMessage(String messageId)
{
String query = "DELETE FROM pending_disabled_messages WHERE message_id = ?;";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
preparedStatement.execute();
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
return false;
}
query = "DELETE FROM command_runners WHERE message_id = ?;";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
preparedStatement.execute();
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
return false;
}
query = "DELETE FROM urban_dictionary WHERE message_id = ?;";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
preparedStatement.execute();
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
return false;
}
@@ -314,66 +359,78 @@ public class DatabaseSource {
return true;
}
public String getQueuedExpiringMessageExpiryDate(String messageId) {
public String getQueuedExpiringMessageExpiryDate(String messageId)
{
String query = """
SELECT expiry_timestamp
FROM pending_disabled_messages
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("expiry_timestamp");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return null;
}
public String getQueuedExpiringMessageChannel(String messageId) {
public String getQueuedExpiringMessageChannel(String messageId)
{
String query = """
SELECT channel_id
FROM pending_disabled_messages
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("channel_id");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return null;
}
public String getQueuedExpiringMessageGuild(String messageId) {
public String getQueuedExpiringMessageGuild(String messageId)
{
String query = """
SELECT guild_id
FROM pending_disabled_messages
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("guild_id");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
@@ -382,7 +439,8 @@ public class DatabaseSource {
public boolean trackUrban(String meanings, String examples,
String contributors, String dates,
Message message, String term) {
Message message, String term)
{
String query = """
INSERT INTO urban_dictionary
@@ -390,7 +448,8 @@ public class DatabaseSource {
(?, ?, ?, ?, ?, ?, ?);
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, message.getId());
preparedStatement.setInt(2, 0);
preparedStatement.setString(3, meanings);
@@ -402,167 +461,196 @@ public class DatabaseSource {
preparedStatement.executeUpdate();
return true;
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return false;
}
public int getUrbanPage(String messageId) {
public int getUrbanPage(String messageId)
{
String query = """
SELECT page
FROM urban_dictionary
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return 0;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getInt("page");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return 0;
}
public String getUrbanMeanings(String messageId) {
public String getUrbanMeanings(String messageId)
{
String query = """
SELECT meanings
FROM urban_dictionary
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("meanings");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return null;
}
public String getUrbanExamples(String messageId) {
public String getUrbanExamples(String messageId)
{
String query = """
SELECT examples
FROM urban_dictionary
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("examples");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return null;
}
public String getUrbanContributors(String messageId) {
public String getUrbanContributors(String messageId)
{
String query = """
SELECT contributors
FROM urban_dictionary
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("contributors");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return null;
}
public String getUrbanDates(String messageId) {
public String getUrbanDates(String messageId)
{
String query = """
SELECT dates
FROM urban_dictionary
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("dates");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return null;
}
public String getUrbanTerm(String messageId) {
public String getUrbanTerm(String messageId)
{
String query = """
SELECT term
FROM urban_dictionary
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
while (resultSet.next())
{
return resultSet.getString("term");
}
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return null;
}
public boolean setUrbanPage(String messageId, int page) {
public boolean setUrbanPage(String messageId, int page)
{
String query = """
UPDATE urban_dictionary
SET page = ?
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setInt(1, page);
preparedStatement.setString(2, messageId);
preparedStatement.executeUpdate();
return true;
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}
return false;
}
public boolean resetExpiryTimestamp(String messageId) {
public boolean resetExpiryTimestamp(String messageId)
{
LocalDateTime expiryTime = LocalDateTime.now().plusSeconds(Cache.getExpiryTimeSeconds());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(Cache.getExpiryTimestampFormat());
@@ -574,14 +662,16 @@ public class DatabaseSource {
WHERE message_id = ?;
""";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
preparedStatement.setString(1, expiryTimeFormatted);
preparedStatement.setString(2, messageId);
preparedStatement.executeUpdate();
return true;
} catch (SQLException e) {
} catch (SQLException e)
{
logException(e);
}

View File

@@ -25,8 +25,8 @@ public class PropertiesSource
{
properties.load(internalPropertiesStream);
}
catch (IOException e) {
} catch (IOException e)
{
LOGGER.error(e.getMessage());
HidekoBot.shutdown();
return;
@@ -34,5 +34,7 @@ public class PropertiesSource
}
public String getProperty(String property)
{ return properties == null ? "" : properties.getProperty(property); }
{
return properties == null ? "" : properties.getProperty(property);
}
}