From b70529d2fa9830eb6cb9930914193b31c900976d Mon Sep 17 00:00:00 2001 From: RedVortexDev <58099979+RedVortexDev@users.noreply.github.com> Date: Mon, 18 Nov 2024 20:46:27 +0200 Subject: [PATCH 1/2] feat: remove locking behavior from ?solve Required me to make some changes to how stuff is handled, as now we can't rely on the post being locked to make sure it's solved --- .../helpbot/bot/HelpBotInstance.java | 2 +- .../impl/other/util/SolvedCommand.java | 16 +++++-- .../bot/events/ChannelArchiveEvent.java | 24 ----------- .../bot/events/ChannelCreatedEvent.java | 6 ++- .../bot/events/ChannelUpdatedNameEvent.java | 42 +++++++++++++++++++ ...edTagsEvent.java => PostChannelEvent.java} | 28 +++++++++---- 6 files changed, 81 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java create mode 100644 src/main/java/com/diamondfire/helpbot/bot/events/ChannelUpdatedNameEvent.java rename src/main/java/com/diamondfire/helpbot/bot/events/{PostAppliedTagsEvent.java => PostChannelEvent.java} (62%) diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index ff329378..7c297f70 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -141,7 +141,7 @@ public static void initialize() throws LoginException { .setActivity(Activity.watching("for " + getConfig().getPrefix() + "help")) .setGatewayEncoding(GatewayEncoding.ETF) .disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.CLIENT_STATUS) - .addEventListeners(new MessageEvent(), new ReadyEvent(), new GuildJoinEvent(), new ButtonEvent(), new MessageEditEvent(), new PostAppliedTagsEvent(), new ChannelCreatedEvent(), new ChannelArchiveEvent()); + .addEventListeners(new MessageEvent(), new ReadyEvent(), new GuildJoinEvent(), new ButtonEvent(), new MessageEditEvent(), new PostChannelEvent(), new ChannelCreatedEvent(), new ChannelUpdatedNameEvent()); jda = builder.build(); CommandHandler.getInstance().initialize(); diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/util/SolvedCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/util/SolvedCommand.java index 3bf2d991..8987699e 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/util/SolvedCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/util/SolvedCommand.java @@ -22,6 +22,11 @@ public String getName() { return "solved"; } + @Override + public String[] getAliases() { + return new String[]{"solve", "resolved"}; + } + @Override public HelpContext getHelpContext() { return new HelpContext() @@ -64,8 +69,8 @@ public void run(CommandEvent event) { return; } - // Check if the post is already locked. - if (threadChannel.isLocked()) { + // Check if the post already has the solved tag. + if (threadChannel.getName().startsWith("[SOLVED] ")) { event.reply(new PresetBuilder() .withPreset( new InformativeReply(InformativeReplyType.ERROR, "Post is already solved.") @@ -76,8 +81,11 @@ public void run(CommandEvent event) { // Apply the solved tag, other behavior handled by PostAppliedTagsEvent. ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); ArrayList appliedTags = new ArrayList<>(threadChannel.getAppliedTags()); - if (!appliedTags.contains(solvedTag)) appliedTags.add(solvedTag); - + if (appliedTags.contains(solvedTag)) { + appliedTags.remove(solvedTag); + threadChannel.getManager().setAppliedTags(appliedTags).queue(); + } + appliedTags.add(solvedTag); threadChannel.getManager().setAppliedTags(appliedTags).queue(); } diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java deleted file mode 100644 index 6c13d58f..00000000 --- a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.diamondfire.helpbot.bot.events; - -import com.diamondfire.helpbot.bot.HelpBotInstance; -import net.dv8tion.jda.api.entities.channel.ChannelType; -import net.dv8tion.jda.api.events.channel.update.ChannelUpdateArchivedEvent; -import net.dv8tion.jda.api.hooks.ListenerAdapter; - -public class ChannelArchiveEvent extends ListenerAdapter { - - @Override - public void onChannelUpdateArchived(ChannelUpdateArchivedEvent event) { - // Limit to help forum. - if ( - event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD || - event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel() - ) { - return; - } - - // When a post is archived, it should be locked. - event.getChannel().asThreadChannel().getManager().setLocked(true).queue(); - } - -} diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java index 61166268..254a5b5a 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java +++ b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java @@ -27,7 +27,11 @@ public void onChannelCreate(ChannelCreateEvent event) { ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); if (threadChannel.getAppliedTags().contains(solvedTag)) { ArrayList appliedTags = new ArrayList<>(threadChannel.getAppliedTags()); - appliedTags.remove(solvedTag); + // Solved tag is the only tag, and we need at least one tag. + // In this case, this will do nothing, however ?solved will still change post's the name, so it's fine. + if (appliedTags.size() != 1) { + appliedTags.remove(solvedTag); + } threadChannel.getManager().setAppliedTags(appliedTags).queue(); } diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelUpdatedNameEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelUpdatedNameEvent.java new file mode 100644 index 00000000..15928334 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelUpdatedNameEvent.java @@ -0,0 +1,42 @@ +package com.diamondfire.helpbot.bot.events; + +import com.diamondfire.helpbot.bot.HelpBotInstance; +import net.dv8tion.jda.api.entities.channel.ChannelType; +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; +import net.dv8tion.jda.api.entities.channel.forums.ForumTag; +import net.dv8tion.jda.api.events.channel.update.*; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; + +public class ChannelUpdatedNameEvent extends ListenerAdapter { + + @Override + public void onChannelUpdateName(@NotNull ChannelUpdateNameEvent event) { + // Limit to help forum. + if ( + event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD || + event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel() + ) { + return; + } + + ThreadChannel threadChannel = event.getChannel().asThreadChannel(); + ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); + + if (event.getOldValue() == null || event.getNewValue() == null) return; + + System.out.println(); + System.out.println(event.getOldValue()); + System.out.println(event.getNewValue()); + + // If the post starts with [SOLVED] and has a solved tag, but renamed to not have [SOLVED], then revert to the old name. + if (event.getOldValue().startsWith("[SOLVED] ") && !event.getNewValue().startsWith("[SOLVED] ") && threadChannel.getAppliedTags().contains(solvedTag)) { + threadChannel.getManager().setName(event.getOldValue()).queue(); + // If the post does not start with [SOLVED] and does not have a solved tag, but renamed to have [SOLVED], then revert to the old name. + } else if (!event.getOldValue().startsWith("[SOLVED] ") && event.getNewValue().startsWith("[SOLVED] ") && !threadChannel.getAppliedTags().contains(solvedTag)) { + threadChannel.getManager().setName(event.getOldValue()).queue(); + } + + } + +} diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/PostChannelEvent.java similarity index 62% rename from src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java rename to src/main/java/com/diamondfire/helpbot/bot/events/PostChannelEvent.java index 48b7033c..514d0f72 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java +++ b/src/main/java/com/diamondfire/helpbot/bot/events/PostChannelEvent.java @@ -3,13 +3,14 @@ import com.diamondfire.helpbot.bot.HelpBotInstance; import com.diamondfire.helpbot.bot.command.reply.*; import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; +import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.forums.ForumTag; import net.dv8tion.jda.api.entities.channel.unions.ChannelUnion; -import net.dv8tion.jda.api.events.channel.update.ChannelUpdateAppliedTagsEvent; +import net.dv8tion.jda.api.events.channel.update.*; import net.dv8tion.jda.api.hooks.ListenerAdapter; -public class PostAppliedTagsEvent extends ListenerAdapter { +public class PostChannelEvent extends ListenerAdapter { @Override public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) { @@ -22,9 +23,8 @@ public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) { ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); - // If the solved tag is added and the post is not locked, lock the thread. - if (event.getAddedTags().contains(solvedTag) && !threadChannel.isLocked()) { - threadChannel.getManager().setLocked(true).queue(); + // If the solved tag is added, add [SOLVED] to the thread's name. + if (event.getAddedTags().contains(solvedTag) && !threadChannel.getName().startsWith("[SOLVED] ")) { threadChannel.sendMessageEmbeds( new PresetBuilder() .withPreset( @@ -32,10 +32,24 @@ public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) { ).getEmbed().build() ).queue(); threadChannel.getManager().setName("[SOLVED] " + channel.getName()).queue(); - } else if (event.getRemovedTags().contains(solvedTag) && threadChannel.isLocked()) { - // If the solved tag is removed and the post is locked, put the old tags back. + } else if (event.getRemovedTags().contains(solvedTag) && threadChannel.getName().startsWith("[SOLVED] ")) { + // If the solved tag is removed and the post has [SOLVED] in its name, put the old tags back. threadChannel.getManager().setAppliedTags(event.getOldTags()).queue(); } } + @Override + public void onChannelUpdateArchived(ChannelUpdateArchivedEvent event) { + // Limit to help forum. + if ( + event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD || + event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel() + ) { + return; + } + + // When a post is archived, it should be locked. + event.getChannel().asThreadChannel().getManager().setLocked(true).queue(); + } + } From 70393085d18b05240383c760452091c52956497c Mon Sep 17 00:00:00 2001 From: RedVortexDev <58099979+RedVortexDev@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:31:46 +0200 Subject: [PATCH 2/2] fix: leftover debug --- .../helpbot/bot/events/ChannelUpdatedNameEvent.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelUpdatedNameEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelUpdatedNameEvent.java index 15928334..197d1078 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelUpdatedNameEvent.java +++ b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelUpdatedNameEvent.java @@ -25,10 +25,6 @@ public void onChannelUpdateName(@NotNull ChannelUpdateNameEvent event) { if (event.getOldValue() == null || event.getNewValue() == null) return; - System.out.println(); - System.out.println(event.getOldValue()); - System.out.println(event.getNewValue()); - // If the post starts with [SOLVED] and has a solved tag, but renamed to not have [SOLVED], then revert to the old name. if (event.getOldValue().startsWith("[SOLVED] ") && !event.getNewValue().startsWith("[SOLVED] ") && threadChannel.getAppliedTags().contains(solvedTag)) { threadChannel.getManager().setName(event.getOldValue()).queue();