Skip to content

Commit

Permalink
generate javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
yannicklamprecht committed Dec 14, 2024
1 parent 9bbcdda commit ec05a29
Show file tree
Hide file tree
Showing 168 changed files with 4,951 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@
import de.chojo.repbot.dao.provider.Guilds;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;

/**
* Handler for analyzing messages in the context of a guild.
*/
public class MessageAnalyzer implements MessageHandler {
private final Guilds guilds;

/**
* Constructs a new MessageAnalyzer handler.
*
* @param guilds the Guilds provider
*/
public MessageAnalyzer(Guilds guilds) {
this.guilds = guilds;
}

/**
* Handles the message context interaction event.
*
* @param event the MessageContextInteractionEvent
* @param eventContext the EventContext
*/
@Override
public void onMessage(MessageContextInteractionEvent event, EventContext eventContext) {
Analyzer.sendAnalyzerLog(event, guilds, event.getTarget().getIdLong(), eventContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@
import de.chojo.jdautil.interactions.user.UserHandler;
import de.chojo.jdautil.wrapper.EventContext;
import de.chojo.repbot.commands.log.handler.Donated;
import de.chojo.repbot.commands.log.handler.Received;
import de.chojo.repbot.dao.provider.Guilds;
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;

/**
* Handles the donated reputation user context interaction.
*/
public class DonatedReputation implements UserHandler {
private final Guilds guilds;

/**
* Constructs a new DonatedReputation handler.
*
* @param guilds the Guilds provider
*/
public DonatedReputation(Guilds guilds) {
this.guilds = guilds;
}


/**
* Handles the user context interaction event.
*
* @param event the user context interaction event
* @param eventContext the event context
*/
@Override
public void onUser(UserContextInteractionEvent event, EventContext eventContext) {
Donated.send(event, event.getTargetMember(), guilds, eventContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@
import de.chojo.repbot.dao.provider.Guilds;
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;

/**
* Handles the received reputation user context interaction.
*/
public class ReceivedReputation implements UserHandler {
private final Guilds guilds;

/**
* Constructs a ReceivedReputation handler with the specified guilds provider.
*
* @param guilds the guilds provider
*/
public ReceivedReputation(Guilds guilds) {
this.guilds = guilds;
}


/**
* Handles the user context interaction event.
*
* @param event the user context interaction event
* @param eventContext the event context
*/
@Override
public void onUser(UserContextInteractionEvent event, EventContext eventContext) {
Received.send(event, event.getTargetMember(), guilds, eventContext);
Expand Down
93 changes: 73 additions & 20 deletions src/main/java/de/chojo/repbot/analyzer/ContextResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

import static org.slf4j.LoggerFactory.getLogger;

/**
* Class responsible for resolving context for messages and voice states.
*/
public class ContextResolver {
private static final Logger log = getLogger(ContextResolver.class);
private final Voice voiceData;
Expand All @@ -46,31 +49,43 @@ public class ContextResolver {
.expireAfterWrite(10, TimeUnit.SECONDS)
.build();

/**
* Constructs a ContextResolver instance with the specified voice data and configuration.
*
* @param voiceData the voice data provider
* @param configuration the configuration settings
*/
public ContextResolver(Voice voiceData, Configuration configuration) {
this.voiceData = voiceData;
this.configuration = configuration;
}

/**
* Get members which have written in the channel of the message.
* <p>
* Only members which have written in the last 100 messages which are not older than the max history and are not
* send before the first message of the message author are returned
* Gets the context of members who have written in the channel of the specified message.
*
* @param message message to determine channel, author and start time
* @param settings setting sof the guild.
* @return list of members which have written in this channel
* @param target the target member
* @param message the message to determine channel, author, and start time
* @param settings the settings of the guild
* @return the message context
*/
@NotNull
public MessageContext getChannelContext(Member target, Message message, @Nullable Settings settings) {
try {
return messageContextCache.get(message.getIdLong(), () -> retrieveChannelContext(target, message, settings).resolve());
} catch (ExecutionException e) {
log.error("Could not conpute channel context.", e);
log.error("Could not compute channel context.", e);
}
return MessageContext.byMessageAndMember(message, target);
}

/**
* Retrieves the channel context for the specified message.
*
* @param target the target member
* @param message the message to determine channel, author, and start time
* @param settings the settings of the guild
* @return the message context
*/
private MessageContext retrieveChannelContext(Member target, Message message, Settings settings) {
var history = message.getChannel().getHistoryBefore(message, configuration.analyzerSettings().historySize())
.complete();
Expand All @@ -92,12 +107,12 @@ private MessageContext retrieveChannelContext(Member target, Message message, Se
}

/**
* Add the latest authors.
* Adds the latest authors to the context.
* <p>
* Authors are considered latest when they have written a message in the last {@link AbuseProtection#minMessages()}
* Authors are considered latest when they have written a message in the last {@link AbuseProtection#minMessages()}.
*
* @param context context to add
* @param settings settings
* @param context the message context
* @param settings the settings of the guild
*/
private void addLatestAuthors(MessageContext context, @Nullable Settings settings) {
var maxAge = Instant.now().minus(configuration.analyzerSettings().latestMaxHours(), ChronoUnit.HOURS);
Expand All @@ -111,12 +126,12 @@ private void addLatestAuthors(MessageContext context, @Nullable Settings setting
}

/**
* Add the recent authors.
* Adds the recent authors to the context.
* <p>
* Authors are considered recent when they have written a message in the {@link AbuseProtection#maxMessageAge()}
* Authors are considered recent when they have written a message in the {@link AbuseProtection#maxMessageAge()}.
*
* @param context context to add
* @param settings settings
* @param context the message context
* @param settings the settings of the guild
*/
private void addRecentAuthors(MessageContext context, Settings settings) {
var maxAge = Instant.now().minus(settings == null ? Long.MAX_VALUE : settings.abuseProtection()
Expand All @@ -128,11 +143,11 @@ private void addRecentAuthors(MessageContext context, Settings settings) {
}

/**
* Add the ids and messages which were newer than oldest to the context
* Adds the IDs and messages which were newer than the oldest to the context.
*
* @param messages messages
* @param context context
* @param oldest oldest allowed message
* @param messages the messages
* @param context the message context
* @param oldest the oldest allowed message
*/
private void addMembersAfter(Collection<Message> messages, MessageContext context, Instant oldest) {
// filter message for only recent messages and after the first message of the user.
Expand All @@ -151,6 +166,13 @@ private void addMembersAfter(Collection<Message> messages, MessageContext contex
context.addIds(memberIds);
}

/**
* Finds the oldest message by the target member in the context.
*
* @param context the message context
* @param maxAge the maximum age of the message
* @return the oldest message instant
*/
private Instant findOldestMessageByTarget(MessageContext context, Instant maxAge) {
return context.rawMessages().stream()
.filter(mes -> Verifier.equalSnowflake(mes.getAuthor(), context.user()))
Expand All @@ -160,6 +182,14 @@ private Instant findOldestMessageByTarget(MessageContext context, Instant maxAge
.orElse(maxAge);
}

/**
* Gets the voice context for the specified message.
*
* @param target the target member
* @param message the message to determine channel, author, and start time
* @param settings the settings of the guild
* @return the message context
*/
public MessageContext getVoiceContext(Member target, Message message, @Nullable Settings settings) {
try {
return voiceContextCache.get(message.getIdLong(), () -> retrieveVoiceContext(target, message, settings).resolve()
Expand All @@ -170,6 +200,14 @@ public MessageContext getVoiceContext(Member target, Message message, @Nullable
return MessageContext.byMessageAndMember(message, target);
}

/**
* Retrieves the voice context for the specified message.
*
* @param target the target member
* @param message the message to determine channel, author, and start time
* @param settings the settings of the guild
* @return the message context
*/
private MessageContext retrieveVoiceContext(Member target, Message message, @Nullable Settings settings) {
var context = MessageContext.byMessageAndMember(message, target);
var voiceState = target.getVoiceState();
Expand All @@ -193,10 +231,25 @@ private MessageContext retrieveVoiceContext(Member target, Message message, @Nul
return context;
}

/**
* Gets the combined context for the specified message.
*
* @param message the message to determine channel, author, and start time
* @param settings the settings of the guild
* @return the combined message context
*/
public MessageContext getCombinedContext(Message message, @Nullable Settings settings) {
return getCombinedContext(message.getMember(), message, settings);
}

/**
* Gets the combined context for the specified member and message.
*
* @param target the target member
* @param message the message to determine channel, author, and start time
* @param settings the settings of the guild
* @return the combined message context
*/
public MessageContext getCombinedContext(Member target, Message message, @Nullable Settings settings) {
return getChannelContext(target, message, settings)
.combine(getVoiceContext(target, message, settings));
Expand Down
Loading

0 comments on commit ec05a29

Please sign in to comment.