use of com.sx4.bot.entities.cache.GuildMessage 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.cache.GuildMessage 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());
});
}
use of com.sx4.bot.entities.cache.GuildMessage 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());
});
}
Aggregations