Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added beginning of stateless help channel management.
  • Loading branch information
andrewlalis committed Sep 25, 2021
commit 88282121301874ec2303eeb33c50fbdeefefee11
7 changes: 6 additions & 1 deletion src/main/java/com/javadiscord/javabot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.javadiscord.javabot.data.H2DataSource;
import com.javadiscord.javabot.events.*;
import com.javadiscord.javabot.help.HelpChannelListener;
import com.javadiscord.javabot.help.HelpChannelUpdater;
import com.javadiscord.javabot.properties.config.BotConfig;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand All @@ -16,6 +18,7 @@
import java.util.TimeZone;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* The main class where the bot is initialized.
Expand Down Expand Up @@ -74,6 +77,7 @@ public static void main(String[] args) throws Exception {
.addEventListeners(slashCommands)
.build();
addEventListeners(jda);
asyncPool.scheduleAtFixedRate(new HelpChannelUpdater(jda), 1, 1, TimeUnit.SECONDS);
}

/**
Expand All @@ -92,7 +96,8 @@ private static void addEventListeners(JDA jda) {
new AutoMod(),
new SubmissionListener(),
new StarboardListener(),
new InteractionListener()
new InteractionListener(),
new HelpChannelListener()
);
}
}
Expand Down
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();
}
}
}
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());
}
}
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();
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class GuildConfig {
private transient Path file;

private SlashCommandConfig slashCommand;
private HelpConfig help;
private ModerationConfig moderation;
private QOTWConfig qotw;
private WelcomeConfig welcome;
Expand All @@ -38,6 +39,7 @@ public GuildConfig(Guild guild, Path file) {
this.file = file;
// Initialize all config items.
this.slashCommand = new SlashCommandConfig();
this.help = new HelpConfig();
this.moderation = new ModerationConfig();
this.qotw = new QOTWConfig();
this.welcome = new WelcomeConfig();
Expand All @@ -50,6 +52,7 @@ public GuildConfig(Guild guild, Path file) {
private void setGuild(Guild guild) {
this.guild = guild;
this.slashCommand.setGuildConfig(this);
this.help.setGuildConfig(this);
this.moderation.setGuildConfig(this);
this.qotw.setGuildConfig(this);
this.welcome.setGuildConfig(this);
Expand Down
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 = "⛔";
Comment thread
andrewlalis marked this conversation as resolved.
Outdated

/**
* 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;
}