use of net.javadiscord.javabot.systems.jam.model.Jam in project JavaBot by Java-Discord.
the class ActiveJamSubcommand method handleSlashCommandInteraction.
@Override
public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteractionEvent event) {
if (event.getGuild() == null) {
return Responses.warning(event, "This command can only be used in a guild.");
}
try (Connection con = Bot.dataSource.getConnection()) {
con.setAutoCommit(false);
Jam activeJam = new JamRepository(con).getActiveJam(event.getGuild().getIdLong());
if (activeJam == null) {
return Responses.warning(event, "No Active Jam", "There is currently no active jam in this guild.");
}
try {
var reply = this.handleJamCommand(event, activeJam, con, Bot.config.get(event.getGuild()).getJam());
con.commit();
return reply;
} catch (SQLException e) {
con.rollback();
log.warn("Exception thrown while handling Jam command: {}", e.getMessage());
return Responses.error(event, "An error occurred:\n```" + e.getMessage() + "```");
}
} catch (SQLException e) {
e.printStackTrace();
return Responses.error(event, "An SQL error occurred.");
}
}
use of net.javadiscord.javabot.systems.jam.model.Jam in project JavaBot by Java-Discord.
the class JamInfoSubcommand method fetchJam.
private Jam fetchJam(SlashCommandInteractionEvent event) {
Jam jam;
try (Connection con = Bot.dataSource.getConnection()) {
JamRepository jamRepository = new JamRepository(con);
OptionMapping idOption = event.getOption("id");
if (idOption == null) {
if (event.getGuild() == null) {
throw new RuntimeException("Cannot find active Jam without Guild context.");
}
jam = jamRepository.getActiveJam(event.getGuild().getIdLong());
} else {
jam = jamRepository.getJam(idOption.getAsLong());
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error occurred while fetching the Jam info.", e);
}
return jam;
}
use of net.javadiscord.javabot.systems.jam.model.Jam in project JavaBot by Java-Discord.
the class PlanNewJamSubcommand method createNewJam.
private void createNewJam(InteractionHook hook, long guildId, String name, LocalDate startsAt) {
try (Connection con = Bot.dataSource.getConnection()) {
JamRepository jamRepository = new JamRepository(con);
Jam activeJam = jamRepository.getActiveJam(guildId);
if (activeJam != null) {
Responses.warning(hook, "There is already an active Jam (id = `" + activeJam.getId() + "`). Complete that Jam before planning a new one.").queue();
return;
}
Jam jam = new Jam();
jam.setGuildId(guildId);
jam.setName(name);
jam.setStartedBy(hook.getInteraction().getUser().getIdLong());
jam.setStartsAt(startsAt);
jam.setCompleted(false);
jamRepository.saveNewJam(jam);
Responses.success(hook, "Jam Created", "Jam has been created! *Jam ID = `" + jam.getId() + "`*. Use `/jam info` for more info.").queue();
} catch (SQLException e) {
Responses.error(hook, "Error occurred while creating the Jam: " + e.getMessage()).queue();
}
}
use of net.javadiscord.javabot.systems.jam.model.Jam in project JavaBot by Java-Discord.
the class JamInfoSubcommand method handleSlashCommandInteraction.
@Override
public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteractionEvent event) {
Jam jam = this.fetchJam(event);
if (jam == null) {
return Responses.warning(event, "No Jam was found.");
}
event.getJDA().retrieveUserById(jam.getStartedBy()).queue(user -> {
EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Jam Information").setColor(Bot.config.get(event.getGuild()).getJam().getJamEmbedColor()).addField("Id", Long.toString(jam.getId()), true).addField("Name", jam.getFullName(), true).addField("Created at", jam.getCreatedAt().format(DateTimeFormatter.ofPattern("d MMMM yyyy 'at' kk:mm:ss 'UTC'")), true).addField("Started by", user.getAsTag(), true).addField("Starts at", jam.getStartsAt().format(DateTimeFormatter.ofPattern("d MMMM yyyy")), true).addField("Current phase", jam.getCurrentPhase(), true);
if (jam.getEndsAt() != null) {
embedBuilder.addField("Ends at", jam.getEndsAt().format(DateTimeFormatter.ofPattern("d MMMM yyyy")), true);
}
event.getHook().sendMessageEmbeds(embedBuilder.build()).queue();
});
return event.deferReply();
}
use of net.javadiscord.javabot.systems.jam.model.Jam 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;
}
Aggregations