Search in sources :

Example 1 with Jam

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.");
    }
}
Also used : SQLException(java.sql.SQLException) Jam(net.javadiscord.javabot.systems.jam.model.Jam) Connection(java.sql.Connection) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository)

Example 2 with Jam

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;
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) SQLException(java.sql.SQLException) Jam(net.javadiscord.javabot.systems.jam.model.Jam) Connection(java.sql.Connection) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository)

Example 3 with 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();
    }
}
Also used : SQLException(java.sql.SQLException) Jam(net.javadiscord.javabot.systems.jam.model.Jam) Connection(java.sql.Connection) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository)

Example 4 with Jam

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();
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Jam(net.javadiscord.javabot.systems.jam.model.Jam)

Example 5 with Jam

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

Aggregations

Jam (net.javadiscord.javabot.systems.jam.model.Jam)6 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 JamRepository (net.javadiscord.javabot.systems.jam.dao.JamRepository)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 OffsetDateTime (java.time.OffsetDateTime)1 HashMap (java.util.HashMap)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 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)1 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)1 JamConfig (net.javadiscord.javabot.data.config.guild.JamConfig)1 JamSubmission (net.javadiscord.javabot.systems.jam.model.JamSubmission)1 JamTheme (net.javadiscord.javabot.systems.jam.model.JamTheme)1 ColorUtils (net.javadiscord.javabot.util.ColorUtils)1