Search in sources :

Example 1 with JamRepository

use of net.javadiscord.javabot.systems.jam.dao.JamRepository 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 JamRepository

use of net.javadiscord.javabot.systems.jam.dao.JamRepository in project JavaBot by Java-Discord.

the class ToSubmissionTransition method transition.

@Override
public void transition(Jam jam, SlashCommandInteractionEvent event, JamChannelManager channelManager, Connection con) throws SQLException {
    JamMessageRepository messageRepository = new JamMessageRepository(con);
    List<JamTheme> themes = new JamThemeRepository(con).getThemes(jam);
    long themeVotingMessageId = messageRepository.getMessageId(jam, "theme_voting");
    var votes = channelManager.getThemeVotes(themeVotingMessageId, themes);
    var voteCounts = this.recordAndCountVotes(jam, votes, con);
    JamTheme winningTheme = this.determineWinner(voteCounts);
    if (winningTheme == null) {
        throw new IllegalStateException("No winning jam theme could be found.");
    }
    this.updateThemesAcceptedState(themes, winningTheme, con);
    channelManager.sendChosenThemeMessage(voteCounts, winningTheme);
    new JamRepository(con).updateJamPhase(jam, JamPhase.SUBMISSION);
    messageRepository.removeMessageId(jam, "theme_voting");
}
Also used : JamMessageRepository(net.javadiscord.javabot.systems.jam.dao.JamMessageRepository) JamTheme(net.javadiscord.javabot.systems.jam.model.JamTheme) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository) JamThemeRepository(net.javadiscord.javabot.systems.jam.dao.JamThemeRepository)

Example 3 with JamRepository

use of net.javadiscord.javabot.systems.jam.dao.JamRepository in project JavaBot by Java-Discord.

the class ToThemeVotingTransition method transition.

@Override
public void transition(Jam jam, SlashCommandInteractionEvent event, JamChannelManager channelManager, Connection con) throws SQLException {
    List<JamTheme> themes = new JamThemeRepository(con).getThemes(jam);
    if (themes.isEmpty()) {
        throw new IllegalStateException("Cannot start theme voting until at least one theme is available.");
    }
    long votingMessageId = channelManager.sendThemeVotingMessages(jam, themes);
    new JamMessageRepository(con).saveMessageId(jam, votingMessageId, "theme_voting");
    new JamRepository(con).updateJamPhase(jam, JamPhase.THEME_VOTING);
}
Also used : JamMessageRepository(net.javadiscord.javabot.systems.jam.dao.JamMessageRepository) JamTheme(net.javadiscord.javabot.systems.jam.model.JamTheme) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository) JamThemeRepository(net.javadiscord.javabot.systems.jam.dao.JamThemeRepository)

Example 4 with JamRepository

use of net.javadiscord.javabot.systems.jam.dao.JamRepository 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 5 with JamRepository

use of net.javadiscord.javabot.systems.jam.dao.JamRepository in project JavaBot by Java-Discord.

the class ToCompletionTransition method transition.

@Override
public void transition(Jam jam, SlashCommandInteractionEvent event, JamChannelManager channelManager, Connection con) throws SQLException {
    JamMessageRepository messageRepository = new JamMessageRepository(con);
    List<JamSubmission> submissions = new JamSubmissionRepository(con).getSubmissions(jam);
    var votes = channelManager.getSubmissionVotes(this.getSubmissionMessageIds(submissions, messageRepository));
    var voteCounts = this.recordAndCountVotes(votes, con, messageRepository);
    var winningSubmissions = this.determineWinners(voteCounts);
    if (winningSubmissions.isEmpty()) {
        channelManager.sendNoWinnersMessage();
    } else if (winningSubmissions.size() == 1) {
        channelManager.sendSingleWinnerMessage(winningSubmissions.get(0), voteCounts, event);
    } else {
        channelManager.sendMultipleWinnersMessage(winningSubmissions, voteCounts, event);
    }
    new JamRepository(con).completeJam(jam);
}
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)

Aggregations

JamRepository (net.javadiscord.javabot.systems.jam.dao.JamRepository)8 JamMessageRepository (net.javadiscord.javabot.systems.jam.dao.JamMessageRepository)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Jam (net.javadiscord.javabot.systems.jam.model.Jam)3 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)2 JamSubmissionRepository (net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository)2 JamThemeRepository (net.javadiscord.javabot.systems.jam.dao.JamThemeRepository)2 JamSubmission (net.javadiscord.javabot.systems.jam.model.JamSubmission)2 JamTheme (net.javadiscord.javabot.systems.jam.model.JamTheme)2 NewsChannel (net.dv8tion.jda.api.entities.NewsChannel)1