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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations