use of com.sx4.bot.entities.management.LoggerEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildMemberRoleRemove.
public void onGuildMemberRoleRemove(GuildMemberRoleRemoveEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = event.getUser();
List<Role> roles = event.getRoles();
Role firstRole = roles.get(0);
// Ensure role delete event has been sent just in case
this.delay(() -> {
StringBuilder description = new StringBuilder();
boolean multiple = roles.size() > 1;
LoggerEvent loggerEvent = LoggerEvent.MEMBER_ROLE_REMOVE;
LoggerContext loggerContext = new LoggerContext().setRole(multiple ? 0L : firstRole.getIdLong()).setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setColor(this.bot.getConfig().getRed());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), null));
boolean deleted;
if (multiple) {
StringBuilder builder = new StringBuilder();
/* Make sure there is always enough space to write all the components to it */
int maxLength = MessageEmbed.DESCRIPTION_MAX_LENGTH - 32 - /* Max nickname length */
32 - /* Result String overhead */
16 - /* " and x more" overhead */
3;
for (int i = 0; i < roles.size(); i++) {
Role role = roles.get(i);
String entry = (i == roles.size() - 1 ? " and " : i != 0 ? ", " : "") + role.getAsMention();
if (builder.length() + entry.length() < maxLength) {
builder.append(entry);
} else {
builder.append(String.format(" and **%s** more", roles.size() - i));
break;
}
}
description.append(String.format("The roles %s have been removed from `%s`", builder, member.getEffectiveName()));
deleted = false;
} else {
deleted = guild.getRoleById(firstRole.getIdLong()) == null;
description.append(String.format("The role %s has been removed from `%s`", deleted ? "`" + firstRole.getName() + "`" : firstRole.getAsMention(), member.getEffectiveName()));
embed.setFooter(new EmbedFooter(String.format("Role ID: %s", firstRole.getId()), null));
if (deleted) {
description.append(" by **role deletion**");
}
}
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 (!deleted && !firstRole.isManaged() && guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogs(guild, ActionType.MEMBER_ROLE_UPDATE).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() == member.getUser().getIdLong()).filter(e -> {
AuditLogChange change = e.getChangeByKey(AuditLogKey.MEMBER_ROLES_REMOVE);
if (change == null) {
return false;
}
List<Map<String, String>> roleEntries = change.getNewValue();
List<String> roleIds = roleEntries.stream().map(roleEntry -> roleEntry.get("id")).collect(Collectors.toList());
for (Role role : roles) {
if (!roleIds.contains(role.getId())) {
return false;
}
}
return true;
}).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());
}
});
});
}
use of com.sx4.bot.entities.management.LoggerEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onPermissionOverrideUpdate.
public void onPermissionOverrideUpdate(PermissionOverrideUpdateEvent 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(event.getOldAllowRaw(), event.getOldInheritedRaw(), event.getOldDenyRaw(), permissionOverride);
StringBuilder description = new StringBuilder(String.format("The %s %s has had permission overrides updated 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().getOrange());
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");
Integer denyNewValue = deny == null ? null : deny.getNewValue(), denyOldValue = deny == null ? null : deny.getOldValue();
Integer allowNewValue = allow == null ? null : allow.getNewValue(), allowOldValue = allow == null ? null : allow.getOldValue();
int denyNew = denyNewValue == null ? (int) permissionOverride.getDeniedRaw() : denyNewValue, denyOld = denyOldValue == null ? (int) event.getOldDenyRaw() : denyOldValue;
int allowNew = allowNewValue == null ? (int) permissionOverride.getAllowedRaw() : allowNewValue, allowOld = allowOldValue == null ? (int) event.getOldAllowRaw() : allowOldValue;
return denyNew == permissionOverride.getDeniedRaw() && denyOld == event.getOldDenyRaw() && allowNew == permissionOverride.getAllowedRaw() && allowOld == event.getOldAllowRaw();
}).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());
}
});
}
use of com.sx4.bot.entities.management.LoggerEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onRoleUpdateName.
public void onRoleUpdateName(RoleUpdateNameEvent event) {
Guild guild = event.getGuild();
Role role = event.getRole();
LoggerEvent loggerEvent = LoggerEvent.ROLE_NAME_UPDATE;
LoggerContext loggerContext = new LoggerContext().setRole(role);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The role %s has been renamed", role.getAsMention()));
embed.setColor(this.bot.getConfig().getOrange());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
embed.setFooter(new EmbedFooter(String.format("Role ID: %s", role.getId()), null));
embed.addField(new EmbedField(false, "Before", String.format("`%s`", event.getOldName())));
embed.addField(new EmbedField(false, "After", String.format("`%s`", event.getNewName())));
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.ROLE_UPDATE).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() == role.getIdLong()).filter(e -> e.getChangeByKey(AuditLogKey.ROLE_NAME) != null).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("The role %s has been renamed by **%s**", role.getAsMention(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of com.sx4.bot.entities.management.LoggerEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onChannelUpdateName.
public void onChannelUpdateName(GuildChannel channel, String oldName, LoggerEvent loggerEvent) {
Guild guild = channel.getGuild();
ChannelType channelType = channel.getType();
String typeReadable = LoggerUtility.getChannelTypeReadable(channelType);
LoggerContext loggerContext = new LoggerContext().setChannel(channel);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The %s `%s` has just been renamed", typeReadable, channel.getName()));
embed.setColor(this.bot.getConfig().getOrange());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
embed.setFooter(new EmbedFooter(String.format("%s ID: %s", channel.getType() == ChannelType.CATEGORY ? "Category" : "Channel", channel.getId()), null));
embed.addField(new EmbedField(false, "Before", String.format("`%s`", oldName)));
embed.addField(new EmbedField(false, "After", String.format("`%s`", channel.getName())));
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_UPDATE).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 -> e.getChangeByKey(AuditLogKey.CHANNEL_NAME) != null).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("The %s %s has just been renamed by **%s**", typeReadable, channelType == ChannelType.CATEGORY ? "`" + channel.getName() + "`" : channel.getAsMention(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of com.sx4.bot.entities.management.LoggerEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildMessageDelete.
public void onGuildMessageDelete(GuildMessageDeleteEvent event) {
TextChannel channel = event.getChannel();
Guild guild = event.getGuild();
LoggerEvent loggerEvent = LoggerEvent.MESSAGE_DELETE;
LoggerContext loggerContext = new LoggerContext().setChannel(channel);
GuildMessage message = this.bot.getMessageCache().getMessageById(event.getMessageIdLong());
if (message != null) {
loggerContext.setUser(message.getAuthor());
}
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;
}
WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setColor(this.bot.getConfig().getRed()).setTimestamp(Instant.now()).setFooter(new EmbedFooter("Message ID: " + event.getMessageId(), null));
StringBuilder description = new StringBuilder();
if (message == null) {
description.append(String.format("A message sent in %s was deleted", channel.getAsMention()));
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
} else {
User author = message.getAuthor();
description.append(String.format("The message sent by `%s` in %s was deleted", author.getAsTag(), channel.getAsMention()));
embed.setAuthor(new EmbedAuthor(author.getAsTag(), author.getEffectiveAvatarUrl(), null));
String content = message.getContent();
if (!content.isBlank()) {
embed.addField(new EmbedField(false, "Message", StringUtility.limit(content, MessageEmbed.VALUE_MAX_LENGTH, "...")));
}
}
if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.MESSAGE_DELETE).whenComplete((logs, auditException) -> {
this.messageCache.putIfAbsent(guild.getIdLong(), new TLongIntHashMap());
TLongIntMap guildCache = this.messageCache.get(guild.getIdLong());
AuditLogEntry entry = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toMinutes() <= 5).filter(e -> Long.parseLong(e.getOptionByName("channel_id")) == channel.getIdLong()).filter(e -> {
int count = Integer.parseInt(e.getOptionByName("count"));
int oldCount = guildCache.get(e.getIdLong());
guildCache.put(e.getIdLong(), count);
return (count == 1 && count != oldCount) || count > oldCount;
}).findFirst().orElse(null);
User moderator = entry == null ? null : entry.getUser();
if (moderator != null) {
loggerContext.setModerator(moderator);
description.append(" by **").append(moderator.getAsTag()).append("**");
}
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
return;
}
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
}
Aggregations