Skip to content

Commit

Permalink
Help Improvements (#104)
Browse files Browse the repository at this point in the history
* feat: in help forum, ?solve & lock closed posts

* fix: duplicate behavior from ?solved

* style: remove var usage & set event.getChannel()s to a variable
  • Loading branch information
RedVortexDev authored Nov 16, 2024
1 parent bfe400f commit 8d2df10
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static void initialize() throws LoginException {
// others
//new CowsayCommand(),
new MimicCommand(),
new SolvedCommand(),
//new FetchDataCommand(),
new InfoCommand(),
new EvalCommand(),
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ForumTag> appliedTags = new ArrayList<>(threadChannel.getAppliedTags());
if (!appliedTags.contains(solvedTag)) appliedTags.add(solvedTag);

threadChannel.getManager().setAppliedTags(appliedTags).queue();
}

}
8 changes: 8 additions & 0 deletions src/main/java/com/diamondfire/helpbot/bot/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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<ForumTag> appliedTags = new ArrayList<>(threadChannel.getAppliedTags());
appliedTags.remove(solvedTag);
threadChannel.getManager().setAppliedTags(appliedTags).queue();
}

}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}

}

0 comments on commit 8d2df10

Please sign in to comment.