Search in sources :

Example 1 with PermissionException

use of net.dv8tion.jda.api.exceptions.PermissionException in project Saber-Bot by notem.

the class EventListener method onMessageReactionAdd.

@Override
@SuppressWarnings("unchecked")
public void onMessageReactionAdd(MessageReactionAddEvent event) {
    // stop processing if the event is not from a guild text channel
    if (!event.isFromType(ChannelType.TEXT))
        return;
    // don't process reactions added on non RSVP channels
    if (!Main.getScheduleManager().isRSVPEnabled(event.getChannel().getId()))
        return;
    // don't process reactions added by the bot
    if (event.getUser().getId().equals(event.getJDA().getSelfUser().getId()))
        return;
    if (reactionLimiter.check(event.getUser().getId()))
        return;
    // add the user to the appropriate rsvp list and remove the emoji
    try {
        Document doc = Main.getDBDriver().getEventCollection().find(eq("messageId", event.getMessageId())).first();
        if (doc != null) {
            ScheduleEntry se = new ScheduleEntry(doc);
            boolean removeReaction = se.handleRSVPReaction(event);
            if (removeReaction) {
                // attempt to remove the reaction
                Consumer<Throwable> errorProcessor = e -> {
                    if (!(e instanceof PermissionException)) {
                        Logging.exception(this.getClass(), e);
                    }
                };
                event.getReaction().removeReaction(event.getUser()).queue(null, errorProcessor);
            }
        }
    } catch (PermissionException ignored) {
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
    }
}
Also used : Document(org.bson.Document) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) JDA(net.dv8tion.jda.api.JDA) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) GuildJoinEvent(net.dv8tion.jda.api.events.guild.GuildJoinEvent) Collection(java.util.Collection) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) Set(java.util.Set) MessageReactionAddEvent(net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent) ws.nmathe.saber.utils(ws.nmathe.saber.utils) ReadyEvent(net.dv8tion.jda.api.events.ReadyEvent) StringUtils(org.apache.commons.lang3.StringUtils) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) GuildLeaveEvent(net.dv8tion.jda.api.events.guild.GuildLeaveEvent) Consumer(java.util.function.Consumer) TextChannelDeleteEvent(net.dv8tion.jda.api.events.channel.text.TextChannelDeleteEvent) List(java.util.List) GuildMemberLeaveEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberLeaveEvent) MessageDeleteEvent(net.dv8tion.jda.api.events.message.MessageDeleteEvent) GuildSettingsManager(ws.nmathe.saber.core.settings.GuildSettingsManager) Filters.eq(com.mongodb.client.model.Filters.eq) Main(ws.nmathe.saber.Main) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) Document(org.bson.Document) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException)

Example 2 with PermissionException

use of net.dv8tion.jda.api.exceptions.PermissionException in project Saber-Bot by notem.

the class ScheduleManager method sortSchedule.

/**
 * Reorders the schedule so that entries are displayed by start datetime ascending order in
 * the discord schedule channel
 * @param cId schedule ID
 * @param reverseOrder (boolean) whether or not to reverse the sort order
 */
public void sortSchedule(String cId, boolean reverseOrder) {
    if (this.getScheduleSize(cId) > MAX_SIZE_TO_SYNC)
        return;
    if (this.isLocked(cId))
        return;
    // lock the channel
    this.lock(cId);
    // always unlock the schedule at finish regardless of success or failure
    try {
        // identify which shard is responsible for the schedule
        Document doc = Main.getDBDriver().getScheduleCollection().find(eq("_id", cId)).projection(fields(include("guildId"))).first();
        JDA jda = Main.getShardManager().getJDA(doc.getString("guildId"));
        // find the message channel and send the 'is typing' while processing
        MessageChannel chan = jda.getTextChannelById(cId);
        chan.sendTyping().queue();
        int sortOrder = 1;
        if (reverseOrder)
            sortOrder = -1;
        LinkedList<ScheduleEntry> unsortedEntries = new LinkedList<>();
        Main.getDBDriver().getEventCollection().find(eq("channelId", cId)).sort(new Document("start", sortOrder)).forEach((Consumer<? super Document>) document -> unsortedEntries.add(new ScheduleEntry(document)));
        // selection sort the entries by timestamp
        while (!unsortedEntries.isEmpty()) {
            // continue to send 'is typing'
            chan.sendTyping().queue();
            ScheduleEntry top = unsortedEntries.pop();
            ScheduleEntry min = top;
            for (ScheduleEntry cur : unsortedEntries) {
                Message minMsg = min.getMessageObject();
                Message topMsg = cur.getMessageObject();
                if (minMsg != null && topMsg != null) {
                    OffsetDateTime a = minMsg.getTimeCreated();
                    OffsetDateTime b = topMsg.getTimeCreated();
                    if (a.isAfter(b)) {
                        min = cur;
                    }
                }
            }
            // swap messages and update db
            if (!(min == top)) {
                Message tmp = top.getMessageObject();
                top.setMessageObject(min.getMessageObject());
                Main.getDBDriver().getEventCollection().updateOne(eq("_id", top.getId()), new Document("$set", new Document("messageId", min.getMessageObject().getId())));
                min.setMessageObject(tmp);
                Main.getDBDriver().getEventCollection().updateOne(eq("_id", min.getId()), new Document("$set", new Document("messageId", tmp.getId())));
            }
            // reload display
            top.reloadDisplay();
        }
    } catch (PermissionException e) {
        String m = e.getMessage() + ": Channel ID " + cId;
        Logging.warn(this.getClass(), m);
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
    } finally {
        // always unlock
        this.unlock(cId);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) Document(org.bson.Document) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) Projections.fields(com.mongodb.client.model.Projections.fields) Permission(net.dv8tion.jda.api.Permission) Logging(ws.nmathe.saber.utils.Logging) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Updates.set(com.mongodb.client.model.Updates.set) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Projections.include(com.mongodb.client.model.Projections.include) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) java.time(java.time) Guild(net.dv8tion.jda.api.entities.Guild) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Filters.eq(com.mongodb.client.model.Filters.eq) Main(ws.nmathe.saber.Main) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) Message(net.dv8tion.jda.api.entities.Message) JDA(net.dv8tion.jda.api.JDA) Document(org.bson.Document) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel)

Example 3 with PermissionException

use of net.dv8tion.jda.api.exceptions.PermissionException in project MantaroBot by Mantaro.

the class MessageCmds method prune.

private void prune(Context ctx, List<Message> messageHistory) {
    messageHistory = messageHistory.stream().filter(message -> !message.getTimeCreated().isBefore(OffsetDateTime.now().minusWeeks(2))).collect(Collectors.toList());
    if (messageHistory.isEmpty()) {
        ctx.sendLocalized("commands.prune.messages_too_old", EmoteReference.ERROR);
        return;
    }
    final var size = messageHistory.size();
    if (messageHistory.size() < 3) {
        ctx.sendLocalized("commands.prune.too_few_messages", EmoteReference.ERROR);
        return;
    }
    ctx.getChannel().deleteMessages(messageHistory).queue(success -> {
        ctx.sendLocalized("commands.prune.success", EmoteReference.PENCIL, size);
        var db = ctx.getDBGuild();
        db.getData().setCases(db.getData().getCases() + 1);
        db.saveAsync();
        ModLog.log(ctx.getMember(), null, "Pruned Messages", ctx.getChannel().getName(), ModLog.ModAction.PRUNE, db.getData().getCases(), size);
    }, error -> {
        if (error instanceof PermissionException) {
            PermissionException pe = (PermissionException) error;
            ctx.sendLocalized("commands.prune.lack_perms", EmoteReference.ERROR, pe.getPermission());
        } else {
            ctx.sendLocalized("commands.prune.error_deleting", EmoteReference.ERROR, error.getClass().getSimpleName(), error.getMessage());
            error.printStackTrace();
        }
    });
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException)

Example 4 with PermissionException

use of net.dv8tion.jda.api.exceptions.PermissionException in project Saber-Bot by notem.

the class EntryProcessor method run.

@SuppressWarnings("unchecked")
public void run() {
    try {
        /*
             * Process events
             */
        if (type == EntryManager.type.PROCESS) {
            Logging.info(this.getClass(), "Processing entries. . .");
            Bson query;
            /*
                 * process any event-specific announcements
                 */
            query = lte("announcements", new Date());
            processEvents(ActionType.SPECIAL, query);
            /*
                 * process events to end
                 */
            query = and(eq("hasStarted", true), lte("end", new Date()));
            processEvents(ActionType.END, query);
            /*
                 * process events to start
                 */
            query = and(eq("hasStarted", false), lte("start", new Date()));
            processEvents(ActionType.START, query);
            /*
                 * process events with reminders
                 */
            query = and(and(eq("hasStarted", false), lte("reminders", new Date())), gte("start", new Date()));
            query = or(query, and(and(eq("hasStarted", true), lte("end_reminders", new Date())), gte("end", new Date())));
            processEvents(ActionType.REMIND, query);
            Logging.info(this.getClass(), "Currently processing " + processing.size() + " events.");
            // exit the bot if any event takes more than a few minutes to process
            int timeThreshold = 3;
            int countThreshold = 30;
            int count = 0;
            for (Date value : timestamps.values()) {
                boolean flagged = value.before(Date.from(Instant.now().minus(timeThreshold, ChronoUnit.MINUTES)));
                if (flagged)
                    count += 1;
            }
            if (count > countThreshold) {
                String txt = "[EXIT] There are " + count + " events that have been in processing for over " + timeThreshold + ".";
                Logging.warn(this.getClass(), txt);
                System.exit(-100);
            }
        } else /*
             * Updates the 'starts in x minutes' timer on events
             */
        {
            // dummy document query will filter all events
            // should an invalid level ever be passed in, all entries will be reloaded!
            Bson query = new Document();
            Logging.info(this.getClass(), "Processing entries: updating timers. . .");
            if (type == EntryManager.type.UPDATE1) {
                // adjust timers for entries starting/ending within the next hour
                query = or(and(eq("hasStarted", false), and(lte("start", Date.from(ZonedDateTime.now().plusHours(1).toInstant())), gte("start", Date.from(ZonedDateTime.now().plusMinutes(4).toInstant())))), and(eq("hasStarted", true), and(lte("end", Date.from(ZonedDateTime.now().plusHours(1).toInstant())), gte("end", Date.from(ZonedDateTime.now().plusMinutes(4).toInstant())))));
            }
            if (type == EntryManager.type.UPDATE2) {
                // purge expiring events
                query = lte("expire", Date.from(ZonedDateTime.now().plusDays(1).toInstant()));
                // delete message objects
                Main.getDBDriver().getEventCollection().find(query).forEach((Consumer<? super Document>) document -> {
                    MessageUtilities.deleteMsg((new ScheduleEntry(document)).getMessageObject(), null);
                });
                // bulk delete entries from the database
                Main.getDBDriver().getEventCollection().deleteMany(query);
                // adjust timers
                query = or(and(eq("hasStarted", false), and(lte("start", Date.from(ZonedDateTime.now().plusDays(1).toInstant())), gte("start", Date.from(ZonedDateTime.now().plusHours(1).toInstant())))), and(eq("hasStarted", true), and(lte("end", Date.from(ZonedDateTime.now().plusDays(1).toInstant())), gte("end", Date.from(ZonedDateTime.now().plusHours(1).toInstant())))));
            }
            if (type == EntryManager.type.UPDATE3) {
                // adjust timers for entries that aren't starting/ending within the next day
                query = or(and(eq("hasStarted", false), gte("start", Date.from(ZonedDateTime.now().plusDays(1).toInstant()))), and(eq("hasStarted", true), gte("end", Date.from(ZonedDateTime.now().plusDays(1).toInstant()))));
            }
            // reload entries based on the appropriate query
            Main.getDBDriver().getEventCollection().find(query).forEach((Consumer<? super Document>) document -> {
                String guildId = document.getString("guildId");
                JDA jda = Main.getShardManager().getJDA(guildId);
                if (jda == null)
                    return;
                if (!jda.getStatus().equals(JDA.Status.CONNECTED))
                    return;
                timerExecutor.execute(() -> {
                    try {
                        (new ScheduleEntry(document)).reloadDisplay();
                    } catch (PermissionException ignored) {
                    } catch (Exception e) {
                        Logging.warn(this.getClass(), "Error occurred while updating event timer.");
                        Logging.exception(this.getClass(), e);
                    }
                });
            });
            Logging.info(this.getClass(), "Finished updating timers. . .");
        }
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
    }
}
Also used : Document(org.bson.Document) JDA(net.dv8tion.jda.api.JDA) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) Date(java.util.Date) java.util.concurrent(java.util.concurrent) Logging(ws.nmathe.saber.utils.Logging) ZonedDateTime(java.time.ZonedDateTime) Set(java.util.Set) Instant(java.time.Instant) Bson(org.bson.conversions.Bson) Consumer(java.util.function.Consumer) Filters(com.mongodb.client.model.Filters) ChronoUnit(java.time.temporal.ChronoUnit) Map(java.util.Map) MessageUtilities(ws.nmathe.saber.utils.MessageUtilities) Main(ws.nmathe.saber.Main) Collections(java.util.Collections) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) JDA(net.dv8tion.jda.api.JDA) Document(org.bson.Document) Date(java.util.Date) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) Bson(org.bson.conversions.Bson)

Example 5 with PermissionException

use of net.dv8tion.jda.api.exceptions.PermissionException in project Saber-Bot by notem.

the class ScheduleManager method deleteSchedule.

/**
 * Removes a schedule and attempts to delete the schedule's channel
 * @param cId (String) ID of channel / schedule (synonymous)
 */
public void deleteSchedule(String cId) {
    // identify which shard is responsible for the schedule
    Document doc = Main.getDBDriver().getScheduleCollection().find(eq("_id", cId)).projection(fields(include("guildId"))).first();
    JDA jda = Main.getShardManager().getJDA(doc.getString("guildId"));
    try {
        jda.getTextChannelById(cId).delete().complete();
    } catch (PermissionException e) {
        String m = e.getMessage() + ": " + e.getPermission();
        Logging.warn(this.getClass(), m);
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
    }
    Main.getDBDriver().getEventCollection().deleteMany(eq("channelId", cId));
    Main.getDBDriver().getScheduleCollection().deleteOne(eq("_id", cId));
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) JDA(net.dv8tion.jda.api.JDA) Document(org.bson.Document) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException)

Aggregations

PermissionException (net.dv8tion.jda.api.exceptions.PermissionException)7 JDA (net.dv8tion.jda.api.JDA)6 Document (org.bson.Document)4 Consumer (java.util.function.Consumer)3 Permission (net.dv8tion.jda.api.Permission)3 Main (ws.nmathe.saber.Main)3 Filters.eq (com.mongodb.client.model.Filters.eq)2 ChronoUnit (java.time.temporal.ChronoUnit)2 Set (java.util.Set)2 Guild (net.dv8tion.jda.api.entities.Guild)2 Logging (ws.nmathe.saber.utils.Logging)2 Filters (com.mongodb.client.model.Filters)1 Projections.fields (com.mongodb.client.model.Projections.fields)1 Projections.include (com.mongodb.client.model.Projections.include)1 Updates.set (com.mongodb.client.model.Updates.set)1 java.time (java.time)1 Instant (java.time.Instant)1 ZonedDateTime (java.time.ZonedDateTime)1 java.util (java.util)1 Collection (java.util.Collection)1