From bf954c292c6795bb624571e3a6f0590197129662 Mon Sep 17 00:00:00 2001 From: RedVortexDev <58099979+RedVortexDev@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:25:45 +0300 Subject: [PATCH 1/3] feat: in help forum, ?solve & lock closed posts --- .../helpbot/bot/HelpBotInstance.java | 3 +- .../impl/other/util/SolvedCommand.java | 87 +++++++++++++++++++ .../helpbot/bot/config/Config.java | 8 ++ .../bot/events/ChannelArchiveEvent.java | 24 +++++ .../bot/events/ChannelCreatedEvent.java | 32 +++++++ .../bot/events/PostAppliedTagsEvent.java | 40 +++++++++ 6 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/impl/other/util/SolvedCommand.java create mode 100644 src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java create mode 100644 src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java create mode 100644 src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index 9c6bb118..ff329378 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -53,6 +53,7 @@ public static void initialize() throws LoginException { // others //new CowsayCommand(), new MimicCommand(), + new SolvedCommand(), //new FetchDataCommand(), new InfoCommand(), new EvalCommand(), @@ -140,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()); + .addEventListeners(new MessageEvent(), new ReadyEvent(), new GuildJoinEvent(), new ButtonEvent(), new MessageEditEvent(), new PostAppliedTagsEvent(), new ChannelCreatedEvent(), new ChannelArchiveEvent()); 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 new file mode 100644 index 00000000..62cf8b82 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/util/SolvedCommand.java @@ -0,0 +1,87 @@ +package com.diamondfire.helpbot.bot.command.impl.other.util; + +import com.diamondfire.helpbot.bot.HelpBotInstance; +import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; +import com.diamondfire.helpbot.bot.command.help.*; +import com.diamondfire.helpbot.bot.command.impl.Command; +import com.diamondfire.helpbot.bot.command.permissions.Permission; +import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; +import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; +import com.diamondfire.helpbot.bot.events.CommandEvent; +import net.dv8tion.jda.api.entities.channel.concrete.*; +import net.dv8tion.jda.api.entities.channel.forums.ForumTag; + +import java.util.*; + + +public class SolvedCommand extends Command { + + @Override + public String getName() { + return "solved"; + } + + @Override + public HelpContext getHelpContext() { + return new HelpContext() + .description("Marks the help post as solved.") + .category(CommandCategory.OTHER); + } + + @Override + public ArgumentSet compileArguments() { + return new ArgumentSet(); + } + + @Override + public Permission getPermission() { + return Permission.USER; + } + + @Override + public void run(CommandEvent event) { + // Check if the command is used in the help forum. + if (event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()) { + event.reply(new PresetBuilder() + .withPreset( + new InformativeReply(InformativeReplyType.ERROR, "Command can only be used in <#" + HelpBotInstance.getConfig().getHelpChannel() + ">") + )); + return; + } + + ThreadChannel threadChannel = event.getChannel().asThreadChannel(); + + // Check if the command is used by the post owner. + if (event.getMember() == null | threadChannel.getOwnerIdLong() != event.getMember().getIdLong()) { + event.reply(new PresetBuilder() + .withPreset( + new InformativeReply(InformativeReplyType.ERROR, "Command can only be used by the post owner.") + )); + return; + } + + // Check if the post is already locked. + if (threadChannel.isLocked()) { + event.reply(new PresetBuilder() + .withPreset( + new InformativeReply(InformativeReplyType.ERROR, "Post is already solved.") + )); + return; + } + + // Apply the solved tag and lock the post. + var solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); + ArrayList appliedTags = new ArrayList<>(threadChannel.getAppliedTags()); + if (!appliedTags.contains(solvedTag)) appliedTags.add(solvedTag); + threadChannel.getManager().setAppliedTags(appliedTags).queue(); + + threadChannel.getManager().setLocked(true).queue(); + + event.reply(new PresetBuilder() + .withPreset( + new InformativeReply(InformativeReplyType.SUCCESS, "Post marked as solved!") + )); + event.getChannel().asThreadChannel().getManager().setName("[ARCHIVED] " + event.getChannel().getName()).queue(); + } + +} diff --git a/src/main/java/com/diamondfire/helpbot/bot/config/Config.java b/src/main/java/com/diamondfire/helpbot/bot/config/Config.java index 59fc8666..efd2756a 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/config/Config.java +++ b/src/main/java/com/diamondfire/helpbot/bot/config/Config.java @@ -79,6 +79,14 @@ public long getPurgeEvidenceChannel() { return getPropertyLong("purge_evidence_channel"); } + public long getHelpChannel() { + return getPropertyLong("help_channel"); + } + + public long getHelpChannelSolvedTag() { + return getPropertyLong("help_channel_solved_tag"); + } + public long getMutedRole() { return getPropertyLong("muted_role"); } diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java new file mode 100644 index 00000000..6c13d58f --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java @@ -0,0 +1,24 @@ +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 new file mode 100644 index 00000000..1bf2522c --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java @@ -0,0 +1,32 @@ +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.ChannelCreateEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +import java.util.ArrayList; + +public class ChannelCreatedEvent extends ListenerAdapter { + + @Override + public void onChannelCreate(ChannelCreateEvent event) { + // Limit to help forum. + if ( + event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD || + event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel() + ) { + return; + } + + // Remove solved tag if post was created with it. + var solvedTag = event.getChannel().asThreadChannel().getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); + if (event.getChannel().asThreadChannel().getAppliedTags().contains(solvedTag)) { + var appliedTags = new ArrayList<>(event.getChannel().asThreadChannel().getAppliedTags()); + appliedTags.remove(solvedTag); + event.getChannel().asThreadChannel().getManager().setAppliedTags(appliedTags).queue(); + } + + } + +} diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java new file mode 100644 index 00000000..9778aaad --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java @@ -0,0 +1,40 @@ +package com.diamondfire.helpbot.bot.events; + +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.concrete.ThreadChannel; +import net.dv8tion.jda.api.events.channel.update.ChannelUpdateAppliedTagsEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +public class PostAppliedTagsEvent extends ListenerAdapter { + + @Override + public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) { + // Limit to help forum. + if (event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()) { + return; + } + + var solvedTag = event.getChannel().asThreadChannel().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) && !event.getChannel().asThreadChannel().isLocked()) { + event.getChannel().asThreadChannel().getManager().setLocked(true).queue(); + ThreadChannel threadChannel = HelpBotInstance.getJda().getThreadChannelById(event.getChannel().getIdLong()); + if (threadChannel != null) { + threadChannel.sendMessageEmbeds( + new PresetBuilder() + .withPreset( + new InformativeReply(InformativeReplyType.SUCCESS, "Post marked as solved") + ).getEmbed().build() + ).queue(); + } + event.getChannel().asThreadChannel().getManager().setName("[SOLVED] " + event.getChannel().getName()).queue(); + } else if (event.getRemovedTags().contains(solvedTag) && event.getChannel().asThreadChannel().isLocked()) { + // If the solved tag is removed and the post is locked, put the old tags back. + event.getChannel().asThreadChannel().getManager().setAppliedTags(event.getOldTags()).queue(); + } + } + +} From d472fbf43facfb44c75828953544c52714f4612a Mon Sep 17 00:00:00 2001 From: RedVortexDev <58099979+RedVortexDev@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:36:50 +0300 Subject: [PATCH 2/3] fix: duplicate behavior from ?solved --- .../impl/other/util/SolvedCommand.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) 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 62cf8b82..b174c915 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 @@ -8,6 +8,7 @@ import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; import com.diamondfire.helpbot.bot.events.CommandEvent; +import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.*; import net.dv8tion.jda.api.entities.channel.forums.ForumTag; @@ -40,8 +41,11 @@ public Permission getPermission() { @Override public void run(CommandEvent event) { - // Check if the command is used in the help forum. - if (event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()) { + // Limit to help forum. + if ( + event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD || + event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel() + ) { event.reply(new PresetBuilder() .withPreset( new InformativeReply(InformativeReplyType.ERROR, "Command can only be used in <#" + HelpBotInstance.getConfig().getHelpChannel() + ">") @@ -69,19 +73,12 @@ public void run(CommandEvent event) { return; } - // Apply the solved tag and lock the post. + // Apply the solved tag, other behavior handled by PostAppliedTagsEvent. var solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); ArrayList appliedTags = new ArrayList<>(threadChannel.getAppliedTags()); if (!appliedTags.contains(solvedTag)) appliedTags.add(solvedTag); - threadChannel.getManager().setAppliedTags(appliedTags).queue(); - - threadChannel.getManager().setLocked(true).queue(); - event.reply(new PresetBuilder() - .withPreset( - new InformativeReply(InformativeReplyType.SUCCESS, "Post marked as solved!") - )); - event.getChannel().asThreadChannel().getManager().setName("[ARCHIVED] " + event.getChannel().getName()).queue(); + threadChannel.getManager().setAppliedTags(appliedTags).queue(); } } From d360c246c1e4b78335ca62e52508f27e4fb71673 Mon Sep 17 00:00:00 2001 From: RedVortexDev <58099979+RedVortexDev@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:54:41 +0300 Subject: [PATCH 3/3] style: remove var usage & set event.getChannel()s to a variable --- .../impl/other/util/SolvedCommand.java | 2 +- .../bot/events/ChannelCreatedEvent.java | 12 ++++--- .../bot/events/PostAppliedTagsEvent.java | 33 ++++++++++--------- 3 files changed, 26 insertions(+), 21 deletions(-) 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 b174c915..3bf2d991 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 @@ -74,7 +74,7 @@ public void run(CommandEvent event) { } // Apply the solved tag, other behavior handled by PostAppliedTagsEvent. - var solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); + ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); ArrayList appliedTags = new ArrayList<>(threadChannel.getAppliedTags()); if (!appliedTags.contains(solvedTag)) appliedTags.add(solvedTag); 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 1bf2522c..61166268 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java +++ b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java @@ -2,6 +2,8 @@ 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.ChannelCreateEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -20,11 +22,13 @@ public void onChannelCreate(ChannelCreateEvent event) { } // Remove solved tag if post was created with it. - var solvedTag = event.getChannel().asThreadChannel().getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); - if (event.getChannel().asThreadChannel().getAppliedTags().contains(solvedTag)) { - var appliedTags = new ArrayList<>(event.getChannel().asThreadChannel().getAppliedTags()); + ThreadChannel threadChannel = event.getChannel().asThreadChannel(); + + ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); + if (threadChannel.getAppliedTags().contains(solvedTag)) { + ArrayList appliedTags = new ArrayList<>(threadChannel.getAppliedTags()); appliedTags.remove(solvedTag); - event.getChannel().asThreadChannel().getManager().setAppliedTags(appliedTags).queue(); + threadChannel.getManager().setAppliedTags(appliedTags).queue(); } } diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java index 9778aaad..48b7033c 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java +++ b/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java @@ -4,6 +4,8 @@ import com.diamondfire.helpbot.bot.command.reply.*; import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; 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.hooks.ListenerAdapter; @@ -12,28 +14,27 @@ public class PostAppliedTagsEvent extends ListenerAdapter { @Override public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) { // Limit to help forum. - if (event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()) { + ChannelUnion channel = event.getChannel(); + ThreadChannel threadChannel = channel.asThreadChannel(); + if (threadChannel.getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()) { return; } - var solvedTag = event.getChannel().asThreadChannel().getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag()); + 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) && !event.getChannel().asThreadChannel().isLocked()) { - event.getChannel().asThreadChannel().getManager().setLocked(true).queue(); - ThreadChannel threadChannel = HelpBotInstance.getJda().getThreadChannelById(event.getChannel().getIdLong()); - if (threadChannel != null) { - threadChannel.sendMessageEmbeds( - new PresetBuilder() - .withPreset( - new InformativeReply(InformativeReplyType.SUCCESS, "Post marked as solved") - ).getEmbed().build() - ).queue(); - } - event.getChannel().asThreadChannel().getManager().setName("[SOLVED] " + event.getChannel().getName()).queue(); - } else if (event.getRemovedTags().contains(solvedTag) && event.getChannel().asThreadChannel().isLocked()) { + if (event.getAddedTags().contains(solvedTag) && !threadChannel.isLocked()) { + threadChannel.getManager().setLocked(true).queue(); + threadChannel.sendMessageEmbeds( + new PresetBuilder() + .withPreset( + new InformativeReply(InformativeReplyType.SUCCESS, "Post marked as solved") + ).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. - event.getChannel().asThreadChannel().getManager().setAppliedTags(event.getOldTags()).queue(); + threadChannel.getManager().setAppliedTags(event.getOldTags()).queue(); } }