use of club.minnced.discord.webhook.send.WebhookEmbedBuilder in project Sx4 by sx4-discord-bot.
the class ServerLogHandler method onGuildLeave.
public void onGuildLeave(GuildLeaveEvent event) {
ShardManager manager = event.getJDA().getShardManager();
Member owner = event.getGuild().getOwner();
WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setColor(this.bot.getConfig().getRed()).setThumbnailUrl(event.getGuild().getIconUrl()).setTimestamp(Instant.now()).setDescription(String.format("I am now in %,d servers and connected to %,d users", manager.getGuildCache().size(), manager.getUserCache().size())).setAuthor(new WebhookEmbed.EmbedAuthor("Left Server!", event.getJDA().getSelfUser().getEffectiveAvatarUrl(), null)).addField(new WebhookEmbed.EmbedField(true, "Server Name", event.getGuild().getName())).addField(new WebhookEmbed.EmbedField(true, "Server ID", event.getGuild().getId())).addField(new WebhookEmbed.EmbedField(true, "Server Owner", (owner == null ? "Anonymous#0000" : owner.getUser().getAsTag()) + "\n" + event.getGuild().getOwnerId())).addField(new WebhookEmbed.EmbedField(true, "Server Members", String.format("%,d member%s", event.getGuild().getMemberCount(), event.getGuild().getMemberCount() == 1 ? "" : "s"))).addField(new WebhookEmbed.EmbedField(false, "Stayed for", TimeUtility.LONG_TIME_FORMATTER.parse(Duration.between(event.getGuild().getSelfMember().getTimeJoined(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds())));
this.webhook.send(embed.build());
}
use of club.minnced.discord.webhook.send.WebhookEmbedBuilder in project Sx4 by sx4-discord-bot.
the class StarboardHandler method getStarboardMessage.
private WebhookMessage getStarboardMessage(Document guildData, Document starboard, Guild guild, Member member, ReactionEmote emote, boolean premium) {
List<Document> messages = guildData.getList("messages", Document.class, StarboardManager.DEFAULT_CONFIGURATION);
int stars = starboard.getInteger("count");
Document messageData = messages.stream().filter(d -> starboard.getInteger("count") >= d.getInteger("stars")).max(Comparator.comparingInt(d -> d.getInteger("stars"))).orElse(null);
if (messageData == null) {
return null;
}
int nextStars = messages.stream().filter(d -> starboard.getInteger("count") < d.getInteger("stars")).mapToInt(d -> d.getInteger("stars")).min().orElse(0);
User author = this.bot.getShardManager().getUserById(starboard.getLong("authorId"));
long channelId = starboard.getLong("channelId");
TextChannel channel = guild.getTextChannelById(channelId);
if (channel == null) {
return null;
}
String messageLink = "https://discord.com/channels/" + guild.getId() + "/" + channelId + "/" + starboard.getLong("originalMessageId");
// temporary while embed support isn't a thing
WebhookEmbedBuilder builder = new WebhookEmbedBuilder().setAuthor(new WebhookEmbed.EmbedAuthor(author == null ? "Anonymous#0000" : author.getAsTag(), author == null ? null : author.getEffectiveAvatarUrl(), null)).setColor(-21453).addField(new WebhookEmbed.EmbedField(false, "Message Link", "[Jump!](" + messageLink + ")")).setImageUrl(starboard.getString("image"));
String content = starboard.getString("content");
if (content != null && !content.isBlank()) {
builder.addField(new WebhookEmbed.EmbedField(false, "Message", StringUtility.limit(content, MessageEmbed.VALUE_MAX_LENGTH, "[...](" + messageLink + ")")));
}
Document webhookData = guildData.get("webhook", MongoDatabase.EMPTY_DOCUMENT);
try {
return this.format(messageData.get("message", Document.class), member, channel, emote, stars, nextStars, starboard.getObjectId("_id")).setUsername(premium ? webhookData.get("name", "Sx4 - Starboard") : "Sx4 - Starboard").setAvatarUrl(premium ? webhookData.get("avatar", this.bot.getShardManager().getShardById(0).getSelfUser().getEffectiveAvatarUrl()) : this.bot.getShardManager().getShardById(0).getSelfUser().getEffectiveAvatarUrl()).addEmbeds(builder.build()).build();
} catch (IllegalArgumentException e) {
// TODO: can't currently happen but when embed support is added handle this
return null;
}
}
use of club.minnced.discord.webhook.send.WebhookEmbedBuilder in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildVoiceGuildMute.
public void onGuildVoiceGuildMute(GuildVoiceGuildMuteEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = member.getUser();
GuildVoiceState voiceState = event.getVoiceState();
VoiceChannel channel = voiceState.getChannel();
boolean muted = voiceState.isGuildMuted();
LoggerEvent loggerEvent = muted ? LoggerEvent.MEMBER_SERVER_VOICE_MUTE : LoggerEvent.MEMBER_SERVER_VOICE_UNMUTE;
LoggerContext loggerContext = new LoggerContext().setUser(user).setChannel(channel == null ? 0L : channel.getIdLong());
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` has been %s", member.getEffectiveName(), muted ? "muted" : "unmuted"));
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(member.getUser().getAsTag(), member.getUser().getEffectiveAvatarUrl(), null));
embed.setFooter(new EmbedFooter(String.format("User ID: %s", user.getId()), null));
embed.setColor(muted ? this.bot.getConfig().getRed() : this.bot.getConfig().getGreen());
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.MEMBER_UPDATE).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() == member.getUser().getIdLong()).filter(e -> e.getChangeByKey(AuditLogKey.MEMBER_MUTE) != null).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("`%s` has been %s by **%s**", member.getEffectiveName(), muted ? "muted" : "unmuted", moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
Aggregations