Search in sources :

Example 1 with FutureAction

use of stream.flarebot.flarebot.scheduler.FutureAction in project FlareBot by FlareBot.

the class FlareBot method loadFutureTasks.

private void loadFutureTasks() {
    if (FlareBot.testBot)
        return;
    final int[] loaded = { 0 };
    CassandraController.runTask(session -> {
        ResultSet set = session.execute("SELECT * FROM flarebot.future_tasks");
        Row row;
        while ((row = set.one()) != null) {
            FutureAction fa = new FutureAction(row.getLong("guild_id"), row.getLong("channel_id"), row.getLong("responsible"), row.getLong("target"), row.getString("content"), new DateTime(row.getTimestamp("expires_at")), new DateTime(row.getTimestamp("created_at")), FutureAction.Action.valueOf(row.getString("action").toUpperCase()));
            try {
                if (new DateTime().isAfter(fa.getExpires()))
                    fa.execute();
                else {
                    fa.queue();
                    loaded[0]++;
                }
            } catch (NullPointerException e) {
                LOGGER.error("Failed to execute/queue future task" + "\nAction: " + fa.getAction() + "\nResponsible: " + fa.getResponsible() + "\nTarget: " + fa.getTarget() + "\nContent: " + fa.getContent(), e);
            }
        }
    });
    LOGGER.info("Loaded " + loaded[0] + " future tasks");
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) FutureAction(stream.flarebot.flarebot.scheduler.FutureAction)

Example 2 with FutureAction

use of stream.flarebot.flarebot.scheduler.FutureAction in project FlareBot by FlareBot.

the class RemindCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    if (args.length < 2) {
        if (args.length == 1) {
            if (args[0].equalsIgnoreCase("list")) {
                Set<FutureAction> futureActions = FlareBot.instance().getFutureActions();
                StringBuilder actionBuilder = new StringBuilder();
                for (FutureAction action : futureActions) {
                    if ((action.getAction().equals(FutureAction.Action.REMINDER) || action.getAction().equals(FutureAction.Action.DM_REMINDER)) && action.getResponsible() == sender.getIdLong()) {
                        LocalDateTime time = LocalDateTime.ofInstant(action.getExpires().toDate().toInstant(), TimeZone.getTimeZone("UTC").toZoneId());
                        actionBuilder.append("`").append(FormatUtils.truncate(100, action.getContent())).append("` at ").append(FormatUtils.formatTime(time)).append(" via ").append(action.getAction().equals(FutureAction.Action.REMINDER) ? GuildUtils.getChannel(String.valueOf(action.getChannelId())).getAsMention() : "Direct Messages").append("\n\n");
                    }
                }
                PagedEmbedBuilder<String> pagedEmbedBuilder = new PagedEmbedBuilder<>(PaginationUtil.splitStringToList(actionBuilder.toString(), PaginationUtil.SplitMethod.CHAR_COUNT, 1000));
                pagedEmbedBuilder.setTitle("Reminders for " + MessageUtils.getTag(sender));
                PaginationUtil.sendEmbedPagedMessage(pagedEmbedBuilder.build(), 0, channel, sender, ButtonGroupConstants.REMIND_LIST);
            } else if (args[0].equalsIgnoreCase("clear")) {
                Set<FutureAction> futureActions = FlareBot.instance().getFutureActions();
                for (FutureAction action : futureActions) {
                    if ((action.getAction().equals(FutureAction.Action.REMINDER) || action.getAction().equals(FutureAction.Action.DM_REMINDER)) && action.getResponsible() == sender.getIdLong())
                        action.delete();
                }
                MessageUtils.sendSuccessMessage("Cleared your reminders successfully", channel, sender);
            } else {
                MessageUtils.sendUsage(this, channel, sender, args);
            }
        } else {
            MessageUtils.sendUsage(this, channel, sender, args);
        }
    } else {
        Period period;
        if ((period = GeneralUtils.getTimeFromInput(args[0], channel)) == null)
            return;
        String reminder;
        FutureAction.Action action;
        if (args[1].equalsIgnoreCase("dm")) {
            reminder = MessageUtils.getMessage(args, 2);
            action = FutureAction.Action.DM_REMINDER;
        } else {
            reminder = MessageUtils.getMessage(args, 1);
            action = FutureAction.Action.REMINDER;
        }
        channel.sendMessage(sender.getAsMention() + " \uD83D\uDC4D I will remind you in " + FormatUtils.formatJodaTime(period).toLowerCase() + " (at " + FormatUtils.formatPrecisely(period) + ") to `" + reminder + "`").queue();
        Scheduler.queueFutureAction(guild.getGuildIdLong(), channel.getIdLong(), sender.getIdLong(), reminder.substring(0, Math.min(reminder.length(), 1000)), period, action);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Set(java.util.Set) PagedEmbedBuilder(stream.flarebot.flarebot.util.pagination.PagedEmbedBuilder) Period(org.joda.time.Period) FutureAction(stream.flarebot.flarebot.scheduler.FutureAction)

Aggregations

LocalDateTime (java.time.LocalDateTime)2 FutureAction (stream.flarebot.flarebot.scheduler.FutureAction)2 ResultSet (com.datastax.driver.core.ResultSet)1 Row (com.datastax.driver.core.Row)1 Set (java.util.Set)1 DateTime (org.joda.time.DateTime)1 Period (org.joda.time.Period)1 PagedEmbedBuilder (stream.flarebot.flarebot.util.pagination.PagedEmbedBuilder)1