Added support for server thread channels.

When mentioned in a server thread channel, the parent channel's Markov chain is used to generate responses. This avoids low quality sentences which would occur if every server thread channel was given a brand new Markov chain.

Messages received from server thread channels do not get ingested.
This commit is contained in:
Logan Fick 2022-07-05 16:35:40 -04:00
parent c183ed805e
commit e4abb0d84d
Signed by: LogalDeveloper
GPG Key ID: 43E58A0C922AB7D1
2 changed files with 24 additions and 12 deletions

View File

@ -9,32 +9,38 @@
package dev.logal.crabstero.listeners;
import dev.logal.crabstero.utils.MarkovChainMessages;
import org.javacord.api.entity.channel.ServerTextChannel;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageAuthor;
import org.javacord.api.entity.message.MessageType;
import org.javacord.api.event.message.MessageCreateEvent;
import org.javacord.api.listener.message.MessageCreateListener;
import java.util.Optional;
public final class MessageCreate implements MessageCreateListener {
@Override
public void onMessageCreate(final MessageCreateEvent event) {
final MessageAuthor author = event.getMessageAuthor();
final Optional<ServerTextChannel> serverTextChannel = event.getChannel().asServerTextChannel();
if (!serverTextChannel.isPresent() || author.isBotUser() || author.isWebhook() || author.isYourself()) {
return;
// Is this message being created outside of a server text channel or a server thread channel?
final TextChannel channel = event.getChannel();
if (channel.asServerTextChannel().isEmpty() && channel.asServerThreadChannel().isEmpty()) {
return; // Yes. Ignore it.
}
// Is the user creating this message another bot, a webhook, or Crabstero itself?
final MessageAuthor author = event.getMessageAuthor();
if (author.isBotUser() || author.isWebhook() || author.isYourself()) {
return; // Yes. Ignore it.
}
// Is this message mentioning Crabstero?
final Message message = event.getMessage();
if (message.getMentionedUsers().contains(event.getApi().getYourself())) {
MarkovChainMessages.replyToMessage(message);
return;
MarkovChainMessages.replyToMessage(message); // Yes. Respond to it.
return; // Prevent further processing of the message.
}
if (message.getType() == MessageType.NORMAL) {
MarkovChainMessages.ingestMessage(message);
// Is this message a normal message outside of a server thread channel?
if (message.getType() == MessageType.NORMAL && channel.asServerThreadChannel().isEmpty()) {
MarkovChainMessages.ingestMessage(message); // Yes. Ingest the message.
}
}
}

View File

@ -46,7 +46,13 @@ public class MarkovChainMessages {
return;
}
final long channelID = channel.getId();
final long channelID;
if (channel.asServerThreadChannel().isPresent()) {
channelID = channel.asServerThreadChannel().get().getParent().getId();
} else {
channelID = channel.getId();
}
final MessageBuilder response = new MessageBuilder();
final MarkovChain markovChain = new MarkovChain(channelID);