use of com.sx4.bot.core.Sx4 in project Sx4 by sx4-discord-bot.
the class PremiumCommand method credit.
@Command(value = "credit", description = "Checks your current credit")
@CommandId(179)
@Examples({ "premium credit" })
public void credit(Sx4CommandEvent event) {
Document premium = event.getMongoMain().getUserById(event.getAuthor().getIdLong(), Projections.include("premium.credit", "premium.endAt")).get("premium", MongoDatabase.EMPTY_DOCUMENT);
int credit = premium.getInteger("credit", 0);
long endAt = premium.get("endAt", -1L);
OffsetDateTime expire = OffsetDateTime.ofInstant(Instant.ofEpochSecond(endAt), ZoneOffset.UTC);
String format = String.format(expire.format(this.formatter), NumberUtility.getSuffix(expire.getDayOfMonth()));
event.replyFormat("Your current credit is **$%,.2f**%s", credit / 100D, endAt == -1 ? "" : "\n\nYour personal premium " + (expire.isBefore(OffsetDateTime.now(ZoneOffset.UTC)) ? "expired on the **" + format + "**, you can renew it here <https://patreon.com/Sx4>" : "will expire on the **" + format + "**")).queue();
}
use of com.sx4.bot.core.Sx4 in project Sx4 by sx4-discord-bot.
the class PremiumCommand method add.
@Command(value = "add", description = "Make a server premium")
@CommandId(177)
@Examples({ "premium add", "premium add 31", "premium add 20 Sx4 | Support Server" })
@Premium
public void add(Sx4CommandEvent event, @Argument(value = "days") @DefaultNumber(31) @Limit(min = 1, max = 365) int days, @Argument(value = "server", endless = true, nullDefault = true) Guild guild) {
if (guild == null) {
guild = event.getGuild();
}
int monthPrice = event.getConfig().getPremiumPrice();
int price = (int) Math.round((monthPrice / (double) event.getConfig().getPremiumDays()) * days);
long endAtPrior = event.getMongo().getGuildById(guild.getIdLong(), Projections.include("premium.endAt")).getEmbedded(List.of("premium", "endAt"), 0L);
boolean hasPremium = endAtPrior != 0;
MessageEmbed embed = new EmbedBuilder().setColor(event.getConfig().getOrange()).setAuthor("Premium", null, event.getAuthor().getEffectiveAvatarUrl()).setDescription(String.format("Buying %d day%s of premium will:\n\n• Make you unable to use this credit on the other version of the bot\n• Use **$%.2f** of your credit\n• %s %1$s day%2$s of premium to the server\n\n:warning: **This action cannot be reversed** :warning:", days, days == 1 ? "" : "s", price / 100D, hasPremium ? "Add an extra" : "Give")).build();
String acceptId = new CustomButtonId.Builder().setType(ButtonType.PREMIUM_CONFIRM).setTimeout(60).setOwners(event.getAuthor().getIdLong()).setArguments(guild.getIdLong(), days).getId();
String rejectId = new CustomButtonId.Builder().setType(ButtonType.GENERIC_REJECT).setTimeout(60).setOwners(event.getAuthor().getIdLong()).getId();
List<Button> buttons = List.of(Button.success(acceptId, "Confirm"), Button.danger(rejectId, "Cancel"));
event.reply(embed).setActionRow(buttons).queue();
}
use of com.sx4.bot.core.Sx4 in project Sx4 by sx4-discord-bot.
the class ServerLogHandler method onGuildJoin.
public void onGuildJoin(GuildJoinEvent event) {
ShardManager manager = event.getJDA().getShardManager();
Member owner = event.getGuild().getOwner();
WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setColor(this.bot.getConfig().getGreen()).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("Joined 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")));
this.webhook.send(embed.build());
if (event.getGuild().getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
event.getGuild().retrieveAuditLogs().type(ActionType.BOT_ADD).queueAfter(500, TimeUnit.MILLISECONDS, audits -> {
AuditLogEntry audit = audits.stream().filter(auditLog -> auditLog.getTargetIdLong() == event.getJDA().getSelfUser().getIdLong()).filter(auditLog -> Duration.between(auditLog.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).findFirst().orElse(null);
if (audit != null) {
audit.getUser().openPrivateChannel().flatMap(channel -> channel.sendMessage("Thanks for adding me to your server!\nThe default prefix is `s?` however if that doesn't work use `@Sx4#1617 prefix` to check your current prefixes\nAll my info and commands can be found in `s?help`\nIf you need any help feel free to join the support server: https://discord.gg/PqJNcfB")).queue();
}
});
}
}
use of com.sx4.bot.core.Sx4 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");
BaseGuildMessageChannel channel = guild.getChannelById(BaseGuildMessageChannel.class, 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 com.sx4.bot.core.Sx4 in project Sx4 by sx4-discord-bot.
the class WelcomerHandler method sendWelcomer.
public void sendWelcomer(Guild guild, Member member) {
JDA jda = guild.getJDA();
Document data = this.bot.getMongo().getGuildById(guild.getIdLong(), Projections.include("welcomer", "premium.endAt"));
Document welcomer = data.get("welcomer", MongoDatabase.EMPTY_DOCUMENT);
Document image = welcomer.get("image", MongoDatabase.EMPTY_DOCUMENT);
if (member.isPending() == welcomer.get("screening", true)) {
return;
}
boolean messageEnabled = welcomer.getBoolean("enabled", false), imageEnabled = image.getBoolean("enabled", false);
if (!messageEnabled && !imageEnabled) {
return;
}
long channelId = welcomer.get("channelId", 0L);
BaseGuildMessageChannel channel = channelId == 0L ? null : guild.getChannelById(BaseGuildMessageChannel.class, channelId);
boolean dm = welcomer.getBoolean("dm", false);
if (channel == null && !dm) {
return;
}
Document webhookData = welcomer.get("webhook", MongoDatabase.EMPTY_DOCUMENT);
boolean premium = Clock.systemUTC().instant().getEpochSecond() < data.getEmbedded(List.of("premium", "endAt"), 0L);
WelcomerUtility.getWelcomerMessage(this.bot.getHttpClient(), messageEnabled ? welcomer.get("message", WelcomerManager.DEFAULT_MESSAGE) : null, image.getString("bannerId"), member, this.bot.getConfig().isCanary(), imageEnabled, premium, (builder, exception) -> {
if (exception instanceof IllegalArgumentException) {
this.bot.getMongo().updateGuildById(guild.getIdLong(), Updates.unset("welcomer.message")).whenComplete(MongoDatabase.exceptionally());
return;
}
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (dm) {
member.getUser().openPrivateChannel().flatMap(privateChannel -> MessageUtility.fromWebhookMessage(privateChannel, builder.build())).queue(null, ErrorResponseException.ignore(ErrorResponse.CANNOT_SEND_TO_USER));
} else {
WebhookMessage message = builder.setUsername(premium ? webhookData.get("name", "Sx4 - Welcomer") : "Sx4 - Welcomer").setAvatarUrl(premium ? webhookData.get("avatar", jda.getSelfUser().getEffectiveAvatarUrl()) : jda.getSelfUser().getEffectiveAvatarUrl()).build();
this.bot.getWelcomerManager().sendWelcomer(channel, webhookData, message);
}
});
}
Aggregations