-
Notifications
You must be signed in to change notification settings - Fork 25
Add Automated Help Channel Moderation System [Beta] #77
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Added beginning of stateless help channel management.
- Loading branch information
commit 88282121301874ec2303eeb33c50fbdeefefee11
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/javadiscord/javabot/help/HelpChannelListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.javadiscord.javabot.help; | ||
|
|
||
| import com.javadiscord.javabot.Bot; | ||
| import net.dv8tion.jda.api.entities.Category; | ||
| import net.dv8tion.jda.api.entities.TextChannel; | ||
| import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; | ||
| import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * This listener is responsible for handling messages that are sent in one or | ||
| * more designated help channels. | ||
| */ | ||
| public class HelpChannelListener extends ListenerAdapter { | ||
| @Override | ||
| public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) { | ||
| if (event.getAuthor().isBot() || event.getAuthor().isSystem()) return; | ||
|
|
||
| var config = Bot.config.get(event.getGuild()).getHelp(); | ||
| TextChannel channel = event.getChannel(); | ||
| Category category = channel.getParent(); | ||
| if (category == null) return; | ||
| if (channel.getName().startsWith(config.getOpenChannelPrefix())) { | ||
| String rawChannelName = channel.getName().substring(config.getOpenChannelPrefix().length()); | ||
| channel.getManager().setName(config.getReservedChannelPrefix() + rawChannelName).queue(); | ||
| channel.getManager().setPosition(category.getTextChannels().size()).queue(); | ||
| } | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/javadiscord/javabot/help/HelpChannelManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.javadiscord.javabot.help; | ||
|
|
||
| import com.javadiscord.javabot.properties.config.guild.HelpConfig; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.TextChannel; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class HelpChannelManager { | ||
| /** | ||
| * Opens a text channel so that it is ready for a new question. | ||
| * @param channel The channel to open. | ||
| * @param config The configuration properties. | ||
| */ | ||
| public void open(TextChannel channel, HelpConfig config) { | ||
| String rawName = channel.getName().substring(config.getReservedChannelPrefix().length()); | ||
| channel.getManager().setName(config.getOpenChannelPrefix() + rawName).queue(); | ||
| channel.getManager().setPosition(0).queue(); | ||
| removeAllMessages(channel); | ||
| } | ||
|
|
||
| /** | ||
| * Utility method to remove all messages from a channel. | ||
| * @param channel The channel to remove messages from. | ||
| */ | ||
| private void removeAllMessages(TextChannel channel) { | ||
| List<Message> messages; | ||
| do { | ||
| messages = channel.getHistory().retrievePast(50).complete(); | ||
| if (messages.isEmpty()) break; | ||
| if (messages.size() == 1) { | ||
| channel.deleteMessageById(messages.get(0).getIdLong()).complete(); | ||
| } else { | ||
| channel.deleteMessages(messages).complete(); | ||
| } | ||
| } while (!messages.isEmpty()); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/javadiscord/javabot/help/HelpChannelUpdater.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.javadiscord.javabot.help; | ||
|
|
||
| import com.javadiscord.javabot.Bot; | ||
| import com.javadiscord.javabot.properties.config.guild.HelpConfig; | ||
| import net.dv8tion.jda.api.JDA; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.TextChannel; | ||
| import net.dv8tion.jda.api.entities.User; | ||
|
|
||
| import java.time.OffsetDateTime; | ||
|
|
||
| public class HelpChannelUpdater implements Runnable { | ||
| private final JDA jda; | ||
| private final HelpChannelManager channelManager; | ||
|
|
||
| public HelpChannelUpdater(JDA jda) { | ||
| this.jda = jda; | ||
| this.channelManager = new HelpChannelManager(); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void run() { | ||
| for (var guild : this.jda.getGuilds()) { | ||
| var config = Bot.config.get(guild).getHelp(); | ||
| var channels = guild.getTextChannels(); | ||
| for (var channel : channels) { | ||
| if (channel.getName().startsWith(config.getReservedChannelPrefix())) { | ||
| this.checkReservedChannel(channel, config); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void checkReservedChannel(TextChannel channel, HelpConfig config) { | ||
| channel.getHistoryFromBeginning(1).queue(history -> { | ||
| if (history.isEmpty()) { | ||
| // Revert to open channel. | ||
| this.channelManager.open(channel, config); | ||
| } else { | ||
| Message firstMsg = history.getRetrievedHistory().get(0); | ||
| var channelBecomesInactiveAt = firstMsg.getTimeCreated().plusSeconds(config.getInactivityTimeoutSeconds()); | ||
| if (OffsetDateTime.now().isAfter(channelBecomesInactiveAt)) { | ||
| User user = firstMsg.getAuthor(); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/javadiscord/javabot/properties/config/guild/HelpConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.javadiscord.javabot.properties.config.guild; | ||
|
|
||
| import com.javadiscord.javabot.properties.config.GuildConfigItem; | ||
| import lombok.Data; | ||
| import lombok.EqualsAndHashCode; | ||
|
|
||
| @Data | ||
| @EqualsAndHashCode(callSuper = true) | ||
| public class HelpConfig extends GuildConfigItem { | ||
| /** | ||
| * The string which is prefixed to any open help channel, where users are | ||
| * free to ask a question. | ||
| */ | ||
| private String openChannelPrefix = "\uD83D\uDFE2"; | ||
|
|
||
| /** | ||
| * The string which is prefixed to any reserved help channel, where a user | ||
| * has already asked a question and is in the process of getting an answer. | ||
| */ | ||
| private String reservedChannelPrefix = "⛔"; | ||
|
|
||
| /** | ||
| * The string which is prefixed to any inactive reserved help channel, where | ||
| * a user has asked a question but the channel has been inactive for a set | ||
| * amount of time. | ||
| */ | ||
| private String inactiveChannelPrefix = "\uD83D\uDFE0"; | ||
|
|
||
| /** | ||
| * The number of seconds of inactivity before a channel is considered inactive. | ||
| */ | ||
| private int inactivityTimeoutSeconds = 1_800; | ||
|
|
||
| /** | ||
| * The number of seconds to wait between each help channel update check. | ||
| */ | ||
| private long updateIntervalSeconds = 60; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.