diff --git a/src/main/java/xyz/vallat/louis/commands/Version.java b/src/main/java/xyz/vallat/louis/commands/Version.java index e3f29ae..7c131cb 100644 --- a/src/main/java/xyz/vallat/louis/commands/Version.java +++ b/src/main/java/xyz/vallat/louis/commands/Version.java @@ -1,9 +1,12 @@ package xyz.vallat.louis.commands; import discord4j.core.event.domain.message.MessageCreateEvent; +import discord4j.core.object.entity.Guild; import discord4j.rest.util.Color; import reactor.core.publisher.Mono; import xyz.vallat.louis.MovieQuoteBot; +import xyz.vallat.louis.database.SubtitleLineManager; +import xyz.vallat.louis.database.SubtitleManager; import xyz.vallat.louis.discord.DiscordManager; public class Version extends Command { @@ -16,12 +19,27 @@ public class Version extends Command { public Mono execute(MessageCreateEvent event) { return event.getMessage().getChannel() .flatMap(channel -> channel - .createEmbed(embedCreateSpec -> embedCreateSpec - .setColor(Color.BISMARK) - .setTitle(MovieQuoteBot.NAME) - .setDescription(MovieQuoteBot.DESCRIPTION) - .addField("Version", MovieQuoteBot.VERSION, true) - .addField("Guilds", String.valueOf(DiscordManager.getGuilds().block()), true) + .createEmbed(embedCreateSpec -> { + embedCreateSpec + .setColor(Color.BISMARK) + .setTitle(MovieQuoteBot.NAME) + .setDescription(MovieQuoteBot.DESCRIPTION) + .addField("Guilds", String.valueOf(DiscordManager.getGuilds().block()), true) + .addField("Subtitles imported", String.valueOf(SubtitleManager.getNumberOfSubtitles()), true) + .addField("Lines imported", String.valueOf(SubtitleLineManager.getNumberOfSubtitleLines()), true); + if (event.getGuildId().isPresent()) { + Guild guild = event.getGuild().block(); + if (guild != null) + embedCreateSpec.addField("This guild imported", + SubtitleLineManager + .getNumberOfSubtitleLinesByGuild(guild.getId().asString()) + + " subtitles, from " + + SubtitleManager + .getNumberOfSubtitlesByGuild(guild.getId().asString()) + " subtitles.", + true); + } + embedCreateSpec.addField("Version", MovieQuoteBot.VERSION, true); + } ) ) .then(); diff --git a/src/main/java/xyz/vallat/louis/database/DBManager.java b/src/main/java/xyz/vallat/louis/database/DBManager.java index 6616abd..ed4cd45 100644 --- a/src/main/java/xyz/vallat/louis/database/DBManager.java +++ b/src/main/java/xyz/vallat/louis/database/DBManager.java @@ -9,13 +9,13 @@ import xyz.vallat.louis.env.EnvironmentVariables; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; -import java.sql.Statement; import static xyz.vallat.louis.database.FilmManager.initializeFilm; import static xyz.vallat.louis.database.LanguageManager.importLanguageIfNeeded; import static xyz.vallat.louis.database.LanguageManager.initializeLanguages; import static xyz.vallat.louis.database.PropertyManager.initializeProperties; import static xyz.vallat.louis.database.SubtitleLineManager.initializeSubtitleLine; +import static xyz.vallat.louis.database.SubtitleManager.initializeSubtitle; public final class DBManager { @@ -68,27 +68,4 @@ public final class DBManager { private static void updateDatabaseIfNeeded() { PropertyManager.saveProperty(VERSION_PROPERTY, MovieQuoteBot.VERSION); } - - private static void initializeSubtitle(Connection connection) throws SQLException { - logger.debug("Creating subtitle table."); - try (Statement stmt = connection.createStatement()) { - String query = """ - CREATE TABLE IF NOT EXISTS subtitles - ( - id int GENERATED ALWAYS AS IDENTITY, - film_id int NOT NULL, - language_id int NOT NULL, - importer varchar(37), - importer_guild_id text, - imported_date timestamptz NOT NULL DEFAULT now(), - UNIQUE (film_id, language_id), - PRIMARY KEY (id), - FOREIGN KEY (film_id) - REFERENCES films (id), - FOREIGN KEY (language_id) - REFERENCES languages (id) - );"""; - stmt.executeUpdate(query); - } - } } diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java index 4afb9b2..cc2f1af 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java @@ -2,8 +2,10 @@ package xyz.vallat.louis.database; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import xyz.vallat.louis.codes.ExitCodes; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; @@ -14,6 +16,41 @@ public final class SubtitleLineManager { private SubtitleLineManager() { } + public static int getNumberOfSubtitleLines() { + logger.debug("Getting the number of subtitle lines in database."); + try (Connection connection = DBManager.getConnection()) { + try (Statement stmt = connection.createStatement()) { + String query = "SELECT COUNT(*) FROM subtitle_lines;"; + stmt.executeQuery(query); + stmt.getResultSet().next(); + return (stmt.getResultSet().getInt(1)); + } + } catch (SQLException e) { + logger.error("Could not connect to the database right now. Reason: {}", e.getMessage()); + System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); + } + return -1; + } + + public static int getNumberOfSubtitleLinesByGuild(String guild_id) { + logger.debug("Getting the number of subtitle lines in database from guild '{}'.", guild_id); + try (Connection connection = DBManager.getConnection()) { + String query = "SELECT COUNT(*) FROM subtitle_lines " + + "INNER JOIN subtitles ON subtitles.id = subtitle_lines.subtitle_id " + + "WHERE importer_guild_id = ?;"; + try (PreparedStatement stmt = connection.prepareStatement(query)) { + stmt.setString(1, guild_id); + stmt.executeQuery(); + stmt.getResultSet().next(); + return (stmt.getResultSet().getInt(1)); + } + } catch (SQLException e) { + logger.error("Could not connect to the database right now. Reason: {}", e.getMessage()); + System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); + } + return -1; + } + static void initializeSubtitleLine(Connection connection) throws SQLException { logger.debug("Creating subtitle_line table."); try (Statement stmt = connection.createStatement()) { diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleManager.java b/src/main/java/xyz/vallat/louis/database/SubtitleManager.java new file mode 100644 index 0000000..7050817 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/database/SubtitleManager.java @@ -0,0 +1,76 @@ +package xyz.vallat.louis.database; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import xyz.vallat.louis.codes.ExitCodes; + +import java.nio.charset.StandardCharsets; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; + +public class SubtitleManager { + + private static final Logger logger = LoggerFactory.getLogger(SubtitleManager.class.getCanonicalName()); + + private SubtitleManager() { + } + + public static int getNumberOfSubtitles() { + logger.debug("Getting the number of subtitles in database."); + try (Connection connection = DBManager.getConnection()) { + try (Statement stmt = connection.createStatement()) { + String query = "SELECT COUNT(*) FROM subtitles;"; + stmt.executeQuery(query); + stmt.getResultSet().next(); + return (stmt.getResultSet().getInt(1)); + } + } catch (SQLException e) { + logger.error("Could not connect to the database right now. Reason: {}", e.getMessage()); + System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); + } + return -1; + } + + public static int getNumberOfSubtitlesByGuild(String guild_id) { + logger.debug("Getting the number of subtitles in database from guild '{}'.", guild_id); + try (Connection connection = DBManager.getConnection()) { + String query = "SELECT COUNT(*) FROM subtitles " + + "WHERE importer_guild_id = ?;"; + try (PreparedStatement stmt = connection.prepareStatement(query)) { + stmt.setString(1, guild_id); + stmt.executeQuery(); + stmt.getResultSet().next(); + return (stmt.getResultSet().getInt(1)); + } + } catch (SQLException e) { + logger.error("Could not connect to the database right now. Reason: {}", e.getMessage()); + System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); + } + return -1; + } + + static void initializeSubtitle(Connection connection) throws SQLException { + logger.debug("Creating subtitle table."); + try (Statement stmt = connection.createStatement()) { + String query = """ + CREATE TABLE IF NOT EXISTS subtitles + ( + id int GENERATED ALWAYS AS IDENTITY, + film_id int NOT NULL, + language_id int NOT NULL, + importer varchar(37), + importer_guild_id text, + imported_date timestamptz NOT NULL DEFAULT now(), + UNIQUE (film_id, language_id), + PRIMARY KEY (id), + FOREIGN KEY (film_id) + REFERENCES films (id), + FOREIGN KEY (language_id) + REFERENCES languages (id) + );"""; + stmt.executeUpdate(query); + } + } +}