Search in sources :

Example 6 with JamSubmission

use of net.javadiscord.javabot.systems.jam.model.JamSubmission in project JavaBot by Java-Discord.

the class ToSubmissionVotingTransition method transition.

@Override
public void transition(Jam jam, SlashCommandInteractionEvent event, JamChannelManager channelManager, Connection con) throws SQLException {
    List<JamSubmission> submissions = new JamSubmissionRepository(con).getSubmissions(jam);
    if (submissions.isEmpty()) {
        throw new IllegalStateException("Cannot start submission voting because there aren't any submissions.");
    }
    JamMessageRepository messageRepository = new JamMessageRepository(con);
    var messageIds = channelManager.sendSubmissionVotingMessage(jam, submissions, event.getJDA());
    for (var entry : messageIds.entrySet()) {
        messageRepository.saveMessageId(jam, entry.getValue(), "submission-" + entry.getKey().getId());
    }
    new JamRepository(con).updateJamPhase(jam, JamPhase.SUBMISSION_VOTING);
}
Also used : JamSubmissionRepository(net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) JamMessageRepository(net.javadiscord.javabot.systems.jam.dao.JamMessageRepository) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository)

Example 7 with JamSubmission

use of net.javadiscord.javabot.systems.jam.model.JamSubmission in project JavaBot by Java-Discord.

the class JamChannelManager method getSubmissionVotes.

/**
 * Gets a list of user ids for each Jam submission, indicating the list of
 * users that voted for the submission.
 *
 * @param submissionMessageMap A map containing for each submission, the id
 *                             of the message on which users will react with
 *                             the vote emoji.
 * @return A map containing a list of user ids for each Jam submission.
 */
public Map<JamSubmission, List<Long>> getSubmissionVotes(Map<JamSubmission, Long> submissionMessageMap) {
    Map<JamSubmission, List<Long>> votesMap = new HashMap<>();
    OffsetDateTime cutoff = OffsetDateTime.now();
    for (var entry : submissionMessageMap.entrySet()) {
        Message message = this.config.getVotingChannel().retrieveMessageById(entry.getValue()).complete();
        Guild guild = message.getGuild();
        List<User> users = message.retrieveReactionUsers(JamPhaseManager.SUBMISSION_VOTE_UNICODE).complete();
        votesMap.put(entry.getKey(), users.stream().filter(user -> isUserVoteValid(guild, user, cutoff)).map(ISnowflake::getIdLong).toList());
    }
    return votesMap;
}
Also used : ColorUtils(net.javadiscord.javabot.util.ColorUtils) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) JDA(net.dv8tion.jda.api.JDA) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) HashMap(java.util.HashMap) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) Jam(net.javadiscord.javabot.systems.jam.model.Jam) JamTheme(net.javadiscord.javabot.systems.jam.model.JamTheme) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) JamConfig(net.javadiscord.javabot.data.config.guild.JamConfig) Map(java.util.Map) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) HashMap(java.util.HashMap) OffsetDateTime(java.time.OffsetDateTime) List(java.util.List)

Example 8 with JamSubmission

use of net.javadiscord.javabot.systems.jam.model.JamSubmission in project JavaBot by Java-Discord.

the class JamChannelManager method sendSubmissionVotingMessage.

/**
 * Sends a message in the Jam voting channel for each submission, so that
 * users can begin voting on submissions.
 *
 * @param jam         The jam that the submissions are for.
 * @param submissions The submissions to make a message for.
 * @param jda         The JDA instance.
 * @return A map containing for every submission, the id of the message that
 * users will react with votes on.
 */
public Map<JamSubmission, Long> sendSubmissionVotingMessage(Jam jam, List<JamSubmission> submissions, JDA jda) {
    this.removeAllMessages(this.config.getVotingChannel());
    Map<JamSubmission, Long> messageIds = new HashMap<>();
    for (JamSubmission submission : submissions) {
        User user = jda.getUserById(submission.getUserId());
        String userName = user == null ? "Unknown user" : user.getAsTag();
        EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Submission by " + userName, submission.getSourceLink()).setColor(ColorUtils.randomPastel()).setTimestamp(submission.getCreatedAt()).addField("Description", submission.getDescription(), false);
        Message message = this.config.getVotingChannel().sendMessageEmbeds(embedBuilder.build()).complete();
        message.addReaction(JamPhaseManager.SUBMISSION_VOTE_UNICODE).complete();
        messageIds.put(submission, message.getIdLong());
    }
    EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Voting Has Begun!").setColor(this.config.getJamEmbedColor()).setDescription(String.format("Go to %s to vote for who you think should win the %s.", this.config.getVotingChannel().getAsMention(), jam.getFullName()));
    this.config.getAnnouncementChannel().sendMessageEmbeds(embedBuilder.build()).queue();
    this.pingRole();
    return messageIds;
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) HashMap(java.util.HashMap)

Example 9 with JamSubmission

use of net.javadiscord.javabot.systems.jam.model.JamSubmission in project JavaBot by Java-Discord.

the class JamSubmissionRepository method getSubmissions.

/**
 * Gets all submission based on the given {@link Jam}.
 *
 * @param jam The {@link Jam}.
 * @return A {@link List} with all {@link JamSubmission}s.
 * @throws SQLException If an error occurs.
 */
public List<JamSubmission> getSubmissions(Jam jam) throws SQLException {
    PreparedStatement stmt = con.prepareStatement(StringResourceCache.load("/jam/sql/find_latest_submissions.sql"));
    stmt.setLong(1, jam.getId());
    ResultSet rs = stmt.executeQuery();
    List<JamSubmission> submissions = new ArrayList<>();
    while (rs.next()) {
        submissions.add(this.readSubmission(rs, jam));
    }
    stmt.close();
    return submissions;
}
Also used : JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 10 with JamSubmission

use of net.javadiscord.javabot.systems.jam.model.JamSubmission in project JavaBot by Java-Discord.

the class JamSubmissionRepository method getSubmissions.

/**
 * Gets all submission based on the given {@link Jam} and user id.
 *
 * @param jam    The {@link Jam}.
 * @param page   The page.
 * @param userId The user's id.
 * @return A {@link List} with all {@link JamSubmission}s.
 * @throws SQLException If an error occurs.
 */
public List<JamSubmission> getSubmissions(Jam jam, int page, Long userId) throws SQLException {
    int pageSize = 10;
    String sql = "SELECT js.* FROM jam_submission js WHERE js.jam_id = ? /* CONDITIONS */ ORDER BY js.created_at LIMIT 10 /* OFFSET */".replace("/* OFFSET */", "OFFSET " + (page - 1) * pageSize);
    if (userId != null) {
        sql = sql.replace("/* CONDITIONS */", "AND js.user_id = ?");
    }
    PreparedStatement stmt = con.prepareStatement(sql);
    stmt.setLong(1, jam.getId());
    if (userId != null) {
        stmt.setLong(2, userId);
    }
    ResultSet rs = stmt.executeQuery();
    List<JamSubmission> submissions = new ArrayList<>();
    while (rs.next()) {
        submissions.add(this.readSubmission(rs, jam));
    }
    stmt.close();
    return submissions;
}
Also used : JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Aggregations

JamSubmission (net.javadiscord.javabot.systems.jam.model.JamSubmission)12 PreparedStatement (java.sql.PreparedStatement)4 HashMap (java.util.HashMap)4 JamSubmissionRepository (net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository)4 ResultSet (java.sql.ResultSet)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 ArrayList (java.util.ArrayList)2 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)2 JamMessageRepository (net.javadiscord.javabot.systems.jam.dao.JamMessageRepository)2 JamRepository (net.javadiscord.javabot.systems.jam.dao.JamRepository)2 OffsetDateTime (java.time.OffsetDateTime)1 List (java.util.List)1 Map (java.util.Map)1 JDA (net.dv8tion.jda.api.JDA)1 net.dv8tion.jda.api.entities (net.dv8tion.jda.api.entities)1 User (net.dv8tion.jda.api.entities.User)1 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)1 JamConfig (net.javadiscord.javabot.data.config.guild.JamConfig)1 Jam (net.javadiscord.javabot.systems.jam.model.Jam)1 JamTheme (net.javadiscord.javabot.systems.jam.model.JamTheme)1