use of net.dv8tion.jda.api.events.guild.override.PermissionOverrideDeleteEvent in project JDA by DV8FromTheWorld.
the class ChannelUpdateHandler method applyPermissions.
private void applyPermissions(IPermissionContainerMixin<?> channel, DataArray permOverwrites) {
TLongObjectMap<PermissionOverride> currentOverrides = new TLongObjectHashMap<>(channel.getPermissionOverrideMap());
List<IPermissionHolder> changed = new ArrayList<>(currentOverrides.size());
Guild guild = channel.getGuild();
for (int i = 0; i < permOverwrites.length(); i++) {
DataObject overrideJson = permOverwrites.getObject(i);
long id = overrideJson.getUnsignedLong("id", 0);
if (handlePermissionOverride(currentOverrides.remove(id), overrideJson, id, channel))
addPermissionHolder(changed, guild, id);
}
currentOverrides.forEachValue(override -> {
channel.getPermissionOverrideMap().remove(override.getIdLong());
addPermissionHolder(changed, guild, override.getIdLong());
api.handleEvent(new PermissionOverrideDeleteEvent(api, responseNumber, channel, override));
return true;
});
}
use of net.dv8tion.jda.api.events.guild.override.PermissionOverrideDeleteEvent in project JDA by DV8FromTheWorld.
the class ChannelUpdateHandler method handlePermissionOverride.
// True => override status changed (created/deleted/updated)
// False => nothing changed, ignore
private boolean handlePermissionOverride(PermissionOverride currentOverride, DataObject override, long overrideId, IPermissionContainerMixin<?> channel) {
final long allow = override.getLong("allow");
final long deny = override.getLong("deny");
final int type = override.getInt("type");
final boolean isRole = type == 0;
if (!isRole) {
if (type != 1) {
EntityBuilder.LOG.debug("Ignoring unknown invite of type '{}'. JSON: {}", type, override);
return false;
} else if (!api.isCacheFlagSet(CacheFlag.MEMBER_OVERRIDES) && overrideId != api.getSelfUser().getIdLong()) {
return false;
}
}
if (// Permissions were updated?
currentOverride != null) {
long oldAllow = currentOverride.getAllowedRaw();
long oldDeny = currentOverride.getDeniedRaw();
PermissionOverrideImpl impl = (PermissionOverrideImpl) currentOverride;
if (oldAllow == allow && oldDeny == deny)
return false;
if (overrideId == channel.getGuild().getIdLong() && (allow | deny) == 0L) {
// We delete empty overrides for the @everyone role because that's what the client also does, otherwise our sync checks don't work!
channel.getPermissionOverrideMap().remove(overrideId);
api.handleEvent(new PermissionOverrideDeleteEvent(api, responseNumber, channel, currentOverride));
return true;
}
impl.setAllow(allow);
impl.setDeny(deny);
api.handleEvent(new PermissionOverrideUpdateEvent(api, responseNumber, channel, currentOverride, oldAllow, oldDeny));
} else // New override?
{
// Empty @everyone overrides should be treated as not existing at all
if (overrideId == channel.getGuild().getIdLong() && (allow | deny) == 0L)
return false;
PermissionOverrideImpl impl;
currentOverride = impl = new PermissionOverrideImpl(channel, overrideId, isRole);
impl.setAllow(allow);
impl.setDeny(deny);
channel.getPermissionOverrideMap().put(overrideId, currentOverride);
api.handleEvent(new PermissionOverrideCreateEvent(api, responseNumber, channel, currentOverride));
}
return true;
}
use of net.dv8tion.jda.api.events.guild.override.PermissionOverrideDeleteEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onPermissionOverrideDelete.
public void onPermissionOverrideDelete(PermissionOverrideDeleteEvent event) {
Guild guild = event.getGuild();
GuildChannel channel = event.getChannel();
ChannelType channelType = event.getChannelType();
Member member = event.getMember();
Role role = event.getRole();
boolean roleOverride = event.isRoleOverride();
LoggerEvent loggerEvent = channelType == ChannelType.CATEGORY ? LoggerEvent.CATEGORY_OVERRIDE_DELETE : channelType == ChannelType.STORE ? LoggerEvent.STORE_CHANNEL_OVERRIDE_DELETE : channelType == ChannelType.VOICE ? LoggerEvent.VOICE_CHANNEL_OVERRIDE_DELETE : LoggerEvent.TEXT_CHANNEL_OVERRIDE_DELETE;
LoggerContext loggerContext = new LoggerContext().setUser(roleOverride ? 0L : member.getIdLong()).setRole(roleOverride ? role.getIdLong() : 0L).setChannel(channel);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setColor(this.bot.getConfig().getRed());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
embed.setFooter(new EmbedFooter(String.format("%s ID: %s", roleOverride ? "Role" : "User", event.getPermissionHolder().getIdLong()), null));
// wait for member leave or role delete event if needed
this.delay(() -> {
this.bot.getMongo().aggregateLoggers(this.getPipeline(guild.getIdLong())).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (documents.isEmpty()) {
return;
}
Document data = documents.get(0);
List<Document> loggers = LoggerUtility.getValidLoggers(data.getList("loggers", Document.class), loggerEvent, loggerContext);
if (loggers.isEmpty()) {
return;
}
boolean deleted = (roleOverride ? event.getRole() : event.getMember()) == null;
StringBuilder description = new StringBuilder(String.format("The %s %s has had permission overrides deleted for %s", LoggerUtility.getChannelTypeReadable(channelType), channelType == ChannelType.CATEGORY ? "`" + channel.getName() + "`" : channel.getAsMention(), roleOverride ? (deleted ? "`" + role.getName() + "`" : role.getAsMention()) : "`" + member.getEffectiveName() + "`"));
if (deleted) {
description.append(String.format(" by **%s**", roleOverride ? "role deletion" : "member leave"));
}
if (!deleted && guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogs(guild, ActionType.CHANNEL_OVERRIDE_DELETE).whenComplete((logs, auditException) -> {
User moderator = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).filter(e -> e.getTargetIdLong() == channel.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
description.append(String.format(" by **%s**", moderator.getAsTag()));
}
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
});
}
Aggregations