use of net.dv8tion.jda.api.events.guild.override.PermissionOverrideCreateEvent 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.PermissionOverrideCreateEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onPermissionOverrideCreate.
public void onPermissionOverrideCreate(PermissionOverrideCreateEvent event) {
Guild guild = event.getGuild();
GuildChannel channel = event.getChannel();
IPermissionHolder permissionHolder = event.getPermissionHolder();
PermissionOverride permissionOverride = event.getPermissionOverride();
ChannelType channelType = event.getChannelType();
LoggerEvent loggerEvent = channelType == ChannelType.CATEGORY ? LoggerEvent.CATEGORY_OVERRIDE_CREATE : channelType == ChannelType.STORE ? LoggerEvent.STORE_CHANNEL_OVERRIDE_CREATE : channelType == ChannelType.VOICE ? LoggerEvent.VOICE_CHANNEL_OVERRIDE_CREATE : LoggerEvent.TEXT_CHANNEL_OVERRIDE_CREATE;
LoggerContext loggerContext = new LoggerContext().setUser(event.isMemberOverride() ? permissionHolder.getIdLong() : 0L).setRole(event.isRoleOverride() ? permissionHolder.getIdLong() : 0L).setChannel(channel);
String message = LoggerUtility.getPermissionOverrideDifference(0L, Permission.ALL_PERMISSIONS, 0L, permissionOverride);
StringBuilder description = new StringBuilder();
description.append(String.format("The %s %s has had permission overrides created for %s", LoggerUtility.getChannelTypeReadable(channelType), channelType == ChannelType.CATEGORY ? "`" + channel.getName() + "`" : channel.getAsMention(), event.isRoleOverride() ? event.getRole().getAsMention() : "`" + event.getMember().getEffectiveName() + "`"));
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setColor(this.bot.getConfig().getGreen());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
embed.setFooter(new EmbedFooter(String.format("%s ID: %s", event.isRoleOverride() ? "Role" : "User", permissionHolder.getIdLong()), null));
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;
}
if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.CHANNEL_OVERRIDE_CREATE).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()).filter(e -> {
AuditLogChange allow = e.getChangeByKey("allow"), deny = e.getChangeByKey("deny");
int denyNew = deny == null ? (int) permissionOverride.getDeniedRaw() : deny.getNewValue();
int allowNew = allow == null ? (int) permissionOverride.getAllowedRaw() : allow.getNewValue();
return denyNew == permissionOverride.getDeniedRaw() && allowNew == permissionOverride.getAllowedRaw();
}).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
description.append(String.format(" by **%s**", moderator.getAsTag()));
}
description.append(message);
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
description.append(message);
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
Aggregations