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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.egg.papertrail</groupId>
<artifactId>paper-trail-bot</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>PaperTrail</name>
<description>A simple bot for logging all user actions</description>
<properties>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/papertrail/cleanup/BotKickListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.jetbrains.annotations.NotNull;
import org.papertrail.database.DatabaseConnector;
import org.papertrail.database.TableNames;
import org.papertrail.database.Schema;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
Expand All @@ -28,8 +28,8 @@ public void onGuildLeave(@NotNull GuildLeaveEvent event) {

Guild leftGuild = event.getGuild();
vThreadPool.execute(()->{
dc.getGuildDataAccess().unregister(leftGuild.getId(), TableNames.AUDIT_LOG_TABLE);
dc.getGuildDataAccess().unregister(leftGuild.getId(), TableNames.MESSAGE_LOG_REGISTRATION_TABLE);
dc.getGuildDataAccess().unregister(leftGuild.getId(), Schema.AUDIT_LOG_TABLE);
dc.getGuildDataAccess().unregister(leftGuild.getId(), Schema.MESSAGE_LOG_REGISTRATION_TABLE);
});

}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/papertrail/database/DatabaseConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import com.zaxxer.hikari.HikariDataSource;

import static org.papertrail.database.Schema.initializeSchema;


public class DatabaseConnector {

private static final String DB_URL = EnvConfig.get("DATABASEURL");
Expand All @@ -17,6 +20,7 @@ public class DatabaseConnector {
public DatabaseConnector() {
initializeDataSource();
this.dsl = DSL.using(dataSource, SQLDialect.POSTGRES);
initializeSchema(dsl);
}

public GuildDataAccess getGuildDataAccess() {
Expand All @@ -37,7 +41,8 @@ private static synchronized void initializeDataSource() {
dataSource.setConnectionTimeout(30000); // 30 seconds
dataSource.setIdleTimeout(600000); // 10 minutes
dataSource.setMaxLifetime(1800000); // 30 minutes
dataSource.setPoolName("PaperTrailPool");
dataSource.setPoolName("PaperTrailPool");
dataSource.setConnectionInitSql("SET TIME ZONE 'UTC'");
}
// Ensure the data source is closed on shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/papertrail/database/GuildDataAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.selectOne;
import static org.jooq.impl.DSL.table;
import static org.papertrail.database.Schema.CHANNEL_ID_COLUMN;
import static org.papertrail.database.Schema.GUILD_ID_COLUMN;

public class GuildDataAccess {

private final DSLContext dsl;

private static final String GUILD_ID_COLUMN = "guild_id";
private static final String CHANNEL_ID_COLUMN = "channel_id";


public GuildDataAccess(DSLContext dsl) {
this.dsl = dsl;
}
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/org/papertrail/database/MessageDataAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.selectOne;
import static org.jooq.impl.DSL.table;
import static org.papertrail.database.Schema.AUTHOR_ID_COLUMN;
import static org.papertrail.database.Schema.MESSAGE_CONTENT_COLUMN;
import static org.papertrail.database.Schema.MESSAGE_ID_COLUMN;

public class MessageDataAccess {

private final DSLContext dsl;

private static final String MESSAGE_ID_COLUMN = "message_id";
private static final String MESSAGE_CONTENT_COLUMN = "message_content";
private static final String AUTHOR_ID_COLUMN = "author_id";

public MessageDataAccess (DSLContext dsl) {
this.dsl = dsl;
}

public void logMessage(String messageId, String messageContent, String authorId) {

dsl.insertInto(table(TableNames.MESSAGE_LOG_CONTENT_TABLE))
dsl.insertInto(table(Schema.MESSAGE_LOG_CONTENT_TABLE))
.columns(field(MESSAGE_ID_COLUMN), field(MESSAGE_CONTENT_COLUMN), field(AUTHOR_ID_COLUMN))
.values(Long.parseLong(messageId), MessageEncryption.encrypt(messageContent), Long.parseLong(authorId))
.onConflictDoNothing()
Expand All @@ -31,30 +30,30 @@ public void logMessage(String messageId, String messageContent, String authorId)
public AuthorAndMessageEntity retrieveAuthorAndMessage (String messageId) {

return dsl.select(field(AUTHOR_ID_COLUMN), field(MESSAGE_CONTENT_COLUMN))
.from(table(TableNames.MESSAGE_LOG_CONTENT_TABLE))
.from(table(Schema.MESSAGE_LOG_CONTENT_TABLE))
.where(field(MESSAGE_ID_COLUMN).eq(Long.parseLong(messageId)))
.fetchOne(r -> new AuthorAndMessageEntity(String.valueOf(r.get(field(AUTHOR_ID_COLUMN))), MessageEncryption.decrypt(String.valueOf(r.get(field(MESSAGE_CONTENT_COLUMN))))));
}

public boolean messageExists (String messageId) {

return dsl.fetchExists(
selectOne().from(table(TableNames.MESSAGE_LOG_CONTENT_TABLE))
selectOne().from(table(Schema.MESSAGE_LOG_CONTENT_TABLE))
.where(field(MESSAGE_ID_COLUMN).eq(Long.parseLong(messageId)))
);
}

public void updateMessage (String messageId, String messageContent) {

dsl.update(table(TableNames.MESSAGE_LOG_CONTENT_TABLE))
dsl.update(table(Schema.MESSAGE_LOG_CONTENT_TABLE))
.set(field(MESSAGE_CONTENT_COLUMN), MessageEncryption.encrypt(messageContent))
.where(field(MESSAGE_ID_COLUMN).eq(Long.parseLong(messageId)))
.execute();
}

public void deleteMessage (String messageId) {

dsl.deleteFrom(table(TableNames.MESSAGE_LOG_CONTENT_TABLE))
dsl.deleteFrom(table(Schema.MESSAGE_LOG_CONTENT_TABLE))
.where(field(MESSAGE_ID_COLUMN).eq(Long.parseLong(messageId)))
.execute();
}
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/org/papertrail/database/Schema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.papertrail.database;

import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;

import static org.jooq.impl.DSL.constraint;

public class Schema {

private Schema() {
throw new IllegalStateException("Utility Class");
}

public static final String SCHEMA_NAME = "public";

public static final String AUDIT_LOG_TABLE = "audit_log_table";
public static final String MESSAGE_LOG_REGISTRATION_TABLE = "message_log_registration_table";
// the following to column names are used in both tables: message_log_registration_table and audit_log_table
public static final String GUILD_ID_COLUMN = "guild_id";
public static final String CHANNEL_ID_COLUMN = "channel_id";

public static final String MESSAGE_LOG_CONTENT_TABLE = "message_log_content_table";
public static final String MESSAGE_ID_COLUMN = "message_id";
public static final String MESSAGE_CONTENT_COLUMN = "message_content";
public static final String AUTHOR_ID_COLUMN = "author_id";

public static void initializeSchema(DSLContext dsl) {

dsl.createSchemaIfNotExists(SCHEMA_NAME).execute();

// create audit log table
dsl.createTableIfNotExists(AUDIT_LOG_TABLE)
.column(GUILD_ID_COLUMN, SQLDataType.BIGINT.notNull())
.column(CHANNEL_ID_COLUMN, SQLDataType.BIGINT.notNull())
.constraints(
constraint(AUDIT_LOG_TABLE+"_pk").primaryKey(GUILD_ID_COLUMN),
constraint(AUDIT_LOG_TABLE+"_unique").unique(CHANNEL_ID_COLUMN)
)
.execute();

// create message log registration table
dsl.createTableIfNotExists(MESSAGE_LOG_REGISTRATION_TABLE)
.column(GUILD_ID_COLUMN, SQLDataType.BIGINT.notNull())
.column(CHANNEL_ID_COLUMN, SQLDataType.BIGINT.notNull())
.constraints(
constraint(MESSAGE_LOG_REGISTRATION_TABLE+"_pk").primaryKey(GUILD_ID_COLUMN),
constraint(MESSAGE_LOG_REGISTRATION_TABLE+"_unique").unique(CHANNEL_ID_COLUMN)
).execute();

// create message log content table
dsl.createTableIfNotExists(MESSAGE_LOG_CONTENT_TABLE)
.column(MESSAGE_ID_COLUMN, SQLDataType.BIGINT.notNull())
.column(MESSAGE_CONTENT_COLUMN, SQLDataType.CLOB.nullable(true))
.column(AUTHOR_ID_COLUMN, SQLDataType.BIGINT.notNull())
.column("created_at", SQLDataType.TIMESTAMP.default_(DSL.currentTimestamp()).notNull())
.constraints(
constraint(MESSAGE_LOG_CONTENT_TABLE+"pk").primaryKey(MESSAGE_ID_COLUMN)
)
.execute();
// create index on created_at column
dsl.createIndexIfNotExists(MESSAGE_LOG_CONTENT_TABLE+"_created_at_idx")
.on(MESSAGE_LOG_CONTENT_TABLE, "created_at")
.execute();

setupCronDeletion(dsl);
}

private static void setupCronDeletion(DSLContext dsl) {
dsl.query("CREATE EXTENSION IF NOT EXISTS pg_cron;").execute();

String cronScheduleQuery = String.format("""
SELECT cron.schedule(
'daily_log_cleanup',
'0 2 * * *', -- 2:00 AM UTC daily
$$DELETE FROM %s WHERE created_at < CURRENT_TIMESTAMP - INTERVAL '30 days';$$
);
""", MESSAGE_LOG_CONTENT_TABLE);

dsl.query(cronScheduleQuery).execute();

}
}
12 changes: 0 additions & 12 deletions src/main/java/org/papertrail/database/TableNames.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Objects;

import org.papertrail.database.DatabaseConnector;
import org.papertrail.database.TableNames;
import org.papertrail.database.Schema;
import org.papertrail.utilities.EnvConfig;
import org.papertrail.version.AuthorInfo;
import org.papertrail.version.ProjectInfo;
Expand Down Expand Up @@ -36,7 +36,7 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {

if(event.getName().equals("announcement")) {

List<String> registeredChannelList = dc.getGuildDataAccess().retrieveAllRegisteredChannels(TableNames.AUDIT_LOG_TABLE);
List<String> registeredChannelList = dc.getGuildDataAccess().retrieveAllRegisteredChannels(Schema.AUDIT_LOG_TABLE);

if(registeredChannelList.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Objects;

import org.papertrail.database.DatabaseConnector;
import org.papertrail.database.TableNames;
import org.papertrail.database.Schema;
import org.tinylog.Logger;

import net.dv8tion.jda.api.EmbedBuilder;
Expand Down Expand Up @@ -59,7 +59,7 @@ private void setAuditLogging(SlashCommandInteractionEvent event) {
String guildId = Objects.requireNonNull(event.getGuild()).getId();
// retrieve the previously registered channel_id associated with the given
// guild_id
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, TableNames.AUDIT_LOG_TABLE);
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, Schema.AUDIT_LOG_TABLE);

// if there is a registered channel_id in the database, send a warning message
// in the channel where the command was called from, stating that a channel has
Expand All @@ -83,7 +83,7 @@ private void setAuditLogging(SlashCommandInteractionEvent event) {
String channelIdToRegister = event.getChannel().asTextChannel().getId();
try {
// register the channel_id along with guild_id in the database
dc.getGuildDataAccess().registerGuildAndChannel(guildId, channelIdToRegister, TableNames.AUDIT_LOG_TABLE);
dc.getGuildDataAccess().registerGuildAndChannel(guildId, channelIdToRegister, Schema.AUDIT_LOG_TABLE);

EmbedBuilder eb = new EmbedBuilder();
eb.setTitle("📝 Audit Log Configuration");
Expand Down Expand Up @@ -118,7 +118,7 @@ private void retrieveAuditLoggingChannel(SlashCommandInteractionEvent event) {
String guildId = Objects.requireNonNull(event.getGuild()).getId();

// retrieve the channel_id registered in the database
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, TableNames.AUDIT_LOG_TABLE);
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, Schema.AUDIT_LOG_TABLE);

// if there is no channel_id for the given guild_id in the database, then inform
// the user of the same, else link the channel that has been registered
Expand Down Expand Up @@ -166,7 +166,7 @@ private void unsetAuditLogging(SlashCommandInteractionEvent event) {
}

String guildId = Objects.requireNonNull(event.getGuild()).getId();
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, TableNames.AUDIT_LOG_TABLE);
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, Schema.AUDIT_LOG_TABLE);

if (registeredChannelId == null || registeredChannelId.isBlank()) {

Expand All @@ -180,7 +180,7 @@ private void unsetAuditLogging(SlashCommandInteractionEvent event) {
} else {
try {

dc.getGuildDataAccess().unregister(guildId, TableNames.AUDIT_LOG_TABLE);
dc.getGuildDataAccess().unregister(guildId, Schema.AUDIT_LOG_TABLE);

EmbedBuilder eb = new EmbedBuilder();
eb.setTitle("📝 Audit Log Configuration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Objects;

import org.papertrail.database.DatabaseConnector;
import org.papertrail.database.TableNames;
import org.papertrail.database.Schema;
import org.tinylog.Logger;

import net.dv8tion.jda.api.EmbedBuilder;
Expand Down Expand Up @@ -61,7 +61,7 @@ private void setMessageLogging(SlashCommandInteractionEvent event) {
Guild guild = event.getGuild();
String guildId = Objects.requireNonNull(guild).getId();

String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, TableNames.MESSAGE_LOG_REGISTRATION_TABLE);
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, Schema.MESSAGE_LOG_REGISTRATION_TABLE);

if(registeredChannelId!=null && !registeredChannelId.isBlank()) {

Expand All @@ -79,7 +79,7 @@ private void setMessageLogging(SlashCommandInteractionEvent event) {
String channelIdToRegister = event.getChannelId();
try {
// register the channel_id along with guild_id in the database
dc.getGuildDataAccess().registerGuildAndChannel(guildId, channelIdToRegister, TableNames.MESSAGE_LOG_REGISTRATION_TABLE);
dc.getGuildDataAccess().registerGuildAndChannel(guildId, channelIdToRegister, Schema.MESSAGE_LOG_REGISTRATION_TABLE);

eb.addField("✅ Channel Registration Success","╰┈➤"+"All edited and deleted messages will be logged here", false);
eb.setColor(Color.GREEN);
Expand Down Expand Up @@ -116,7 +116,7 @@ private void retrieveMessageLoggingChannel(SlashCommandInteractionEvent event) {
Guild guild = event.getGuild();
String guildId = Objects.requireNonNull(guild).getId();

String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, TableNames.MESSAGE_LOG_REGISTRATION_TABLE);
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, Schema.MESSAGE_LOG_REGISTRATION_TABLE);
// if there is no channel_id for the given guild_id in the database, then inform
// the user of the same, else link the channel that has been registered
if (registeredChannelId == null || registeredChannelId.isBlank()) {
Expand Down Expand Up @@ -157,7 +157,7 @@ private void unsetMessageLogging(SlashCommandInteractionEvent event) {
Guild guild = event.getGuild();
String guildId = Objects.requireNonNull(guild).getId();

String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, TableNames.MESSAGE_LOG_REGISTRATION_TABLE);
String registeredChannelId = dc.getGuildDataAccess().retrieveRegisteredChannel(guildId, Schema.MESSAGE_LOG_REGISTRATION_TABLE);

if (registeredChannelId == null || registeredChannelId.isBlank()) {
eb.addField("ℹ️ Channel Removal", "╰┈➤"+"No channel has been registered for message logs", false);
Expand All @@ -171,7 +171,7 @@ private void unsetMessageLogging(SlashCommandInteractionEvent event) {
} else {
try {

dc.getGuildDataAccess().unregister(guildId, TableNames.MESSAGE_LOG_REGISTRATION_TABLE);
dc.getGuildDataAccess().unregister(guildId, Schema.MESSAGE_LOG_REGISTRATION_TABLE);

eb.addField("✅ Channel Removal", "╰┈➤"+"Channel successfully unset", false);
eb.setColor(Color.GREEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import org.jetbrains.annotations.NotNull;
import org.papertrail.database.DatabaseConnector;
import org.papertrail.database.TableNames;
import org.papertrail.database.Schema;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
Expand All @@ -33,7 +33,7 @@ public void onGuildMemberUpdateBoostTime(@NotNull GuildMemberUpdateBoostTimeEven
vThreadPool.execute(()->{
// this will return a non-null text id if a channel was previously registered in the database
// server boost logs are mapped to audit log table
String registeredChannelId=dc.getGuildDataAccess().retrieveRegisteredChannel(event.getGuild().getId(), TableNames.AUDIT_LOG_TABLE);
String registeredChannelId=dc.getGuildDataAccess().retrieveRegisteredChannel(event.getGuild().getId(), Schema.AUDIT_LOG_TABLE);

if(registeredChannelId==null ||registeredChannelId.isBlank()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.papertrail.database.DatabaseConnector;
import org.papertrail.database.TableNames;
import org.papertrail.database.Schema;
import org.papertrail.utilities.ColorFormatter;
import org.papertrail.utilities.DurationFormatter;
import org.papertrail.utilities.GuildSystemChannelFlagResolver;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void onGuildAuditLogEntryCreate(@NotNull GuildAuditLogEntryCreateEvent ev

vThreadPool.execute(()->{
// this will return a non-null text id if a channel was previously registered in the database
String registeredChannelId=dc.getGuildDataAccess().retrieveRegisteredChannel(event.getGuild().getId(), TableNames.AUDIT_LOG_TABLE);
String registeredChannelId=dc.getGuildDataAccess().retrieveRegisteredChannel(event.getGuild().getId(), Schema.AUDIT_LOG_TABLE);

if(registeredChannelId==null ||registeredChannelId.isBlank()) {
return;
Expand Down
Loading
Loading