Search in sources :

Example 1 with QuestionQueueRepository

use of net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository in project JavaBot by Java-Discord.

the class RemoveQuestionSubcommand method handleCommand.

@Override
protected ReplyCallbackAction handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException {
    OptionMapping idOption = event.getOption("id");
    if (idOption == null) {
        return Responses.warning(event, "Missing required arguments.");
    }
    long id = idOption.getAsLong();
    boolean removed = new QuestionQueueRepository(con).removeQuestion(guildId, id);
    if (removed) {
        return Responses.success(event, "Question Removed", "The question with id `" + id + "` has been removed.");
    } else {
        return Responses.warning(event, "Could not remove question with id `" + id + "`. Are you sure it exists?");
    }
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) QuestionQueueRepository(net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository)

Example 2 with QuestionQueueRepository

use of net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository in project JavaBot by Java-Discord.

the class AddQuestionSubcommand method handleCommand.

@Override
protected ReplyCallbackAction handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException {
    QOTWQuestion question = new QOTWQuestion();
    question.setGuildId(guildId);
    question.setCreatedBy(event.getUser().getIdLong());
    question.setPriority(0);
    OptionMapping textOption = event.getOption("question");
    if (textOption == null) {
        return Responses.warning(event, "Missing required arguments.");
    }
    String text = textOption.getAsString();
    if (text.isBlank() || text.length() > 1024) {
        return Responses.warning(event, "Invalid question text. Must not be blank, and must be less than 1024 characters.");
    }
    question.setText(text);
    OptionMapping priorityOption = event.getOption("priority");
    if (priorityOption != null) {
        question.setPriority((int) priorityOption.getAsLong());
    }
    new QuestionQueueRepository(con).save(question);
    return Responses.success(event, "Question Added", "Your question has been added to the queue. Its id is `" + question.getId() + "`.");
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) QOTWQuestion(net.javadiscord.javabot.systems.qotw.model.QOTWQuestion) QuestionQueueRepository(net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository)

Example 3 with QuestionQueueRepository

use of net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository in project JavaBot by Java-Discord.

the class QOTWReminderJob method execute.

@Override
protected void execute(JobExecutionContext context, JDA jda) throws JobExecutionException {
    for (var guild : jda.getGuilds()) {
        try (var c = Bot.dataSource.getConnection()) {
            var repo = new QuestionQueueRepository(c);
            var q = repo.getNextQuestion(guild.getIdLong());
            if (q.isEmpty()) {
                GuildUtils.getLogChannel(guild).sendMessageFormat("Warning! @here There's no Question of the Week in the queue. Please add one before it's time to post!").queue();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            GuildUtils.getLogChannel(guild).sendMessageFormat("Warning! @here Could not check to see if there's a question in the QOTW queue:\n```\n%s\n```\n", e.getMessage()).queue();
            throw new JobExecutionException(e);
        }
    }
}
Also used : JobExecutionException(org.quartz.JobExecutionException) SQLException(java.sql.SQLException) QuestionQueueRepository(net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository)

Example 4 with QuestionQueueRepository

use of net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository in project JavaBot by Java-Discord.

the class ListQuestionsSubcommand method handleCommand.

@Override
protected ReplyCallbackAction handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException {
    var repository = new QuestionQueueRepository(con);
    OptionMapping pageOption = event.getOption("page");
    int page = 0;
    if (pageOption != null) {
        int userPage = (int) pageOption.getAsLong();
        if (userPage < 0) {
            return Responses.warning(event, "Invalid page.");
        }
        page = userPage;
    }
    var questions = repository.getQuestions(guildId, page, 10);
    EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("QOTW Questions Queue");
    if (questions.isEmpty()) {
        embedBuilder.setDescription("There are no questions in the queue.");
        return event.replyEmbeds(embedBuilder.build());
    }
    Bot.asyncPool.submit(() -> {
        for (var question : questions) {
            embedBuilder.addField(String.valueOf(question.getId()), String.format("> %s\nPriority: **%d**\nCreated by: %s\nCreated at: %s", question.getText(), question.getPriority(), event.getJDA().retrieveUserById(question.getCreatedBy()).complete().getAsTag(), question.getCreatedAt().format(DateTimeFormatter.ofPattern("d MMMM yyyy"))), false);
        }
        event.getHook().sendMessageEmbeds(embedBuilder.build()).queue();
    });
    return event.deferReply();
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) QuestionQueueRepository(net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository)

Example 5 with QuestionQueueRepository

use of net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository in project JavaBot by Java-Discord.

the class QOTWJob method execute.

@Override
protected void execute(JobExecutionContext context, JDA jda) throws JobExecutionException {
    for (var guild : jda.getGuilds()) {
        if (Bot.config.get(guild).getModeration().getLogChannel() == null)
            continue;
        try (var c = Bot.dataSource.getConnection()) {
            var repo = new QuestionQueueRepository(c);
            var nextQuestion = repo.getNextQuestion(guild.getIdLong());
            if (nextQuestion.isEmpty()) {
                GuildUtils.getLogChannel(guild).sendMessage("Warning! @here No available next question for QOTW!").queue();
            } else {
                var question = nextQuestion.get();
                var config = Bot.config.get(guild).getQotw();
                if (question.getQuestionNumber() == null) {
                    question.setQuestionNumber(repo.getNextQuestionNumber());
                }
                var questionChannel = config.getQuestionChannel();
                if (questionChannel == null)
                    continue;
                questionChannel.sendMessage(config.getQOTWRole().getAsMention()).setEmbeds(buildEmbed(question)).setActionRows(ActionRow.of(Button.success("qotw-submission:" + question.getQuestionNumber(), "Submit your Answer"))).queue(msg -> questionChannel.crosspostMessageById(msg.getIdLong()).queue());
                repo.markUsed(question);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            GuildUtils.getLogChannel(guild).sendMessageFormat("Warning! @here Could not send next QOTW question:\n```\n%s\n```\n", e.getMessage()).queue();
            throw new JobExecutionException(e);
        }
    }
}
Also used : JobExecutionException(org.quartz.JobExecutionException) SQLException(java.sql.SQLException) QuestionQueueRepository(net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository)

Aggregations

QuestionQueueRepository (net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository)5 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)3 SQLException (java.sql.SQLException)2 JobExecutionException (org.quartz.JobExecutionException)2 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 QOTWQuestion (net.javadiscord.javabot.systems.qotw.model.QOTWQuestion)1