use of net.dv8tion.jda.api.events.guild.GuildBanEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildBan.
public void onGuildBan(GuildBanEvent event) {
Guild guild = event.getGuild();
User user = event.getUser();
LoggerEvent loggerEvent = LoggerEvent.MEMBER_BANNED;
LoggerContext loggerContext = new LoggerContext().setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` has been banned", 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));
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.BAN).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() == user.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("`%s` has been banned by **%s**", user.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.events.guild.GuildBanEvent 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.events.guild.GuildBanEvent in project Gary by help-chat.
the class LoggingHandler method execute.
@Override
protected void execute(GenericEvent event) {
switch(EventsEnum.fromEvent(event)) {
case MEMBER_JOIN:
GuildMemberJoinEvent e = (GuildMemberJoinEvent) event;
log(EventsEnum.MEMBER_JOIN, e.getJDA(), e.getGuild(), e.getUser());
break;
case MEMBER_LEAVE:
if (!wasBan) {
GuildMemberLeaveEvent e2 = (GuildMemberLeaveEvent) event;
log(EventsEnum.MEMBER_LEAVE, e2.getJDA(), e2.getGuild(), e2.getUser());
} else {
wasBan = false;
}
break;
case MEMBER_BANNED:
wasBan = true;
GuildBanEvent e3 = (GuildBanEvent) event;
log(EventsEnum.MEMBER_BANNED, e3.getJDA(), e3.getGuild(), e3.getUser());
break;
case MESSAGE_EDIT:
GuildMessageUpdateEvent e5 = (GuildMessageUpdateEvent) event;
if (!e5.getAuthor().isBot() && StringUtils.equalsIgnoreCase(e5.getChannel().getName(), Constants.CHANNELS)) {
log(EventsEnum.MESSAGE_EDIT, e5.getJDA(), e5.getGuild(), e5.getAuthor(), e5.getChannel(), e5.getMessage());
}
break;
case MESSAGE_DELETE:
GuildMessageDeleteEvent e6 = (GuildMessageDeleteEvent) event;
if ((StringUtils.equalsIgnoreCase(e6.getChannel().getName(), Constants.CHANNELS))) {
log(EventsEnum.MESSAGE_DELETE, e6.getJDA(), e6.getGuild(), MessageUtils.getAuthor(e6.getMessageIdLong()), e6.getChannel(), e6.getMessageIdLong(), MessageUtils.getMessage(e6.getMessageIdLong()));
}
break;
case MESSAGE_BULK_DELETE:
MessageBulkDeleteEvent e7 = (MessageBulkDeleteEvent) event;
if ((StringUtils.equalsIgnoreCase(e7.getChannel().getName(), Constants.CHANNELS))) {
log(EventsEnum.MESSAGE_BULK_DELETE, e7.getJDA(), e7.getGuild(), e7.getChannel(), e7.getMessageIds());
}
break;
case VOICE_JOIN:
GuildVoiceJoinEvent e8 = (GuildVoiceJoinEvent) event;
log(EventsEnum.VOICE_JOIN, e8.getJDA(), e8.getGuild(), e8.getMember().getUser(), e8.getChannelJoined());
break;
}
}
use of net.dv8tion.jda.api.events.guild.GuildBanEvent in project JDA by DV8FromTheWorld.
the class GuildBanHandler method handleInternally.
@Override
protected Long handleInternally(DataObject content) {
final long id = content.getLong("guild_id");
if (getJDA().getGuildSetupController().isLocked(id))
return id;
DataObject userJson = content.getObject("user");
GuildImpl guild = (GuildImpl) getJDA().getGuildById(id);
if (guild == null) {
getJDA().getEventCache().cache(EventCache.Type.GUILD, id, responseNumber, allContent, this::handle);
EventCache.LOG.debug("Received Guild Member {} event for a Guild not yet cached.", JDALogger.getLazyString(() -> banned ? "Ban" : "Unban"));
return null;
}
User user = getJDA().getEntityBuilder().createUser(userJson);
if (banned) {
getJDA().handleEvent(new GuildBanEvent(getJDA(), responseNumber, guild, user));
} else {
getJDA().handleEvent(new GuildUnbanEvent(getJDA(), responseNumber, guild, user));
}
return null;
}
Aggregations