Skip to content

Commit 1d48423

Browse files
authored
remove locking behavior from ?solve (#106)
* 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 * fix: leftover debug
1 parent 8d2df10 commit 1d48423

File tree

6 files changed

+77
-37
lines changed

6 files changed

+77
-37
lines changed

src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static void initialize() throws LoginException {
141141
.setActivity(Activity.watching("for " + getConfig().getPrefix() + "help"))
142142
.setGatewayEncoding(GatewayEncoding.ETF)
143143
.disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.CLIENT_STATUS)
144-
.addEventListeners(new MessageEvent(), new ReadyEvent(), new GuildJoinEvent(), new ButtonEvent(), new MessageEditEvent(), new PostAppliedTagsEvent(), new ChannelCreatedEvent(), new ChannelArchiveEvent());
144+
.addEventListeners(new MessageEvent(), new ReadyEvent(), new GuildJoinEvent(), new ButtonEvent(), new MessageEditEvent(), new PostChannelEvent(), new ChannelCreatedEvent(), new ChannelUpdatedNameEvent());
145145

146146
jda = builder.build();
147147
CommandHandler.getInstance().initialize();

src/main/java/com/diamondfire/helpbot/bot/command/impl/other/util/SolvedCommand.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public String getName() {
2222
return "solved";
2323
}
2424

25+
@Override
26+
public String[] getAliases() {
27+
return new String[]{"solve", "resolved"};
28+
}
29+
2530
@Override
2631
public HelpContext getHelpContext() {
2732
return new HelpContext()
@@ -64,8 +69,8 @@ public void run(CommandEvent event) {
6469
return;
6570
}
6671

67-
// Check if the post is already locked.
68-
if (threadChannel.isLocked()) {
72+
// Check if the post already has the solved tag.
73+
if (threadChannel.getName().startsWith("[SOLVED] ")) {
6974
event.reply(new PresetBuilder()
7075
.withPreset(
7176
new InformativeReply(InformativeReplyType.ERROR, "Post is already solved.")
@@ -76,8 +81,11 @@ public void run(CommandEvent event) {
7681
// Apply the solved tag, other behavior handled by PostAppliedTagsEvent.
7782
ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag());
7883
ArrayList<ForumTag> appliedTags = new ArrayList<>(threadChannel.getAppliedTags());
79-
if (!appliedTags.contains(solvedTag)) appliedTags.add(solvedTag);
80-
84+
if (appliedTags.contains(solvedTag)) {
85+
appliedTags.remove(solvedTag);
86+
threadChannel.getManager().setAppliedTags(appliedTags).queue();
87+
}
88+
appliedTags.add(solvedTag);
8189
threadChannel.getManager().setAppliedTags(appliedTags).queue();
8290
}
8391

src/main/java/com/diamondfire/helpbot/bot/events/ChannelArchiveEvent.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/java/com/diamondfire/helpbot/bot/events/ChannelCreatedEvent.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public void onChannelCreate(ChannelCreateEvent event) {
2727
ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag());
2828
if (threadChannel.getAppliedTags().contains(solvedTag)) {
2929
ArrayList<ForumTag> appliedTags = new ArrayList<>(threadChannel.getAppliedTags());
30-
appliedTags.remove(solvedTag);
30+
// Solved tag is the only tag, and we need at least one tag.
31+
// In this case, this will do nothing, however ?solved will still change post's the name, so it's fine.
32+
if (appliedTags.size() != 1) {
33+
appliedTags.remove(solvedTag);
34+
}
3135
threadChannel.getManager().setAppliedTags(appliedTags).queue();
3236
}
3337

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.diamondfire.helpbot.bot.events;
2+
3+
import com.diamondfire.helpbot.bot.HelpBotInstance;
4+
import net.dv8tion.jda.api.entities.channel.ChannelType;
5+
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
6+
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
7+
import net.dv8tion.jda.api.events.channel.update.*;
8+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
public class ChannelUpdatedNameEvent extends ListenerAdapter {
12+
13+
@Override
14+
public void onChannelUpdateName(@NotNull ChannelUpdateNameEvent event) {
15+
// Limit to help forum.
16+
if (
17+
event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD ||
18+
event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()
19+
) {
20+
return;
21+
}
22+
23+
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
24+
ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag());
25+
26+
if (event.getOldValue() == null || event.getNewValue() == null) return;
27+
28+
// If the post starts with [SOLVED] and has a solved tag, but renamed to not have [SOLVED], then revert to the old name.
29+
if (event.getOldValue().startsWith("[SOLVED] ") && !event.getNewValue().startsWith("[SOLVED] ") && threadChannel.getAppliedTags().contains(solvedTag)) {
30+
threadChannel.getManager().setName(event.getOldValue()).queue();
31+
// 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.
32+
} else if (!event.getOldValue().startsWith("[SOLVED] ") && event.getNewValue().startsWith("[SOLVED] ") && !threadChannel.getAppliedTags().contains(solvedTag)) {
33+
threadChannel.getManager().setName(event.getOldValue()).queue();
34+
}
35+
36+
}
37+
38+
}

src/main/java/com/diamondfire/helpbot/bot/events/PostAppliedTagsEvent.java renamed to src/main/java/com/diamondfire/helpbot/bot/events/PostChannelEvent.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import com.diamondfire.helpbot.bot.HelpBotInstance;
44
import com.diamondfire.helpbot.bot.command.reply.*;
55
import com.diamondfire.helpbot.bot.command.reply.feature.informative.*;
6+
import net.dv8tion.jda.api.entities.channel.ChannelType;
67
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
78
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
89
import net.dv8tion.jda.api.entities.channel.unions.ChannelUnion;
9-
import net.dv8tion.jda.api.events.channel.update.ChannelUpdateAppliedTagsEvent;
10+
import net.dv8tion.jda.api.events.channel.update.*;
1011
import net.dv8tion.jda.api.hooks.ListenerAdapter;
1112

12-
public class PostAppliedTagsEvent extends ListenerAdapter {
13+
public class PostChannelEvent extends ListenerAdapter {
1314

1415
@Override
1516
public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) {
@@ -22,20 +23,33 @@ public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) {
2223

2324
ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag());
2425

25-
// If the solved tag is added and the post is not locked, lock the thread.
26-
if (event.getAddedTags().contains(solvedTag) && !threadChannel.isLocked()) {
27-
threadChannel.getManager().setLocked(true).queue();
26+
// If the solved tag is added, add [SOLVED] to the thread's name.
27+
if (event.getAddedTags().contains(solvedTag) && !threadChannel.getName().startsWith("[SOLVED] ")) {
2828
threadChannel.sendMessageEmbeds(
2929
new PresetBuilder()
3030
.withPreset(
3131
new InformativeReply(InformativeReplyType.SUCCESS, "Post marked as solved")
3232
).getEmbed().build()
3333
).queue();
3434
threadChannel.getManager().setName("[SOLVED] " + channel.getName()).queue();
35-
} else if (event.getRemovedTags().contains(solvedTag) && threadChannel.isLocked()) {
36-
// If the solved tag is removed and the post is locked, put the old tags back.
35+
} else if (event.getRemovedTags().contains(solvedTag) && threadChannel.getName().startsWith("[SOLVED] ")) {
36+
// If the solved tag is removed and the post has [SOLVED] in its name, put the old tags back.
3737
threadChannel.getManager().setAppliedTags(event.getOldTags()).queue();
3838
}
3939
}
4040

41+
@Override
42+
public void onChannelUpdateArchived(ChannelUpdateArchivedEvent event) {
43+
// Limit to help forum.
44+
if (
45+
event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD ||
46+
event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()
47+
) {
48+
return;
49+
}
50+
51+
// When a post is archived, it should be locked.
52+
event.getChannel().asThreadChannel().getManager().setLocked(true).queue();
53+
}
54+
4155
}

0 commit comments

Comments
 (0)