use of gnu.trove.map.TLongIntMap in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onMessageBulkDelete.
public void onMessageBulkDelete(MessageBulkDeleteEvent event) {
List<String> messageIds = event.getMessageIds();
TextChannel textChannel = event.getChannel();
Guild guild = event.getGuild();
LoggerEvent loggerEvent = LoggerEvent.MESSAGE_DELETE;
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);
if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.MESSAGE_BULK_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 -> {
int count = Integer.parseInt(e.getOptionByName("count"));
int oldCount = guildCache.get(e.getIdLong());
guildCache.put(e.getIdLong(), count);
return (count == messageIds.size() && count != oldCount) || count > oldCount;
}).findFirst().orElse(null);
this.handleBulkMessages(textChannel, messageIds, data.getList("loggers", Document.class), loggerEvent, entry == null ? null : entry.getUser());
});
return;
}
this.handleBulkMessages(textChannel, messageIds, data.getList("loggers", Document.class), loggerEvent, null);
});
}
use of gnu.trove.map.TLongIntMap in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildVoiceLeave.
public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = member.getUser();
VoiceChannel channel = event.getChannelLeft();
LoggerContext loggerContext = new LoggerContext().setChannel(channel).setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` just left the voice channel %s", member.getEffectiveName(), channel.getAsMention()));
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 = data.getList("loggers", Document.class);
if (loggers.isEmpty()) {
return;
}
if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(event.getGuild(), ActionType.MEMBER_VOICE_KICK).whenComplete((logs, auditException) -> {
this.disconnectCache.putIfAbsent(guild.getIdLong(), new TLongIntHashMap());
TLongIntMap guildCache = this.disconnectCache.get(guild.getIdLong());
AuditLogEntry entry = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toMinutes() <= 10).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();
LoggerEvent loggerEvent = moderator == null ? LoggerEvent.MEMBER_VOICE_LEAVE : LoggerEvent.MEMBER_VOICE_DISCONNECT;
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("`%s` was disconnected from the voice channel %s by **%s**", member.getEffectiveName(), channel.getAsMention(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
LoggerEvent loggerEvent = LoggerEvent.MEMBER_VOICE_LEAVE;
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of gnu.trove.map.TLongIntMap 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 gnu.trove.map.TLongIntMap in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildVoiceMove.
public void onGuildVoiceMove(GuildVoiceMoveEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = member.getUser();
VoiceChannel joined = event.getChannelJoined(), left = event.getChannelLeft();
LoggerEvent loggerEvent = LoggerEvent.MEMBER_VOICE_MOVE;
LoggerContext loggerContext = new LoggerContext().setChannel(joined).setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` just changed voice channel", member.getEffectiveName()));
embed.setColor(this.bot.getConfig().getOrange());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(member.getUser().getAsTag(), member.getUser().getEffectiveAvatarUrl(), null));
embed.addField(new EmbedField(false, "Before", String.format("`%s`", left.getName())));
embed.addField(new EmbedField(false, "After", String.format("`%s`", joined.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(event.getGuild(), ActionType.MEMBER_VOICE_MOVE).whenComplete((logs, auditException) -> {
this.moveCache.putIfAbsent(joined.getIdLong(), new TLongIntHashMap());
TLongIntMap channelCache = this.moveCache.get(joined.getIdLong());
AuditLogEntry entry = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toMinutes() <= 10).filter(e -> Long.parseLong(e.getOptionByName("channel_id")) == joined.getIdLong()).filter(e -> {
int count = Integer.parseInt(e.getOptionByName("count"));
int oldCount = channelCache.get(e.getIdLong());
channelCache.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);
embed.setDescription(String.format("`%s` was moved voice channel by **%s**", member.getEffectiveName(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
Aggregations