use of net.dv8tion.jda.api.audit.AuditLogEntry 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 net.dv8tion.jda.api.audit.AuditLogEntry 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());
}
});
}
use of net.dv8tion.jda.api.audit.AuditLogEntry in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onEmoteAdded.
public void onEmoteAdded(EmoteAddedEvent event) {
Guild guild = event.getGuild();
Emote emote = event.getEmote();
LoggerEvent loggerEvent = LoggerEvent.EMOTE_CREATE;
LoggerContext loggerContext = new LoggerContext().setEmote(emote);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("The emote %s has been created", emote.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("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_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() == emote.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("The emote %s has been created by **%s**", emote.getAsMention(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of net.dv8tion.jda.api.audit.AuditLogEntry in project GeyserDiscordBot by GeyserMC.
the class LogHandler method onGuildBan.
@Override
public void onGuildBan(@NotNull GuildBanEvent event) {
Guild.Ban ban = event.getGuild().retrieveBan(event.getUser()).complete();
// Get the ban from the audit log to get the user that created it
AuditLogEntry banLog = event.getGuild().retrieveAuditLogs().type(ActionType.BAN).stream().filter(auditLogEntry -> auditLogEntry.getTargetIdLong() == ban.getUser().getIdLong()).findFirst().orElse(null);
GeyserBot.getGeneralThreadPool().schedule(() -> {
AuditLogEntry newBanLog;
if (banLog == null) {
newBanLog = event.getGuild().retrieveAuditLogs().type(ActionType.BAN).stream().filter(auditLogEntry -> auditLogEntry.getTargetIdLong() == ban.getUser().getIdLong()).findFirst().orElse(null);
// Still null, shouldn't happen but just quietly fail
if (newBanLog == null) {
return;
}
} else {
newBanLog = banLog;
}
// Don't log bans by the bot (they are handled separately)
if (newBanLog.getUser().getIdLong() != event.getJDA().getSelfUser().getIdLong()) {
// Log the change
int id = GeyserBot.storageManager.addLog(event.getGuild().getMember(newBanLog.getUser()), "ban", event.getUser(), ban.getReason());
// Send the embed as a reply and to the log
try {
ServerSettings.getLogChannel(event.getGuild()).sendMessageEmbeds(new EmbedBuilder().setTitle("Banned user").addField("User", event.getUser().getAsMention(), false).addField("Staff member", newBanLog.getUser().getAsMention(), false).addField("Reason", ban.getReason(), false).setFooter("ID: " + id).setTimestamp(Instant.now()).setColor(BotColors.FAILURE.getColor()).build()).queue();
} catch (IllegalArgumentException ignored) {
}
}
}, banLog == null ? 5 : 0, TimeUnit.SECONDS);
}
use of net.dv8tion.jda.api.audit.AuditLogEntry in project JDA by DV8FromTheWorld.
the class AuditLogPaginationActionImpl method handleSuccess.
@Override
protected void handleSuccess(Response response, Request<List<AuditLogEntry>> request) {
DataObject obj = response.getObject();
DataArray users = obj.getArray("users");
DataArray webhooks = obj.getArray("webhooks");
DataArray entries = obj.getArray("audit_log_entries");
List<AuditLogEntry> list = new ArrayList<>(entries.length());
EntityBuilder builder = api.getEntityBuilder();
TLongObjectMap<DataObject> userMap = new TLongObjectHashMap<>();
for (int i = 0; i < users.length(); i++) {
DataObject user = users.getObject(i);
userMap.put(user.getLong("id"), user);
}
TLongObjectMap<DataObject> webhookMap = new TLongObjectHashMap<>();
for (int i = 0; i < webhooks.length(); i++) {
DataObject webhook = webhooks.getObject(i);
webhookMap.put(webhook.getLong("id"), webhook);
}
for (int i = 0; i < entries.length(); i++) {
try {
DataObject entry = entries.getObject(i);
DataObject user = userMap.get(entry.getLong("user_id", 0));
DataObject webhook = webhookMap.get(entry.getLong("target_id", 0));
AuditLogEntry result = builder.createAuditLogEntry((GuildImpl) guild, entry, user, webhook);
list.add(result);
if (this.useCache)
this.cached.add(result);
this.last = result;
this.lastKey = last.getIdLong();
} catch (ParsingException | NullPointerException e) {
LOG.warn("Encountered exception in AuditLogPagination", e);
}
}
request.onSuccess(list);
}
Aggregations