From 5ee7321978b1a6a5cb40b74a7faacbf4e5c5515d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 21 Nov 2022 00:27:57 +0100 Subject: [PATCH] Implement functional reply tracker Now, with commands that support it, only the user who ran the command/triggered the interaction can use its buttons. --- .../commands/slash/ClearChatCommand.java | 14 ++++- .../hidekobot/database/DatabaseManager.java | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java index d16f80a..385d1ff 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java @@ -145,7 +145,7 @@ public class ClearChatCommand } - Button deleteButton = Button.danger("clear_delete", "Delete").withEmoji(Emoji.fromUnicode("❌")); + Button deleteButton = Button.danger("clear_delete", "Dismiss").withEmoji(Emoji.fromUnicode("❌")); WebhookMessageEditAction webhookMessageEditAction; @@ -167,8 +167,10 @@ public class ClearChatCommand String replyMessageId = message.getId(); String replyChannelId = message.getChannel().getId(); String replyGuildId = message.getGuild().getId(); + String userId = event.getUser().getId(); Configuration.getDatabaseManager().queueDisabling(replyGuildId, replyChannelId, replyMessageId); + Configuration.getDatabaseManager().trackRanCommandReply(replyGuildId, replyChannelId, replyMessageId, userId); } }).start(); @@ -177,6 +179,14 @@ public class ClearChatCommand public void deleteButton(ButtonInteractionEvent event) { - event.getInteraction().getMessage().delete().queue(); + + if(!(Configuration.getDatabaseManager().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) + { + event.reply("❌ You did not run this command!").setEphemeral(true).queue(); + } else + { + event.getInteraction().getMessage().delete().queue(); + } + } } diff --git a/src/main/java/wtf/beatrice/hidekobot/database/DatabaseManager.java b/src/main/java/wtf/beatrice/hidekobot/database/DatabaseManager.java index 221104f..25313af 100644 --- a/src/main/java/wtf/beatrice/hidekobot/database/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/hidekobot/database/DatabaseManager.java @@ -95,6 +95,60 @@ public class DatabaseManager return true; } + public boolean trackRanCommandReply(String guildId, String channelId, String messageId, String userId) + { + String query = "INSERT INTO command_runners " + + "(guild_id, channel_id, message_id, user_id) VALUES " + + " (?, ?, ?, ?);"; + + try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) + { + preparedStatement.setString(1, guildId); + preparedStatement.setString(2, channelId); + preparedStatement.setString(3, messageId); + preparedStatement.setString(4, userId); + + preparedStatement.executeUpdate(); + + return true; + } catch (SQLException e) + { + e.printStackTrace(); + } + + return false; + } + + public boolean isUserTrackedFor(String userId, String messageId) + { + String trackedUserId = getTrackedReplyUserId(messageId); + if(trackedUserId == null) return false; + return userId.equals(trackedUserId); + } + + public String getTrackedReplyUserId(String messageId) + { + String query = "SELECT user_id " + + "FROM command_runners " + + "WHERE message_id = ?;"; + + try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) + { + preparedStatement.setString(1, messageId); + ResultSet resultSet = preparedStatement.executeQuery(); + if(resultSet.isClosed()) return null; + while(resultSet.next()) + { + return resultSet.getString("user_id"); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + + return null; + } + public boolean queueDisabling(String guildId, String channelId, String messageId) { LocalDateTime expiryTime = LocalDateTime.now().plusSeconds(Configuration.getExpiryTimeSeconds());