use of com.sx4.bot.entities.management.LoggerEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onRoleCreate.
public void onRoleCreate(RoleCreateEvent event) {
Guild guild = event.getGuild();
Role role = event.getRole();
LoggerEvent loggerEvent = LoggerEvent.ROLE_CREATE;
LoggerContext loggerContext = new LoggerContext().setRole(role);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The role %s has been created", role.getAsMention()));
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("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;
}
if (!role.isManaged() && guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.ROLE_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() == role.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("The role %s has been created 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 onGuildMemberJoin.
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = event.getUser();
LoggerEvent loggerEvent = user.isBot() ? LoggerEvent.BOT_ADDED : LoggerEvent.MEMBER_JOIN;
LoggerContext loggerContext = new LoggerContext().setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setColor(this.bot.getConfig().getGreen());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), null));
embed.setFooter(new EmbedFooter(String.format("%s ID: %s", user.isBot() ? "Bot" : "User", member.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 (user.isBot()) {
StringBuilder description = new StringBuilder(String.format("`%s` was just added to the server", member.getEffectiveName()));
if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.BOT_ADD).whenComplete((logs, auditException) -> {
User moderator = logs == null ? null : logs.stream().filter(e -> e.getTargetIdLong() == member.getIdLong()).filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).map(AuditLogEntry::getUser).findFirst().orElse(null);
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());
} else {
embed.setDescription(String.format("`%s` just joined the server", member.getEffectiveName()));
}
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 onGuildMessageUpdate.
public void onGuildMessageUpdate(GuildMessageUpdateEvent event) {
Guild guild = event.getGuild();
TextChannel textChannel = event.getChannel();
Member member = event.getMember();
User user = event.getAuthor();
Message message = event.getMessage();
if (message.getTimeEdited() == null) {
return;
}
GuildMessage previousMessage = this.bot.getMessageCache().getMessageById(message.getIdLong());
if (previousMessage != null && message.isPinned() != previousMessage.isPinned()) {
return;
}
LoggerEvent loggerEvent = LoggerEvent.MESSAGE_UPDATE;
LoggerContext loggerContext = new LoggerContext().setUser(user).setChannel(textChannel);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
if (member != null) {
embed.setDescription(String.format("`%s` edited their [message](%s) in %s", member.getEffectiveName(), message.getJumpUrl(), textChannel.getAsMention()));
} else {
embed.setDescription(String.format("`%s` edited their [message](%s) in %s", user.getName(), message.getJumpUrl(), textChannel.getAsMention()));
}
embed.setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), null));
embed.setColor(this.bot.getConfig().getOrange());
embed.setTimestamp(Instant.now());
embed.setFooter(new EmbedFooter(String.format("Message ID: %s", message.getId()), null));
String oldContent = previousMessage == null ? null : previousMessage.getContent();
if (oldContent != null && !oldContent.isBlank()) {
embed.addField(new EmbedField(false, "Before", StringUtility.limit(oldContent, MessageEmbed.VALUE_MAX_LENGTH, "...")));
}
if (!message.getContentRaw().isBlank()) {
embed.addField(new EmbedField(false, "After", StringUtility.limit(message.getContentRaw(), MessageEmbed.VALUE_MAX_LENGTH, String.format("[...](%s)", message.getJumpUrl()))));
}
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.LoggerEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildMemberUpdateNickname.
public void onGuildMemberUpdateNickname(GuildMemberUpdateNicknameEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = event.getUser();
LoggerEvent loggerEvent = LoggerEvent.MEMBER_NICKNAME_UPDATE;
LoggerContext loggerContext = new LoggerContext().setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` has had their nickname changed", member.getEffectiveName()));
embed.setColor(this.bot.getConfig().getOrange());
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));
embed.addField(new EmbedField(false, "Before", String.format("`%s`", event.getOldNickname() != null ? event.getOldNickname() : member.getUser().getName())));
embed.addField(new EmbedField(false, "After", String.format("`%s`", event.getNewNickname() != null ? event.getNewNickname() : member.getUser().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.MEMBER_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() == user.getIdLong()).filter(e -> e.getChangeByKey(AuditLogKey.MEMBER_NICK) != null).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("`%s` has had their nickname changed by **%s**", member.getEffectiveName(), 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 onChannelDelete.
public void onChannelDelete(GuildChannel channel, LoggerEvent loggerEvent) {
Guild guild = channel.getGuild();
String typeReadable = LoggerUtility.getChannelTypeReadable(channel.getType());
LoggerContext loggerContext = new LoggerContext().setChannel(channel);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The %s `%s` has just been deleted", typeReadable, channel.getName()));
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", channel.getType() == ChannelType.CATEGORY ? "Category" : "Channel", channel.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.CHANNEL_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);
embed.setDescription(String.format("The %s `%s` has just been deleted by **%s**", typeReadable, channel.getName(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
Aggregations