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.0.0</version>
<version>1.1.0</version>
<name>PaperTrail</name>
<description>A simple bot for logging all user actions</description>
<properties>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/papertrail/cleanup/BotKickListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void onGuildLeave(@NotNull GuildLeaveEvent event) {
Guild leftGuild = event.getGuild();
try {
dc.unregisterGuildAndChannel(leftGuild.getId(), TableNames.AUDIT_LOG_TABLE);
dc.unregisterGuildAndChannel(leftGuild.getId(), TableNames.MESSAGE_LOG_REGISTRATION_TABLE);
} catch (SQLException e) {
Logger.error("Could not auto-unregister guild upon guild leave", e);
}
Expand Down
172 changes: 149 additions & 23 deletions src/main/java/org/papertrail/database/DatabaseConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,47 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.papertrail.utilities.EnvConfig;
import org.tinylog.Logger;

public class DatabaseConnector {

private static final String DB_URL = EnvConfig.get("DATABASEURL");

private Connection connect;

public DatabaseConnector() throws SQLException {
connect = DriverManager.getConnection(DB_URL);
}

public void registerGuildAndChannel(String guildId, String channelId, String tableName) throws SQLException {

String sqlStatement = "INSERT INTO "+tableName+" (guild_id, channel_id) VALUES (?, ?)";

try(PreparedStatement psmt = connect.prepareStatement(sqlStatement)){
psmt.setString(1, guildId);
psmt.setString(2, channelId);
psmt.setLong(1, Long.parseLong(guildId));
psmt.setLong(2, Long.parseLong(channelId));
psmt.executeUpdate();
}

}

public String retrieveChannelId(String guildId, String tableName) {

String sqlStatement = "SELECT (channel_id) FROM " + tableName + " WHERE guild_id = ?";
public String retrieveRegisteredChannelId(String guildId, String tableName) {

String sqlStatement = "SELECT channel_id FROM " + tableName + " WHERE guild_id = ?";
String channelId = "";

try (PreparedStatement psmt = connect.prepareStatement(sqlStatement)) {
psmt.setString(1, guildId);
psmt.setLong(1, Long.parseLong(guildId));

try (ResultSet rs = psmt.executeQuery()) {
if (!rs.isBeforeFirst()) {
return null;
} else {
while (rs.next()) { // by configuration, there will always be only one channel id row at a time
// because registering multiple channels is not allowed
channelId = rs.getString("channel_id");
}

while (rs.next()) { // by configuration, there will always be only one channel id row at a time
// because registering multiple channels is not allowed
channelId = String.valueOf(rs.getLong("channel_id"));
}
}
return channelId;
Expand All @@ -55,13 +54,140 @@ public String retrieveChannelId(String guildId, String tableName) {
return null;
}
}

/*
* If a guild of the given id is found, will return that id, otherwise null
*/
public String checkGuildRegistration(String guildId, String tableName) {

String sqlStatement = "SELECT guild_id FROM " + tableName + " WHERE guild_id = ?";

try (PreparedStatement psmt = connect.prepareStatement(sqlStatement)) {
psmt.setLong(1, Long.parseLong(guildId));

try (ResultSet rs = psmt.executeQuery()) {

while (rs.next()) {
return String.valueOf(rs.getLong("guild_id"));

}
}
return null;
} catch (SQLException e) {
Logger.error("Could not retrieve registered guild channel id", e);
return null;
}
}

/*
* If a channel of the given id is found, will return that id, otherwise null
*/
public String checkChannelRegistration(String channelId, String tableName) {

String sqlStatement = "SELECT channel_id FROM " + tableName + " WHERE channel_id = ?";

try (PreparedStatement psmt = connect.prepareStatement(sqlStatement)) {
psmt.setLong(1, Long.parseLong(channelId));

try (ResultSet rs = psmt.executeQuery()) {
while (rs.next()) {
return String.valueOf(rs.getLong("guild_id"));
}
}
return null;
} catch (SQLException e) {
Logger.error("Could not retrieve registered guild channel id", e);
return null;
}
}

public void unregisterGuildAndChannel(String guildId, String tableName) throws SQLException {

String sqlStatement = "DELETE FROM "+tableName+" WHERE guild_id = ?";


try(PreparedStatement psmt = connect.prepareStatement(sqlStatement)){
psmt.setLong(1, Long.parseLong(guildId));
psmt.executeUpdate();
}
}


// MESSAGE FUNCTIONS
public void logMessage(String messageId, String messageContent, String authorId, String tableName) throws SQLException {

String sqlStatement = "INSERT INTO "+tableName+" (message_id, message_content, author_id) VALUES (?, ?, ?)";

try(PreparedStatement psmt = connect.prepareStatement(sqlStatement)){
psmt.setLong(1, Long.parseLong(messageId));
psmt.setString(2, messageContent);
psmt.setLong(3, Long.parseLong(authorId));
psmt.executeUpdate();
}
}

public List<String> retrieveAuthorAndMessage(String messageId, String tableName) {

String sqlStatement = "SELECT author_id, message_content FROM " + tableName + " WHERE message_id = ?";

String messageContent = "";
String authorId = "";

try (PreparedStatement psmt = connect.prepareStatement(sqlStatement)) {
psmt.setLong(1, Long.parseLong(messageId));

try (ResultSet rs = psmt.executeQuery()) {
if (rs.next()) { // only one message is logged per row per message id
messageContent = rs.getString("message_content");
authorId = String.valueOf(rs.getLong("author_id"));
return List.of(authorId, messageContent);
}
}
return Collections.emptyList();
} catch (SQLException e) {
Logger.error("Could not retrieve message", e);
e.printStackTrace();
return Collections.emptyList();
}
}
/*
* Checks if the message id exists in the database and returns the same if found, else null
*/
public String checkMessageId(String messageId, String tableName) {

String sqlStatement = "SELECT message_id FROM " + tableName + " WHERE message_id = ?";

try (PreparedStatement psmt = connect.prepareStatement(sqlStatement)) {
psmt.setLong(1, Long.parseLong(messageId));

try (ResultSet rs = psmt.executeQuery()) {
if (rs.next()) { // only one message is logged per row per message id
return String.valueOf(rs.getLong("message_id"));
}
}
return null;
} catch (SQLException e) {
Logger.error("Could not retrieve message id", e);
e.printStackTrace();
return null;
}
}

public void updateMessage(String messageId, String messageContent, String tableName) throws SQLException {

String sqlStatement = "UPDATE " + tableName + " SET message_content = ? WHERE message_id = ?";

try(PreparedStatement psmt = connect.prepareStatement(sqlStatement)){
psmt.setString(1, messageContent);
psmt.setLong(2, Long.parseLong(messageId));
psmt.executeUpdate();
}
}

public void deleteMessage(String messageId, String tableName) throws SQLException {

String sqlStatement = "DELETE FROM "+tableName+" WHERE message_id = ?";

try(PreparedStatement psmt = connect.prepareStatement(sqlStatement)){
psmt.setString(1, guildId);
psmt.setLong(1, Long.parseLong(messageId));
psmt.executeUpdate();
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/papertrail/database/TableNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ public class TableNames {
private TableNames() {
throw new IllegalStateException("Utility Class");
}

public static final String AUDIT_LOG_TABLE = "audit_log_table";
public static final String MESSAGE_LOG_REGISTRATION_TABLE = "message_log_registration_table";
public static final String MESSAGE_LOG_CONTENT_TABLE = "message_log_content_table";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {

eb.addField("🏷️ App Name", "╰┈➤"+VersionInfo.APPNAME, false);
eb.addField("⚙️ App Version", "╰┈➤"+VersionInfo.VERSION, false);
eb.addField("🗂️ App Source Code", VersionInfo.PROJECT_LINK, false);
eb.addField("📃 App Source Code", VersionInfo.PROJECT_LINK, false);
eb.addField("🖧 App Server", "╰┈➤"+VersionInfo.SERVER_LOCATION, false);
eb.addField("🛢 App Database", "╰┈➤"+VersionInfo.DATABASE_LOCATION, false);

eb.setFooter(VersionInfo.APPNAME+" is free and open source ");
eb.setFooter(VersionInfo.APPNAME+" "+VersionInfo.VERSION);
eb.setTimestamp(Instant.now());

MessageEmbed mb = eb.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.papertrail.listeners.customlisteners;

import java.awt.Color;
import java.time.Instant;

import org.papertrail.version.VersionInfo;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class BotSetupListener extends ListenerAdapter {

@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {

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

EmbedBuilder eb = new EmbedBuilder();
eb.setTitle("🛠️ Setup Guide for " + VersionInfo.APPNAME);
eb.setDescription("Welcome to **" + VersionInfo.APPNAME + "**!\nHere's how to get started using the bot in your server.");
eb.setColor(Color.decode("#38e8bc"));

eb.addField("1️⃣ Register Audit Log Channel",
"- Use `/auditlogchannel-set` to **register the current channel** for receiving audit log updates.\n╰┈➤ User must have `Manage Server` permission.",
false);

eb.addField("2️⃣ View Registered Audit Log Channel",
"- Use `/auditlogchannel-view` to **check which channel** is currently registered for audit logs.\nThis is helpful if you're unsure where logs are going.\n╰┈➤ User must have `Manage Server` permission.",
false);

eb.addField("3️⃣ Unregister Audit Log Channel",
"- Use `/auditlogchannel-remove` to **unset the audit log channel** if you wish to stop logging or switch to another one.\n╰┈➤ User must have `Manage Server` permission.",
false);

eb.addBlankField(false);

eb.addField("4️⃣ Register Message Log Channel",
"- Use `/messagelogchannel-set` to **register the current channel** for receiving audit log updates.\n╰┈➤ User must have `Manage Server` permission.",
false);

eb.addField("5️⃣ View Registered Message Log Channel",
"- Use `/messagelogchannel-view` to **check which channel** is currently registered for message logs.\nThis is helpful if you're unsure where logs are going.\n╰┈➤ User must have `Manage Server` permission.",
false);

eb.addField("6️⃣ Unregister Message Log Channel",
"- Use `/messagelogchannel-remove` to **unset the message log channel** if you wish to stop logging or switch to another one.\n╰┈➤ User must have `Manage Server` permission.",
false);

eb.addBlankField(false);

eb.addField("7️⃣ View Server Stats",
"- Use `/stats` to **get useful server information** like member count, channel count, and more.",
false);

eb.addField("8️⃣ Bot Information",
"- Use `/about` to **view bot details**, including author info and version.",
false);

String tips = """
- Make sure the bot has view and message send permissions for the logging channel.
- Commands only work in servers (guilds), not in DMs.
- The bot will lose its configuration if kicked from the server.
- By default, all messages are stored in the database for 30 days, after which the newer ones will replace the older ones.""";

eb.addField("💡 Tips",tips,false);
eb.addField("📬 Need help?", "Create an issue on [GitHub](" + VersionInfo.PROJECT_ISSUE_LINK+")", false);
eb.setFooter(VersionInfo.APPNAME+" "+VersionInfo.VERSION);
eb.setTimestamp(Instant.now());

MessageEmbed mb = eb.build();
event.replyEmbeds(mb).setEphemeral(false).queue();
}
}
}

This file was deleted.

Loading
Loading