use of com.sx4.bot.entities.mod.Reason in project Sx4 by sx4-discord-bot.
the class OffencesCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
Bson filter = Filters.eq("guildId", event.getGuild().getIdLong());
if (member != null) {
filter = Filters.and(filter, Filters.eq("targetId", member.getIdLong()));
}
event.getMongo().getOffences(filter, Sorts.descending("_id")).whenComplete((iterable, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
try {
List<Document> data = iterable.into(new ArrayList<>());
if (data.isEmpty()) {
event.replyFailure((member == null ? "This server" : "**" + member.getUser().getAsTag() + "**") + " has no offences").queue();
return;
}
User user = member == null ? null : member.getUser();
PagedResult<Document> paged = new PagedResult<>(event.getBot(), data).setPerPage(6).setCustomFunction(page -> {
EmbedBuilder embed = new EmbedBuilder().setAuthor("Offences", null, member == null ? event.getGuild().getIconUrl() : user.getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
page.forEach((offence, index) -> {
Action action = Action.fromData(offence.get("action", Document.class));
ObjectId id = offence.getObjectId("_id");
OffsetDateTime time = OffsetDateTime.ofInstant(Instant.ofEpochSecond(id.getTimestamp()), ZoneOffset.UTC);
long targetId = offence.getLong("targetId");
User target = member == null ? event.getShardManager().getUserById(targetId) : user;
String targetContent = target == null ? "Anonymous#0000 (" + targetId + ")" : target.getAsTag();
long moderatorId = offence.getLong("moderatorId");
User moderator = event.getShardManager().getUserById(moderatorId);
String moderatorContent = moderator == null ? "Anonymous#0000 (" + moderatorId + ")" : moderator.getAsTag();
embed.addField(action.toString(), String.format("Target: %s\nModerator: %s\nReason: %s\nTime: %s", targetContent, moderatorContent, offence.get("reason", "None Given"), time.format(this.formatter)), true);
});
return new MessageBuilder().setEmbeds(embed.build());
});
paged.execute(event);
} catch (Throwable e) {
e.printStackTrace();
}
});
}
use of com.sx4.bot.entities.mod.Reason in project Sx4 by sx4-discord-bot.
the class ModLogCommand method view.
@Command(value = "view", aliases = { "viewcase", "view case", "list" }, description = "View a mod log case from the server")
@CommandId(70)
@Examples({ "modlog view 5e45ce6d3688b30ee75201ae", "modlog view" })
public void view(Sx4CommandEvent event, @Argument(value = "id", nullDefault = true) ObjectId id) {
Bson projection = Projections.include("moderatorId", "reason", "targetId", "action");
if (id == null) {
List<Document> allData = event.getMongo().getModLogs(Filters.eq("guildId", event.getGuild().getIdLong()), projection).into(new ArrayList<>());
if (allData.isEmpty()) {
event.replyFailure("There are no mod logs in this server").queue();
return;
}
PagedResult<Document> paged = new PagedResult<>(event.getBot(), allData).setDisplayFunction(data -> {
long targetId = data.getLong("targetId");
User target = event.getShardManager().getUserById(targetId);
return Action.fromData(data.get("action", Document.class)) + " to `" + (target == null ? targetId : target.getAsTag() + "`");
}).setIncreasedIndex(true);
paged.onSelect(select -> event.reply(ModLog.fromData(select.getSelected()).getEmbed(event.getShardManager())).queue());
paged.execute(event);
} else {
Document data = event.getMongo().getModLogById(Filters.and(Filters.eq("_id", id), Filters.eq("guildId", event.getGuild().getIdLong())), projection);
if (data == null) {
event.replyFailure("I could not find a mod log with that id").queue();
return;
}
event.reply(ModLog.fromData(data).getEmbed(event.getShardManager())).queue();
}
}
use of com.sx4.bot.entities.mod.Reason in project Sx4 by sx4-discord-bot.
the class TemporaryBanCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user") String userArgument, @Argument(value = "time", nullDefault = true) Duration time, @Argument(value = "reason", endless = true, nullDefault = true) Reason reason, @Option(value = "days", description = "Set how many days of messages should be deleted from the user") @DefaultNumber(1) @Limit(min = 0, max = 7) int days) {
SearchUtility.getUser(event.getShardManager(), userArgument).thenAccept(user -> {
if (user == null) {
event.replyFailure("I could not find that user").queue();
return;
}
if (user.getIdLong() == event.getSelfUser().getIdLong()) {
event.replyFailure("You cannot ban me, that is illegal").queue();
return;
}
Guild guild = event.getGuild();
Member member = guild.getMember(user);
if (member != null) {
if (!event.getMember().canInteract(member)) {
event.replyFailure("You cannot ban someone higher or equal than your top role").queue();
return;
}
if (!event.getSelfMember().canInteract(member)) {
event.replyFailure("I cannot ban someone higher or equal than my top role").queue();
return;
}
}
event.getGuild().retrieveBan(user).submit().whenComplete((ban, exception) -> {
if (exception instanceof ErrorResponseException && ((ErrorResponseException) exception).getErrorResponse() == ErrorResponse.UNKNOWN_BAN) {
Document data = event.getMongo().getGuildById(guild.getIdLong(), Projections.include("temporaryBan.defaultTime")).get("temporaryBan", MongoDatabase.EMPTY_DOCUMENT);
long duration = time == null ? data.get("defaultTime", ModUtility.DEFAULT_TEMPORARY_BAN_DURATION) : time.toSeconds();
List<Bson> update = List.of(Operators.set("unbanAt", Operators.add(Operators.nowEpochSecond(), duration)));
Bson filter = Filters.and(Filters.eq("userId", event.getMember().getIdLong()), Filters.eq("guildId", event.getGuild().getIdLong()));
event.getMongo().updateTemporaryBan(filter, update, new UpdateOptions().upsert(true)).whenComplete((result, resultException) -> {
if (ExceptionUtility.sendExceptionally(event, resultException)) {
return;
}
event.getGuild().ban(user, days).reason(ModUtility.getAuditReason(reason, event.getAuthor())).queue($ -> {
event.replySuccess("**" + user.getAsTag() + "** has been temporarily banned for " + TimeUtility.LONG_TIME_FORMATTER.parse(duration)).queue();
event.getBot().getModActionManager().onModAction(new TemporaryBanEvent(event.getMember(), user, reason, member != null, duration));
event.getBot().getTemporaryBanManager().putBan(event.getGuild().getIdLong(), user.getIdLong(), duration);
});
});
} else {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.replyFailure("That user is already banned").queue();
}
});
});
}
use of com.sx4.bot.entities.mod.Reason in project Sx4 by sx4-discord-bot.
the class BanCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user") String userArgument, @Argument(value = "reason", endless = true, nullDefault = true) Reason reason, @Option(value = "days", description = "Set how many days of messages should be deleted from the user") @DefaultNumber(1) @Limit(min = 0, max = 7) int days) {
SearchUtility.getUser(event.getShardManager(), userArgument).thenAccept(user -> {
if (user == null) {
event.replyFailure("I could not find that user").queue();
return;
}
if (user.getIdLong() == event.getSelfUser().getIdLong()) {
event.replyFailure("You cannot ban me, that is illegal").queue();
return;
}
Member member = event.getGuild().getMember(user);
if (member != null) {
if (!event.getMember().canInteract(member)) {
event.replyFailure("You cannot ban someone higher or equal than your top role").queue();
return;
}
if (!event.getSelfMember().canInteract(member)) {
event.replyFailure("I cannot ban someone higher or equal than my top role").queue();
return;
}
}
event.getGuild().retrieveBan(user).submit().whenComplete((ban, exception) -> {
if (exception instanceof ErrorResponseException && ((ErrorResponseException) exception).getErrorResponse() == ErrorResponse.UNKNOWN_BAN) {
event.getGuild().ban(user, days).reason(ModUtility.getAuditReason(reason, event.getAuthor())).queue($ -> {
event.replySuccess("**" + user.getAsTag() + "** has been banned").queue();
event.getBot().getModActionManager().onModAction(new BanEvent(event.getMember(), user, reason, member != null));
});
} else {
event.replyFailure("That user is already banned").queue();
}
});
});
}
use of com.sx4.bot.entities.mod.Reason in project Sx4 by sx4-discord-bot.
the class LoggerHandler method handleBulkMessages.
public void handleBulkMessages(TextChannel textChannel, List<String> messageIds, List<Document> loggers, LoggerEvent loggerEvent, User moderator) {
Guild guild = textChannel.getGuild();
for (Document logger : loggers) {
if ((logger.get("events", LoggerEvent.ALL) & loggerEvent.getRaw()) != loggerEvent.getRaw()) {
continue;
}
long channelId = logger.getLong("channelId");
TextChannel channel = guild.getTextChannelById(channelId);
if (channel == null) {
continue;
}
List<Document> entities = logger.getEmbedded(List.of("blacklist", "entities"), Collections.emptyList());
List<WebhookEmbed> embeds = new ArrayList<>();
for (String messageId : messageIds) {
WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setColor(this.bot.getConfig().getRed()).setTimestamp(Instant.now()).setFooter(new EmbedFooter("Message ID: " + messageId, null));
LoggerContext loggerContext = new LoggerContext().setChannel(textChannel);
if (moderator != null) {
loggerContext.setModerator(moderator);
}
String reason = moderator == null ? "in a bulk delete" : "by **" + moderator.getAsTag() + "**";
GuildMessage message = this.bot.getMessageCache().getMessageById(messageId);
if (message == null) {
if (!LoggerUtility.isWhitelisted(entities, loggerEvent, loggerContext)) {
continue;
}
embed.setDescription(String.format("A message sent in %s was deleted %s", textChannel.getAsMention(), reason));
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
} else {
User author = message.getAuthor();
loggerContext.setUser(author);
if (!LoggerUtility.isWhitelisted(entities, loggerEvent, loggerContext)) {
continue;
}
embed.setDescription(String.format("The message sent by `%s` in %s was deleted %s", author.getName(), textChannel.getAsMention(), reason));
embed.setAuthor(new EmbedAuthor(author.getAsTag(), author.getEffectiveAvatarUrl(), null));
String content = message.getContent();
if (!content.isBlank()) {
embed.addField(new EmbedField(false, "Message", StringUtility.limit(content, MessageEmbed.VALUE_MAX_LENGTH, "...")));
}
}
embeds.add(embed.build());
}
this.getManager(channelId).queue(channel, logger, embeds);
}
}
Aggregations