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);
}
}
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);
}
}
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();
}
});
}
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);
}
}
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));
}
Aggregations