Search in sources :

Example 6 with DatabaseAdapter

use of ml.duncte123.skybot.adapters.DatabaseAdapter in project SkyBot by DuncteBot.

the class SQLiteTimers method checkUnbansAndUnmutes.

private static void checkUnbansAndUnmutes(Variables variables) {
    variables.getDatabaseAdapter().getExpiredBansAndMutes((bans, mutes) -> {
        final DatabaseAdapter adapter = variables.getDatabaseAdapter();
        handleUnban(bans, adapter, variables);
        handleUnmute(mutes, adapter, variables);
        return null;
    });
}
Also used : DatabaseAdapter(ml.duncte123.skybot.adapters.DatabaseAdapter)

Example 7 with DatabaseAdapter

use of ml.duncte123.skybot.adapters.DatabaseAdapter in project SkyBot by duncte123.

the class AutoBanBypassCommand method execute.

@Override
public void execute(@NotNull CommandContext ctx) {
    final long checkId;
    try {
        checkId = MiscUtil.parseSnowflake(ctx.getArgs().get(0));
    } catch (NumberFormatException ignored) {
        sendMsg(ctx, "Your input (`" + ctx.getArgs().get(0) + "`) is not a valid user id.");
        return;
    }
    final DatabaseAdapter database = ctx.getDatabaseAdapter();
    final long guildId = ctx.getGuild().getIdLong();
    database.getBanBypass(guildId, checkId, (byPass) -> {
        if (byPass == null) {
            database.createBanBypass(guildId, checkId);
            sendMsg(ctx, "Single use bypass created, please note that this bypass will expire after a week if unused." + "\nPlease keep in mind that this has not unbanned any user, meaning that you will have to unban the user yourself if they are banned");
            return null;
        }
        sendMsg(ctx, "A bypass already exists for this user");
        return null;
    });
}
Also used : DatabaseAdapter(ml.duncte123.skybot.adapters.DatabaseAdapter)

Example 8 with DatabaseAdapter

use of ml.duncte123.skybot.adapters.DatabaseAdapter in project SkyBot by duncte123.

the class AirUtils method handleExpiredReminders.

public static void handleExpiredReminders(List<Reminder> reminders, DatabaseAdapter adapter) {
    // Get the shardManager and a list of ints to purge the ids for
    final ShardManager shardManager = SkyBot.getInstance().getShardManager();
    final List<Integer> toPurge = new ArrayList<>();
    for (final Reminder reminder : reminders) {
        // The reminder message template
        final String message = String.format("%s you asked me to remind you about \"%s\"", TimeFormat.RELATIVE.format(reminder.getCreate_date()), reminder.getReminder().trim());
        // If we have a channel send the message to that
        if (reminder.getIn_channel()) {
            final long channelId = reminder.getChannel_id();
            final TextChannel channel = shardManager.getTextChannelById(channelId);
            // skipping the continue statement makes sure that we roll into the dm part of this
            if (channel != null) {
                // Add the reminder to the list of the reminders to purge
                toPurge.add(reminder.getId());
                sendMsg(new MessageConfig.Builder().setChannel(channel).setMessage(String.format("<@%s>, %s", reminder.getUser_id(), message)).replyTo(reminder.getMessage_id()).build());
                // go to the next one and don't run the user code if a channel was found
                continue;
            }
        }
        try {
            Objects.requireNonNull(shardManager.getShardById(0)).openPrivateChannelById(reminder.getUser_id()).flatMap((c) -> c.sendMessage(message + "\n" + reminder.getJumpUrl())).complete();
            toPurge.add(reminder.getId());
        } catch (ErrorResponseException errorResponseEx) {
            final ErrorResponse errorResponse = errorResponseEx.getErrorResponse();
            if (// The account probably got deleted or something
            errorResponse == ErrorResponse.UNKNOWN_USER || // we cannot dm this user (has dms blocked?)
            errorResponse == ErrorResponse.CANNOT_SEND_TO_USER) {
                toPurge.add(reminder.getId());
            }
        } catch (Exception e) {
            Sentry.captureException(e);
        }
    }
    // get a date that is 2 days in the future
    final OffsetDateTime plusTwoDays = OffsetDateTime.now(ZoneOffset.UTC).plus(2L, ChronoUnit.DAYS);
    // Remove any reminders that have not been removed after 2 days
    final List<Integer> extraRemoval = reminders.stream().filter((reminder) -> reminder.getReminder_date().isAfter(plusTwoDays)).map(Reminder::getId).collect(Collectors.toList());
    toPurge.addAll(extraRemoval);
    if (!toPurge.isEmpty()) {
        adapter.purgeRemindersSync(toPurge);
    }
}
Also used : Arrays(java.util.Arrays) JDA(net.dv8tion.jda.api.JDA) StringUtils(me.duncte123.botcommons.StringUtils) ParsedDuration(me.duncte123.durationparser.ParsedDuration) Permission(net.dv8tion.jda.api.Permission) JSONRequestBody(me.duncte123.botcommons.web.requests.JSONRequestBody) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FinderUtil(com.jagrosh.jdautilities.commons.utils.FinderUtil) User(net.dv8tion.jda.api.entities.User) TemporalAccessor(java.time.temporal.TemporalAccessor) ArrayList(java.util.ArrayList) WebParserUtils.toJSONObject(me.duncte123.botcommons.web.WebParserUtils.toJSONObject) Guild(net.dv8tion.jda.api.entities.Guild) JDAImpl(net.dv8tion.jda.internal.JDAImpl) LavalinkManager(fredboat.audio.player.LavalinkManager) ZoneOffset(java.time.ZoneOffset) Sentry(io.sentry.Sentry) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) MessageUtils.sendMsg(me.duncte123.botcommons.messaging.MessageUtils.sendMsg) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TimeFormat(net.dv8tion.jda.api.utils.TimeFormat) WebParserUtils(me.duncte123.botcommons.web.WebParserUtils) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) ChronoUnit(java.time.temporal.ChronoUnit) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) SkyBot(ml.duncte123.skybot.SkyBot) DatabaseAdapter(ml.duncte123.skybot.adapters.DatabaseAdapter) TLongObjectMap(gnu.trove.map.TLongObjectMap) FakeMember(ml.duncte123.skybot.entities.jda.FakeMember) WebUtils(me.duncte123.botcommons.web.WebUtils) PendingRequest(com.github.natanbc.reliqua.request.PendingRequest) GuildMusicManager(ml.duncte123.skybot.audio.GuildMusicManager) Reminder(ml.duncte123.skybot.objects.api.Reminder) FakePendingRequest(ml.duncte123.skybot.objects.FakePendingRequest) Reminder(ml.duncte123.skybot.objects.api.Reminder) ArrayList(java.util.ArrayList) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DateTimeParseException(java.time.format.DateTimeParseException) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) OffsetDateTime(java.time.OffsetDateTime) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException)

Aggregations

DatabaseAdapter (ml.duncte123.skybot.adapters.DatabaseAdapter)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 PendingRequest (com.github.natanbc.reliqua.request.PendingRequest)2 FinderUtil (com.jagrosh.jdautilities.commons.utils.FinderUtil)2 LavalinkManager (fredboat.audio.player.LavalinkManager)2 TLongLongMap (gnu.trove.map.TLongLongMap)2 TLongObjectMap (gnu.trove.map.TLongObjectMap)2 TLongLongHashMap (gnu.trove.map.hash.TLongLongHashMap)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 Sentry (io.sentry.Sentry)2 OffsetDateTime (java.time.OffsetDateTime)2 ZoneOffset (java.time.ZoneOffset)2 DateTimeParseException (java.time.format.DateTimeParseException)2 ChronoUnit (java.time.temporal.ChronoUnit)2 TemporalAccessor (java.time.temporal.TemporalAccessor)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2