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);
}
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);
}
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;
}
Aggregations