Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove locking behavior from ?solve #106

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.")
Expand All @@ -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<ForumTag> 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);
RedVortexDev marked this conversation as resolved.
Show resolved Hide resolved
threadChannel.getManager().setAppliedTags(appliedTags).queue();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public void onChannelCreate(ChannelCreateEvent event) {
ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag());
if (threadChannel.getAppliedTags().contains(solvedTag)) {
ArrayList<ForumTag> 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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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;

// 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();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -22,20 +23,33 @@ 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(
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.
} 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();
}

}
Loading