Search in sources :

Example 1 with TargetedEntry

use of sx.blah.discord.handle.audit.entry.TargetedEntry in project DiscordSailv2 by Vaerys-Dawn.

the class LoggingHandler method doBanLog.

public static void doBanLog(UserBanEvent event) {
    IGuild guild = event.getGuild();
    GuildObject guildObject = Globals.getGuildContent(guild.getLongID());
    if (!guildObject.config.banLogging || !GuildHandler.testForPerms(Client.getClient().getOurUser(), guild, Permissions.VIEW_AUDIT_LOG))
        return;
    StringHandler output = new StringHandler("> **@%s#%s** was banned");
    output.setContent(String.format(output.toString(), event.getUser().getName(), event.getUser().getDiscriminator()));
    // get recent bans
    List<TargetedEntry> recentBans = event.getGuild().getAuditLog(ActionType.MEMBER_BAN_ADD).getEntriesByTarget(event.getUser().getLongID());
    if (recentBans.size() == 0)
        return;
    // and sort them. last entry is most recent.
    recentBans.sort(Comparator.comparingLong(o -> DiscordUtils.getSnowflakeTimeFromID(o.getLongID()).toEpochMilli()));
    AuditLogEntry lastBan = recentBans.get(recentBans.size() - 1);
    output.appendFormatted(" by **@%s#%s**", lastBan.getResponsibleUser().getName(), lastBan.getResponsibleUser().getDiscriminator());
    String reason = lastBan.getReason().isPresent() ? lastBan.getReason().get() : "No reason provided";
    output.appendFormatted(" with reason `%s`", reason);
    Utility.sendLog(output.toString(), guildObject, true);
}
Also used : GuildObject(com.github.vaerys.masterobjects.GuildObject) GuildMemberEvent(sx.blah.discord.handle.impl.events.guild.member.GuildMemberEvent) ChannelUpdateEvent(sx.blah.discord.handle.impl.events.guild.channel.ChannelUpdateEvent) UserBanEvent(sx.blah.discord.handle.impl.events.guild.member.UserBanEvent) ZonedDateTime(java.time.ZonedDateTime) ChannelSetting(com.github.vaerys.enums.ChannelSetting) LoggerFactory(org.slf4j.LoggerFactory) Client(com.github.vaerys.main.Client) MessageUpdateEvent(sx.blah.discord.handle.impl.events.guild.channel.message.MessageUpdateEvent) ArrayList(java.util.ArrayList) DiscordUtils(sx.blah.discord.api.internal.DiscordUtils) AuditLogEntry(sx.blah.discord.handle.audit.entry.AuditLogEntry) TargetedEntry(sx.blah.discord.handle.audit.entry.TargetedEntry) ActionType(sx.blah.discord.handle.audit.ActionType) ZoneOffset(java.time.ZoneOffset) CommandObject(com.github.vaerys.commands.CommandObject) Globals(com.github.vaerys.main.Globals) Logger(org.slf4j.Logger) ChannelCreateEvent(sx.blah.discord.handle.impl.events.guild.channel.ChannelCreateEvent) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) List(java.util.List) sx.blah.discord.handle.obj(sx.blah.discord.handle.obj) ChannelDeleteEvent(sx.blah.discord.handle.impl.events.guild.channel.ChannelDeleteEvent) Comparator(java.util.Comparator) Utility(com.github.vaerys.main.Utility) UserRoleUpdateEvent(sx.blah.discord.handle.impl.events.guild.member.UserRoleUpdateEvent) GuildObject(com.github.vaerys.masterobjects.GuildObject) AuditLogEntry(sx.blah.discord.handle.audit.entry.AuditLogEntry) TargetedEntry(sx.blah.discord.handle.audit.entry.TargetedEntry)

Example 2 with TargetedEntry

use of sx.blah.discord.handle.audit.entry.TargetedEntry in project DiscordSailv2 by Vaerys-Dawn.

the class LoggingHandler method doKickLog.

/**
 * Handler for logging Kicks.
 *
 * @param guild the Guild the user left.
 * @param user  the User that left the server.
 */
private static void doKickLog(IGuild guild, IUser user) {
    IUser botUser = Client.getClient().getOurUser();
    // test if the bot has auditLog perms
    if (!GuildHandler.testForPerms(botUser, guild, Permissions.VIEW_AUDIT_LOG))
        return;
    // getTimestamp
    long timeStamp = Instant.now().atZone(ZoneOffset.UTC).toEpochSecond() * 1000;
    // build Message
    StringHandler kickLog = new StringHandler("**@%s#%d** has been **Kicked** by **@%s#%d**");
    // do some checks to make sure the user was in fact kicked
    List<TargetedEntry> kicksLog = guild.getAuditLog(ActionType.MEMBER_KICK).getEntriesByTarget(user.getLongID());
    if (kicksLog.size() == 0)
        return;
    // sort kickLog and get latest entry
    kicksLog.sort(Comparator.comparingLong(o -> DiscordUtils.getSnowflakeTimeFromID(o.getLongID()).toEpochMilli()));
    AuditLogEntry lastKick = kicksLog.get(kicksLog.size() - 1);
    // get the latest entry's timestamp
    long lastKickTime = DiscordUtils.getSnowflakeTimeFromID(lastKick.getLongID()).toEpochMilli();
    // get user responsible
    IUser responsible = lastKick.getResponsibleUser();
    // Check if timestamp is within fifteen seconds either way, lastKick is valid.
    long timeDiff = Math.abs(timeStamp - lastKickTime);
    if (timeDiff > 15000)
        return;
    // format and send message
    kickLog.format(user.getName(), user.getDiscriminator(), responsible.getName(), responsible.getName());
    if (lastKick.getReason().isPresent()) {
        kickLog.appendFormatted(" with reason `%s`", lastKick.getReason().get());
    }
    // send log
    GuildObject content = Globals.getGuildContent(guild.getLongID());
    Utility.sendLog(kickLog.toString(), content, true);
}
Also used : GuildObject(com.github.vaerys.masterobjects.GuildObject) GuildMemberEvent(sx.blah.discord.handle.impl.events.guild.member.GuildMemberEvent) ChannelUpdateEvent(sx.blah.discord.handle.impl.events.guild.channel.ChannelUpdateEvent) UserBanEvent(sx.blah.discord.handle.impl.events.guild.member.UserBanEvent) ZonedDateTime(java.time.ZonedDateTime) ChannelSetting(com.github.vaerys.enums.ChannelSetting) LoggerFactory(org.slf4j.LoggerFactory) Client(com.github.vaerys.main.Client) MessageUpdateEvent(sx.blah.discord.handle.impl.events.guild.channel.message.MessageUpdateEvent) ArrayList(java.util.ArrayList) DiscordUtils(sx.blah.discord.api.internal.DiscordUtils) AuditLogEntry(sx.blah.discord.handle.audit.entry.AuditLogEntry) TargetedEntry(sx.blah.discord.handle.audit.entry.TargetedEntry) ActionType(sx.blah.discord.handle.audit.ActionType) ZoneOffset(java.time.ZoneOffset) CommandObject(com.github.vaerys.commands.CommandObject) Globals(com.github.vaerys.main.Globals) Logger(org.slf4j.Logger) ChannelCreateEvent(sx.blah.discord.handle.impl.events.guild.channel.ChannelCreateEvent) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) List(java.util.List) sx.blah.discord.handle.obj(sx.blah.discord.handle.obj) ChannelDeleteEvent(sx.blah.discord.handle.impl.events.guild.channel.ChannelDeleteEvent) Comparator(java.util.Comparator) Utility(com.github.vaerys.main.Utility) UserRoleUpdateEvent(sx.blah.discord.handle.impl.events.guild.member.UserRoleUpdateEvent) GuildObject(com.github.vaerys.masterobjects.GuildObject) AuditLogEntry(sx.blah.discord.handle.audit.entry.AuditLogEntry) TargetedEntry(sx.blah.discord.handle.audit.entry.TargetedEntry)

Example 3 with TargetedEntry

use of sx.blah.discord.handle.audit.entry.TargetedEntry in project Discord4J by Discord4J.

the class DiscordUtils method getAuditLogEntryFromJSON.

/**
 * Converts a json {@link AuditLogEntry} to a {@link AuditLogEntry}.
 *
 * @param guild The guild the entry belongs to.
 * @param users The users of the parent audit log.
 * @param webhooks The webhooks of the parent audit log.
 * @param json The converted audit log entry object.
 * @return The converted audit log entry.
 */
public static AuditLogEntry getAuditLogEntryFromJSON(IGuild guild, LongMap<IUser> users, LongMap<IWebhook> webhooks, AuditLogEntryObject json) {
    long targetID = json.target_id == null ? 0 : Long.parseUnsignedLong(json.target_id);
    long id = Long.parseUnsignedLong(json.id);
    IUser user = users.get(Long.parseUnsignedLong(json.user_id));
    ChangeMap changes = json.changes == null ? new ChangeMap() : Arrays.stream(json.changes).collect(ChangeMap.Collector.toChangeMap());
    OptionMap options = new OptionMap(json.options);
    ActionType actionType = ActionType.fromRaw(json.action_type);
    switch(actionType) {
        case GUILD_UPDATE:
            return new DiscordObjectEntry<>(guild, id, user, changes, json.reason, actionType, options);
        case CHANNEL_CREATE:
        case CHANNEL_UPDATE:
        case CHANNEL_OVERWRITE_CREATE:
        case CHANNEL_OVERWRITE_UPDATE:
        case CHANNEL_OVERWRITE_DELETE:
            IChannel channel = guild.getChannelByID(targetID);
            if (channel == null)
                channel = guild.getVoiceChannelByID(targetID);
            if (channel == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(channel, id, user, changes, json.reason, actionType, options);
        case MEMBER_KICK:
        case MEMBER_BAN_ADD:
        case MEMBER_BAN_REMOVE:
        case MEMBER_UPDATE:
        case MEMBER_ROLE_UPDATE:
        case // message delete target is the author of the message
        MESSAGE_DELETE:
            IUser target = users.get(targetID);
            if (target == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(target, id, user, changes, json.reason, actionType, options);
        case ROLE_CREATE:
        case ROLE_UPDATE:
            IRole role = guild.getRoleByID(targetID);
            if (role == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(role, id, user, changes, json.reason, actionType, options);
        case WEBHOOK_CREATE:
        case WEBHOOK_UPDATE:
            IWebhook webhook = webhooks.get(targetID);
            if (webhook == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(webhook, id, user, changes, json.reason, actionType, options);
        case EMOJI_CREATE:
        case EMOJI_UPDATE:
            IEmoji emoji = guild.getEmojiByID(targetID);
            if (emoji == null) {
                return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
            }
            return new DiscordObjectEntry<>(emoji, id, user, changes, json.reason, actionType, options);
        case CHANNEL_DELETE:
        case ROLE_DELETE:
        case WEBHOOK_DELETE:
        case EMOJI_DELETE:
            return new TargetedEntry(id, user, changes, json.reason, actionType, options, targetID);
        case INVITE_CREATE:
        case INVITE_DELETE:
        case INVITE_UPDATE:
        case MEMBER_PRUNE:
            return new AuditLogEntry(id, user, changes, json.reason, actionType, options);
    }
    return null;
}
Also used : ChangeMap(sx.blah.discord.handle.audit.entry.change.ChangeMap) ActionType(sx.blah.discord.handle.audit.ActionType) AuditLogEntry(sx.blah.discord.handle.audit.entry.AuditLogEntry) TargetedEntry(sx.blah.discord.handle.audit.entry.TargetedEntry) OptionMap(sx.blah.discord.handle.audit.entry.option.OptionMap) DiscordObjectEntry(sx.blah.discord.handle.audit.entry.DiscordObjectEntry)

Aggregations

ActionType (sx.blah.discord.handle.audit.ActionType)3 AuditLogEntry (sx.blah.discord.handle.audit.entry.AuditLogEntry)3 TargetedEntry (sx.blah.discord.handle.audit.entry.TargetedEntry)3 CommandObject (com.github.vaerys.commands.CommandObject)2 ChannelSetting (com.github.vaerys.enums.ChannelSetting)2 Client (com.github.vaerys.main.Client)2 Globals (com.github.vaerys.main.Globals)2 Utility (com.github.vaerys.main.Utility)2 GuildObject (com.github.vaerys.masterobjects.GuildObject)2 Instant (java.time.Instant)2 ZoneOffset (java.time.ZoneOffset)2 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 DiscordUtils (sx.blah.discord.api.internal.DiscordUtils)2 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)2