use of com.sx4.bot.entities.management.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildVoiceGuildDeafen.
public void onGuildVoiceGuildDeafen(GuildVoiceGuildDeafenEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = member.getUser();
GuildVoiceState voiceState = event.getVoiceState();
VoiceChannel channel = voiceState.getChannel();
boolean deafened = voiceState.isGuildDeafened();
LoggerEvent loggerEvent = deafened ? LoggerEvent.MEMBER_SERVER_VOICE_DEAFEN : LoggerEvent.MEMBER_SERVER_VOICE_UNDEAFEN;
LoggerContext loggerContext = new LoggerContext().setChannel(channel == null ? 0L : channel.getIdLong()).setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` has been %s", member.getEffectiveName(), deafened ? "deafened" : "undeafened"));
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(member.getUser().getAsTag(), member.getUser().getEffectiveAvatarUrl(), null));
embed.setFooter(new EmbedFooter(String.format("User ID: %s", user.getId()), null));
embed.setColor(deafened ? this.bot.getConfig().getRed() : this.bot.getConfig().getGreen());
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() == member.getUser().getIdLong()).filter(e -> e.getChangeByKey(AuditLogKey.MEMBER_DEAF) != null).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("`%s` has been %s by **%s**", member.getEffectiveName(), deafened ? "deafened" : "undeafened", 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 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 com.sx4.bot.entities.management.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildMemberRoleAdd.
public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = event.getUser();
List<Role> roles = event.getRoles();
Role firstRole = roles.get(0);
boolean multiple = roles.size() > 1;
LoggerEvent loggerEvent = LoggerEvent.MEMBER_ROLE_ADD;
LoggerContext loggerContext = new LoggerContext().setRole(multiple ? 0L : firstRole.getIdLong()).setUser(user);
StringBuilder description = new StringBuilder();
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setColor(this.bot.getConfig().getGreen());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), null));
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 */
15 - /* " 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 added to `%s`", builder, member.getEffectiveName()));
} else {
description.append(String.format("The role %s has been added to `%s`", firstRole.getAsMention(), member.getEffectiveName()));
embed.setFooter(new EmbedFooter(String.format("Role ID: %s", firstRole.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 (!firstRole.isManaged() && guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(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_ADD);
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.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onPermissionOverrideCreate.
public void onPermissionOverrideCreate(PermissionOverrideCreateEvent 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(0L, Permission.ALL_PERMISSIONS, 0L, permissionOverride);
StringBuilder description = new StringBuilder();
description.append(String.format("The %s %s has had permission overrides created 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().getGreen());
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");
int denyNew = deny == null ? (int) permissionOverride.getDeniedRaw() : deny.getNewValue();
int allowNew = allow == null ? (int) permissionOverride.getAllowedRaw() : allow.getNewValue();
return denyNew == permissionOverride.getDeniedRaw() && allowNew == permissionOverride.getAllowedRaw();
}).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.LoggerContext in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildMemberRemove.
public void onGuildMemberRemove(GuildMemberRemoveEvent event) {
Guild guild = event.getGuild();
User user = event.getUser();
Member member = event.getMember();
List<Role> roles = member == null ? Collections.emptyList() : member.getRoles();
String rolesMessage = StringUtility.joinLimited(", ", roles, Role::getAsMention, MessageEmbed.VALUE_MAX_LENGTH);
LoggerContext loggerContext = new LoggerContext().setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` just left the server", 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));
if (rolesMessage.length() != 0) {
embed.addField(new EmbedField(true, "Roles", rolesMessage));
}
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(guild, ActionType.KICK).whenComplete((logs, auditException) -> {
User moderator = logs == null ? null : logs.stream().filter(e -> e.getTargetIdLong() == user.getIdLong()).filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).map(AuditLogEntry::getUser).findFirst().orElse(null);
LoggerEvent loggerEvent = moderator == null ? LoggerEvent.MEMBER_LEAVE : LoggerEvent.MEMBER_KICKED;
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("`%s` has been kicked by **%s**", user.getName(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
return;
}
LoggerEvent loggerEvent = LoggerEvent.MEMBER_LEAVE;
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
}
Aggregations