use of club.minnced.discord.webhook.send.WebhookEmbedBuilder 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 club.minnced.discord.webhook.send.WebhookEmbedBuilder 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());
});
}
use of club.minnced.discord.webhook.send.WebhookEmbedBuilder in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onEmoteRemoved.
public void onEmoteRemoved(EmoteRemovedEvent event) {
Guild guild = event.getGuild();
Emote emote = event.getEmote();
LoggerEvent loggerEvent = LoggerEvent.EMOTE_DELETE;
LoggerContext loggerContext = new LoggerContext().setEmote(emote);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The emote `%s` has been deleted", emote.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("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);
List<Document> loggers = LoggerUtility.getValidLoggers(data.getList("loggers", Document.class), loggerEvent, loggerContext);
if (loggers.isEmpty()) {
return;
}
if (!emote.isManaged() && guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.EMOTE_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() == emote.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("The emote `%s` has been deleted by **%s**", emote.getName(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of club.minnced.discord.webhook.send.WebhookEmbedBuilder in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onRoleDelete.
public void onRoleDelete(RoleDeleteEvent event) {
Guild guild = event.getGuild();
Role role = event.getRole();
LoggerEvent loggerEvent = LoggerEvent.ROLE_DELETE;
LoggerContext loggerContext = new LoggerContext().setRole(role);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The role `%s` has been deleted", role.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("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_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() == role.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("The role `%s` has been deleted by **%s**", role.getName(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of club.minnced.discord.webhook.send.WebhookEmbedBuilder in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onChannelCreate.
public void onChannelCreate(GuildChannel channel, LoggerEvent loggerEvent) {
Guild guild = channel.getGuild();
ChannelType channelType = channel.getType();
String typeReadable = LoggerUtility.getChannelTypeReadable(channelType);
LoggerContext loggerContext = new LoggerContext().setChannel(channel);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The %s `%s` has just been created", typeReadable, channel.getName()));
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", 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_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()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("The %s %s has just been created by **%s**", typeReadable, channelType == ChannelType.CATEGORY ? "`" + channel.getName() + "`" : channel.getAsMention(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
Aggregations