Skip to content

Commit 8d2df10

Browse files
authored
Help Improvements (#104)
* feat: in help forum, ?solve & lock closed posts * fix: duplicate behavior from ?solved * style: remove var usage & set event.getChannel()s to a variable
1 parent bfe400f commit 8d2df10

File tree

6 files changed

+195
-1
lines changed

6 files changed

+195
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static void initialize() throws LoginException {
5353
// others
5454
//new CowsayCommand(),
5555
new MimicCommand(),
56+
new SolvedCommand(),
5657
//new FetchDataCommand(),
5758
new InfoCommand(),
5859
new EvalCommand(),
@@ -140,7 +141,7 @@ public static void initialize() throws LoginException {
140141
.setActivity(Activity.watching("for " + getConfig().getPrefix() + "help"))
141142
.setGatewayEncoding(GatewayEncoding.ETF)
142143
.disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.CLIENT_STATUS)
143-
.addEventListeners(new MessageEvent(), new ReadyEvent(), new GuildJoinEvent(), new ButtonEvent(), new MessageEditEvent());
144+
.addEventListeners(new MessageEvent(), new ReadyEvent(), new GuildJoinEvent(), new ButtonEvent(), new MessageEditEvent(), new PostAppliedTagsEvent(), new ChannelCreatedEvent(), new ChannelArchiveEvent());
144145

145146
jda = builder.build();
146147
CommandHandler.getInstance().initialize();
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.diamondfire.helpbot.bot.command.impl.other.util;
2+
3+
import com.diamondfire.helpbot.bot.HelpBotInstance;
4+
import com.diamondfire.helpbot.bot.command.argument.ArgumentSet;
5+
import com.diamondfire.helpbot.bot.command.help.*;
6+
import com.diamondfire.helpbot.bot.command.impl.Command;
7+
import com.diamondfire.helpbot.bot.command.permissions.Permission;
8+
import com.diamondfire.helpbot.bot.command.reply.PresetBuilder;
9+
import com.diamondfire.helpbot.bot.command.reply.feature.informative.*;
10+
import com.diamondfire.helpbot.bot.events.CommandEvent;
11+
import net.dv8tion.jda.api.entities.channel.ChannelType;
12+
import net.dv8tion.jda.api.entities.channel.concrete.*;
13+
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
14+
15+
import java.util.*;
16+
17+
18+
public class SolvedCommand extends Command {
19+
20+
@Override
21+
public String getName() {
22+
return "solved";
23+
}
24+
25+
@Override
26+
public HelpContext getHelpContext() {
27+
return new HelpContext()
28+
.description("Marks the help post as solved.")
29+
.category(CommandCategory.OTHER);
30+
}
31+
32+
@Override
33+
public ArgumentSet compileArguments() {
34+
return new ArgumentSet();
35+
}
36+
37+
@Override
38+
public Permission getPermission() {
39+
return Permission.USER;
40+
}
41+
42+
@Override
43+
public void run(CommandEvent event) {
44+
// Limit to help forum.
45+
if (
46+
event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD ||
47+
event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()
48+
) {
49+
event.reply(new PresetBuilder()
50+
.withPreset(
51+
new InformativeReply(InformativeReplyType.ERROR, "Command can only be used in <#" + HelpBotInstance.getConfig().getHelpChannel() + ">")
52+
));
53+
return;
54+
}
55+
56+
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
57+
58+
// Check if the command is used by the post owner.
59+
if (event.getMember() == null | threadChannel.getOwnerIdLong() != event.getMember().getIdLong()) {
60+
event.reply(new PresetBuilder()
61+
.withPreset(
62+
new InformativeReply(InformativeReplyType.ERROR, "Command can only be used by the post owner.")
63+
));
64+
return;
65+
}
66+
67+
// Check if the post is already locked.
68+
if (threadChannel.isLocked()) {
69+
event.reply(new PresetBuilder()
70+
.withPreset(
71+
new InformativeReply(InformativeReplyType.ERROR, "Post is already solved.")
72+
));
73+
return;
74+
}
75+
76+
// Apply the solved tag, other behavior handled by PostAppliedTagsEvent.
77+
ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag());
78+
ArrayList<ForumTag> appliedTags = new ArrayList<>(threadChannel.getAppliedTags());
79+
if (!appliedTags.contains(solvedTag)) appliedTags.add(solvedTag);
80+
81+
threadChannel.getManager().setAppliedTags(appliedTags).queue();
82+
}
83+
84+
}

src/main/java/com/diamondfire/helpbot/bot/config/Config.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public long getPurgeEvidenceChannel() {
7979
return getPropertyLong("purge_evidence_channel");
8080
}
8181

82+
public long getHelpChannel() {
83+
return getPropertyLong("help_channel");
84+
}
85+
86+
public long getHelpChannelSolvedTag() {
87+
return getPropertyLong("help_channel_solved_tag");
88+
}
89+
8290
public long getMutedRole() {
8391
return getPropertyLong("muted_role");
8492
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.events.channel.update.ChannelUpdateArchivedEvent;
6+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
7+
8+
public class ChannelArchiveEvent extends ListenerAdapter {
9+
10+
@Override
11+
public void onChannelUpdateArchived(ChannelUpdateArchivedEvent event) {
12+
// Limit to help forum.
13+
if (
14+
event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD ||
15+
event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()
16+
) {
17+
return;
18+
}
19+
20+
// When a post is archived, it should be locked.
21+
event.getChannel().asThreadChannel().getManager().setLocked(true).queue();
22+
}
23+
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.ChannelCreateEvent;
8+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
9+
10+
import java.util.ArrayList;
11+
12+
public class ChannelCreatedEvent extends ListenerAdapter {
13+
14+
@Override
15+
public void onChannelCreate(ChannelCreateEvent event) {
16+
// Limit to help forum.
17+
if (
18+
event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD ||
19+
event.getChannel().asThreadChannel().getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()
20+
) {
21+
return;
22+
}
23+
24+
// Remove solved tag if post was created with it.
25+
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
26+
27+
ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag());
28+
if (threadChannel.getAppliedTags().contains(solvedTag)) {
29+
ArrayList<ForumTag> appliedTags = new ArrayList<>(threadChannel.getAppliedTags());
30+
appliedTags.remove(solvedTag);
31+
threadChannel.getManager().setAppliedTags(appliedTags).queue();
32+
}
33+
34+
}
35+
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.diamondfire.helpbot.bot.events;
2+
3+
import com.diamondfire.helpbot.bot.HelpBotInstance;
4+
import com.diamondfire.helpbot.bot.command.reply.*;
5+
import com.diamondfire.helpbot.bot.command.reply.feature.informative.*;
6+
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
7+
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
8+
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.hooks.ListenerAdapter;
11+
12+
public class PostAppliedTagsEvent extends ListenerAdapter {
13+
14+
@Override
15+
public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) {
16+
// Limit to help forum.
17+
ChannelUnion channel = event.getChannel();
18+
ThreadChannel threadChannel = channel.asThreadChannel();
19+
if (threadChannel.getParentChannel().getIdLong() != HelpBotInstance.getConfig().getHelpChannel()) {
20+
return;
21+
}
22+
23+
ForumTag solvedTag = threadChannel.getParentChannel().asForumChannel().getAvailableTagById(HelpBotInstance.getConfig().getHelpChannelSolvedTag());
24+
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();
28+
threadChannel.sendMessageEmbeds(
29+
new PresetBuilder()
30+
.withPreset(
31+
new InformativeReply(InformativeReplyType.SUCCESS, "Post marked as solved")
32+
).getEmbed().build()
33+
).queue();
34+
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.
37+
threadChannel.getManager().setAppliedTags(event.getOldTags()).queue();
38+
}
39+
}
40+
41+
}

0 commit comments

Comments
 (0)