From fc846fa901ced755bf931342e292458d8b3e5fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 20 Dec 2022 14:49:44 +0100 Subject: [PATCH] Remove duplicated method --- .../hidekobot/commands/base/ClearChat.java | 14 ---------- .../commands/base/UrbanDictionary.java | 12 -------- .../listeners/ButtonInteractionListener.java | 5 ++-- .../beatrice/hidekobot/util/CommandUtil.java | 28 +++++++++++++++++++ 4 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java index d83fef0..74fad03 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java @@ -179,20 +179,6 @@ public class ClearChat } } - - - public static void dismissMessage(ButtonInteractionEvent event) - { - - if(!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) - { - event.reply("❌ You did not run this command!").setEphemeral(true).queue(); - } else - { - event.getInteraction().getMessage().delete().queue(); - } - } - // cap the amount to avoid abuse. public static int getMaxAmount() { return 1000; } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/UrbanDictionary.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/UrbanDictionary.java index a1cfd92..e8b0823 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/UrbanDictionary.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/UrbanDictionary.java @@ -120,19 +120,7 @@ public class UrbanDictionary } - public static void delete(ButtonInteractionEvent event) - { - String messageId = event.getMessageId(); - DatabaseSource database = Cache.getDatabaseSource(); - // check if the user interacting is the same one who ran the command - if (!(database.isUserTrackedFor(event.getUser().getId(), messageId))) { - event.reply("❌ You did not run this command!").setEphemeral(true).queue(); - return; - } - - event.getInteraction().getMessage().delete().queue(); - } public static void track(Message message, User user, UrbanSearch search, String sanitizedTerm) { diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java index dca38bd..c754333 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java @@ -5,6 +5,7 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter; import wtf.beatrice.hidekobot.commands.base.ClearChat; import wtf.beatrice.hidekobot.commands.base.CoinFlip; import wtf.beatrice.hidekobot.commands.base.UrbanDictionary; +import wtf.beatrice.hidekobot.util.CommandUtil; public class ButtonInteractionListener extends ListenerAdapter { @@ -19,12 +20,12 @@ public class ButtonInteractionListener extends ListenerAdapter case "coinflip_reflip" -> CoinFlip.buttonReFlip(event); // clearchat command - case "clear_dismiss" -> ClearChat.dismissMessage(event); + case "clear_dismiss" -> CommandUtil.delete(event); // urban dictionary navigation case "urban_nextpage" -> UrbanDictionary.changePage(event, true); case "urban_previouspage" -> UrbanDictionary.changePage(event, false); - case "urban_delete" -> UrbanDictionary.delete(event); + case "urban_delete" -> CommandUtil.delete(event); } diff --git a/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java new file mode 100644 index 0000000..2fc4100 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/util/CommandUtil.java @@ -0,0 +1,28 @@ +package wtf.beatrice.hidekobot.util; + +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.datasources.DatabaseSource; + +public class CommandUtil +{ + + /** + * Function to delete a message when a user clicks the "delete" button attached to that message. + * This will check in the database if that user ran the command originally. + * + * @param event the button interaction event. + */ + public static void delete(ButtonInteractionEvent event) + { + // check if the user interacting is the same one who ran the command + if (!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) { + event.reply("❌ You did not run this command!").setEphemeral(true).queue(); + return; + } + + // delete the message + event.getInteraction().getMessage().delete().queue(); + // no need to manually untrack it from database, it will be purged on the next planned check. + } +}