use of com.sx4.bot.entities.management.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildVoiceJoin.
public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = member.getUser();
VoiceChannel channel = event.getChannelJoined();
LoggerEvent loggerEvent = LoggerEvent.MEMBER_VOICE_JOIN;
LoggerContext loggerContext = new LoggerContext().setUser(user).setChannel(channel);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setDescription(String.format("`%s` just joined the voice channel %s", member.getEffectiveName(), channel.getAsMention())).setColor(this.bot.getConfig().getGreen()).setTimestamp(Instant.now()).setFooter(new EmbedFooter(String.format("User ID: %s", member.getId()), null)).setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), 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);
this.queue(guild, data.getList("loggers", Document.class), loggerEvent, loggerContext, embed.build());
});
}
use of com.sx4.bot.entities.management.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method handleBulkMessages.
public void handleBulkMessages(TextChannel textChannel, List<String> messageIds, List<Document> loggers, LoggerEvent loggerEvent, User moderator) {
Guild guild = textChannel.getGuild();
for (Document logger : loggers) {
if ((logger.get("events", LoggerEvent.ALL) & loggerEvent.getRaw()) != loggerEvent.getRaw()) {
continue;
}
long channelId = logger.getLong("channelId");
TextChannel channel = guild.getTextChannelById(channelId);
if (channel == null) {
continue;
}
List<Document> entities = logger.getEmbedded(List.of("blacklist", "entities"), Collections.emptyList());
List<WebhookEmbed> embeds = new ArrayList<>();
for (String messageId : messageIds) {
WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setColor(this.bot.getConfig().getRed()).setTimestamp(Instant.now()).setFooter(new EmbedFooter("Message ID: " + messageId, null));
LoggerContext loggerContext = new LoggerContext().setChannel(textChannel);
if (moderator != null) {
loggerContext.setModerator(moderator);
}
String reason = moderator == null ? "in a bulk delete" : "by **" + moderator.getAsTag() + "**";
GuildMessage message = this.bot.getMessageCache().getMessageById(messageId);
if (message == null) {
if (!LoggerUtility.isWhitelisted(entities, loggerEvent, loggerContext)) {
continue;
}
embed.setDescription(String.format("A message sent in %s was deleted %s", textChannel.getAsMention(), reason));
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
} else {
User author = message.getAuthor();
loggerContext.setUser(author);
if (!LoggerUtility.isWhitelisted(entities, loggerEvent, loggerContext)) {
continue;
}
embed.setDescription(String.format("The message sent by `%s` in %s was deleted %s", author.getName(), textChannel.getAsMention(), reason));
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, "...")));
}
}
embeds.add(embed.build());
}
this.getManager(channelId).queue(channel, logger, embeds);
}
}
use of com.sx4.bot.entities.management.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildBan.
public void onGuildBan(GuildBanEvent event) {
Guild guild = event.getGuild();
User user = event.getUser();
LoggerEvent loggerEvent = LoggerEvent.MEMBER_BANNED;
LoggerContext loggerContext = new LoggerContext().setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` has been banned", user.getName()));
embed.setColor(this.bot.getConfig().getRed());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), null));
embed.setFooter(new EmbedFooter(String.format("User ID: %s", user.getId()), 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.BAN).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() == user.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("`%s` has been banned by **%s**", user.getName(), 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.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onRoleUpdatePermissions.
public void onRoleUpdatePermissions(RoleUpdatePermissionsEvent event) {
Guild guild = event.getGuild();
Role role = event.getRole();
LoggerEvent loggerEvent = LoggerEvent.ROLE_PERMISSION_UPDATE;
LoggerContext loggerContext = new LoggerContext().setRole(role);
String permissionMessage = LoggerUtility.getRolePermissionDifference(event.getOldPermissionsRaw(), event.getNewPermissionsRaw());
if (permissionMessage.isEmpty()) {
return;
}
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("Role ID: %s", role.getId()), 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;
}
StringBuilder description = new StringBuilder(String.format("The role %s has had permission changes made", role.getAsMention()));
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_PERMISSIONS) != null).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
description.append(String.format(" by **%s**", moderator.getAsTag()));
}
description.append(permissionMessage);
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
description.append(permissionMessage);
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of com.sx4.bot.entities.management.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onEmoteUpdateRoles.
public void onEmoteUpdateRoles(EmoteUpdateRolesEvent event) {
Guild guild = event.getGuild();
Emote emote = event.getEmote();
List<Role> newRoles = event.getNewRoles(), oldRoles = event.getOldRoles();
LoggerEvent loggerEvent = LoggerEvent.EMOTE_ROLES_UPDATE;
LoggerContext loggerContext = new LoggerContext().setEmote(emote);
Pair<List<Role>, List<Role>> roles = LoggerUtility.getRoleDifference(newRoles, oldRoles);
List<Role> rolesAdded = roles.getLeft(), rolesRemoved = roles.getRight();
StringBuilder description = new StringBuilder(String.format("The emote %s has had its role whitelist updated", emote.getAsMention()));
/* This event isn't sent when a role is deleted, I'll leave this here in case
if (rolesAdded.size() == 0 && rolesRemoved.size() == 1 && guild.getRoleById(rolesRemoved.get(0).getIdLong()) == null) {
description.append(" by **role deletion**");
}*/
description.append(LoggerUtility.getRoleDifferenceMessage(rolesRemoved, rolesAdded, description.length()));
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(description.toString());
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("Emote ID: %s", emote.getId()), 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);
this.queue(guild, data.getList("loggers", Document.class), loggerEvent, loggerContext, embed.build());
});
}
Aggregations