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..3bf2d991 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/util/SolvedCommand.java @@ -0,0 +1,84 @@ +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.ChannelType; +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) { + // 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() + ">") + )); + 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, 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); + + threadChannel.getManager().setAppliedTags(appliedTags).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..61166268 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java @@ -0,0 +1,36 @@ +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.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. + 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); + 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 new file mode 100644 index 00000000..48b7033c --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java @@ -0,0 +1,41 @@ +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.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; + +public class PostAppliedTagsEvent extends ListenerAdapter { + + @Override + public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) { + // Limit to help forum. + ChannelUnion channel = event.getChannel(); + ThreadChannel threadChannel = channel.asThreadChannel(); + if (threadChannel.getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()) { + return; + } + + 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(); + 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. + threadChannel.getManager().setAppliedTags(event.getOldTags()).queue(); + } + } + +}