Skip to content
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

Task/gen javadoc #650

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/main/java/de/chojo/repbot/ReputationBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,26 @@
import java.io.IOException;
import java.sql.SQLException;

/**
* Main class for the ReputationBot application.
*/
public class ReputationBot {
private static ReputationBot instance;

/**
* Creates a new ReputationBot instance.
*/
private ReputationBot() {
}

/**
* Main method to start the ReputationBot application.
*
* @param args the command line arguments
* @throws SQLException If the database connection fails.
* @throws IOException If the configuration file fails to load.
* @throws LoginException If the bot login fails.
*/
public static void main(String[] args) throws SQLException, IOException, LoginException {
ReputationBot.instance = new ReputationBot();
instance.start();
Expand All @@ -30,6 +47,7 @@ public static void main(String[] args) throws SQLException, IOException, LoginEx
*
* @throws SQLException If the database connection fails.
* @throws IOException If the configuration file fails to load.
* @throws LoginException If the bot login fails.
*/
private void start() throws SQLException, IOException, LoginException {
var configuration = Configuration.create();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/de/chojo/repbot/actions/messages/log/MessageLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@
import de.chojo.repbot.dao.provider.Guilds;
import net.dv8tion.jda.api.Permission;

/**
* Provides a message log functionality for the bot.
*/
public class MessageLog implements MessageProvider<Message> {
private final Guilds guilds;

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

/**
* Returns a configured message for logging.
*
* @return the configured message
*/
@Override
public Message message() {
return Message.of("Message Log")
Expand Down
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 @@ -11,13 +11,29 @@
import de.chojo.repbot.dao.provider.Guilds;
import net.dv8tion.jda.api.Permission;

/**
* Class for handling user donated actions.
*/
public class UserDonated implements UserProvider<User> {
/**
* Provider for accessing guild data.
*/
private final Guilds guilds;

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

/**
* Creates and returns a User object configured for donated reputation.
*
* @return the configured User object
*/
@Override
public User user() {
return User.of("Given Reputation").handler(new DonatedReputation(guilds))
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,13 +11,29 @@
import de.chojo.repbot.dao.provider.Guilds;
import net.dv8tion.jda.api.Permission;

/**
* Class for handling user received actions.
*/
public class UserReceived implements UserProvider<User> {
/**
* Provider for accessing guild data.
*/
private final Guilds guilds;

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

/**
* Creates and returns a User object configured for received reputation.
*
* @return the configured User object
*/
@Override
public User user() {
return User.of("Received Reputation").handler(new ReceivedReputation(guilds))
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
88 changes: 72 additions & 16 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,6 +49,12 @@ 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;
Expand All @@ -57,20 +66,29 @@ public ContextResolver(Voice voiceData, Configuration configuration) {
* 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
*
* @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 +110,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 +129,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 +146,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 +169,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 +185,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 +203,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 +234,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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Gets the combined context for the specified member and message.
* Gets the combined voice and message 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
Loading