Search in sources :

Example 91 with Command

use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.

the class PickaxeCommand method shop.

@Command(value = "shop", description = "View all the pickaxes you are able to buy or craft")
@CommandId(360)
@Examples({ "pickaxe shop" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void shop(Sx4CommandEvent event) {
    List<Pickaxe> pickaxes = event.getBot().getEconomyManager().getItems(Pickaxe.class);
    PagedResult<Pickaxe> paged = new PagedResult<>(event.getBot(), pickaxes).setPerPage(12).setCustomFunction(page -> {
        EmbedBuilder embed = new EmbedBuilder().setAuthor("Pickaxe Shop", null, event.getSelfUser().getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setDescription("Pickaxes are a good way to gain some extra money aswell as some materials").setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
        page.forEach((pickaxe, index) -> {
            List<ItemStack<CraftItem>> items = pickaxe.getCraft();
            String craft = items.isEmpty() ? "None" : items.stream().map(ItemStack::toString).collect(Collectors.joining("\n"));
            embed.addField(pickaxe.getName(), String.format("Price: $%,d\nCraft: %s\nDurability: %,d", pickaxe.getPrice(), craft, pickaxe.getMaxDurability()), true);
        });
        return new MessageBuilder().setEmbeds(embed.build());
    });
    paged.execute(event);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Pickaxe(com.sx4.bot.entities.economy.item.Pickaxe) ItemStack(com.sx4.bot.entities.economy.item.ItemStack) PagedResult(com.sx4.bot.paged.PagedResult) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 92 with Command

use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.

the class VoteCommand method onCommand.

public void onCommand(Sx4CommandEvent event) {
    Request request = new Request.Builder().url(event.getConfig().getVoteWebserverUrl("440996323156819968/votes/user/" + event.getAuthor().getId() + "/unused/use")).addHeader("Authorization", event.getConfig().getVoteApi(true)).build();
    event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
        Document data = Document.parse(response.body().string());
        if (!data.getBoolean("success")) {
            Request latest = new Request.Builder().url(event.getConfig().getVoteWebserverUrl("440996323156819968/votes/user/" + event.getAuthor().getId() + "/latest")).addHeader("Authorization", event.getConfig().getVoteApi(true)).build();
            event.getHttpClient().newCall(latest).enqueue((HttpCallback) latestResponse -> {
                Document latestVote = Document.parse(latestResponse.body().string());
                OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
                boolean weekend = now.getDayOfWeek() == DayOfWeek.FRIDAY || now.getDayOfWeek() == DayOfWeek.SATURDAY || now.getDayOfWeek() == DayOfWeek.SUNDAY;
                EmbedBuilder embed = new EmbedBuilder().setAuthor("Vote Bonus", null, event.getAuthor().getEffectiveAvatarUrl());
                long timeRemaining = 0;
                Document vote = latestVote.get("vote", Document.class);
                if (vote != null && latestVote.getBoolean("success")) {
                    timeRemaining = vote.get("time", Number.class).longValue() - Clock.systemUTC().instant().getEpochSecond() + VoteCommand.COOLDOWN;
                }
                if (timeRemaining > 0) {
                    embed.addField("Sx4", "**[You have voted recently you can vote for the bot again in " + TimeUtility.LONG_TIME_FORMATTER.parse(timeRemaining) + "](https://top.gg/bot/440996323156819968/vote)**", false);
                } else {
                    embed.addField("Sx4", "**[You can vote for Sx4 for an extra $" + (weekend ? 1600 : 800) + "](https://top.gg/bot/440996323156819968/vote)**", false);
                }
                event.reply(embed.build()).queue();
            });
            return;
        }
        List<Document> votes = data.getList("votes", Document.class, Collections.emptyList());
        Map<User, Long> referrers = new HashMap<>();
        long money = 0L;
        for (Document vote : votes) {
            boolean weekend = vote.getBoolean("weekend");
            money += weekend ? 1600 : 800;
            Document query = vote.get("query", Document.class);
            if (query == null) {
                continue;
            }
            Object referral = query.get("referral");
            if (referral == null) {
                continue;
            }
            String id = referral instanceof List ? (String) ((List<?>) referral).get(0) : (String) referral;
            User user;
            try {
                user = event.getShardManager().getUserById(id);
            } catch (NumberFormatException e) {
                continue;
            }
            if (user == null || user.getIdLong() == event.getAuthor().getIdLong() || user.isBot()) {
                continue;
            }
            long amount = weekend ? 500 : 250;
            referrers.compute(user, (key, value) -> value == null ? amount : value + amount);
        }
        List<WriteModel<Document>> bulkData = new ArrayList<>();
        UpdateOptions options = new UpdateOptions().upsert(true);
        StringJoiner content = new StringJoiner(", ");
        referrers.forEach((user, amount) -> {
            content.add(String.format("%s (**$%,d**)", user.getAsTag(), amount));
            bulkData.add(new UpdateOneModel<>(Filters.eq("_id", user.getIdLong()), Updates.inc("economy.balance", amount), options));
        });
        bulkData.add(new UpdateOneModel<>(Filters.eq("_id", event.getAuthor().getIdLong()), Updates.inc("economy.balance", money), options));
        String message = String.format("You have voted for the bot **%,d** time%s since you last used the command gathering you a total of **$%,d**, Vote for the bots again in 12 hours for more money.%s", votes.size(), votes.size() == 1 ? "" : "s", money, content.length() == 0 ? "" : "Referred users: " + content);
        event.getMongo().bulkWriteUsers(bulkData).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            event.reply(message).queue();
        });
    });
}
Also used : Document(org.bson.Document) Request(okhttp3.Request) java.util(java.util) Command(com.jockie.bot.core.command.Command) HttpCallback(com.sx4.bot.http.HttpCallback) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) CommandId(com.sx4.bot.annotations.command.CommandId) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) User(net.dv8tion.jda.api.entities.User) Redirects(com.sx4.bot.annotations.command.Redirects) ModuleCategory(com.sx4.bot.category.ModuleCategory) Examples(com.sx4.bot.annotations.command.Examples) OffsetDateTime(java.time.OffsetDateTime) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) DayOfWeek(java.time.DayOfWeek) Clock(java.time.Clock) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) com.mongodb.client.model(com.mongodb.client.model) ZoneOffset(java.time.ZoneOffset) Argument(com.jockie.bot.core.argument.Argument) User(net.dv8tion.jda.api.entities.User) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) Document(org.bson.Document) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) OffsetDateTime(java.time.OffsetDateTime)

Example 93 with Command

use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.

the class ReputationCommand method amount.

@Command(value = "amount", description = "View the amount of reputation a user has")
@CommandId(420)
@Examples({ "reputation amount", "reputation amount @Shea#6653", "reputation amount Shea" })
public void amount(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
    User user = member == null ? event.getAuthor() : member.getUser();
    int amount = event.getMongo().getUserById(user.getIdLong(), Projections.include("reputation.amount")).getEmbedded(List.of("reputation", "amount"), 0);
    event.replyFormat("%s has **%,d** reputation", user.getAsTag(), amount).queue();
}
Also used : User(net.dv8tion.jda.api.entities.User) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 94 with Command

use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.

the class ReputationCommand method leaderboard.

@Command(value = "leaderboard", aliases = { "lb" }, description = "View the leaderboard for reputation across the bot")
@CommandId(421)
@Redirects({ "leaderboard reputation", "lb rep", "lb reputation", "leaderboard rep" })
@Examples({ "reputation leaderboard", "reputation leaderboard --server" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void leaderboard(Sx4CommandEvent event, @Option(value = "server", aliases = { "guild" }, description = "View the leaderboard with a server filter") boolean guild) {
    List<Bson> pipeline = List.of(Aggregates.project(Projections.computed("reputation", "$reputation.amount")), Aggregates.match(Filters.exists("reputation")), Aggregates.sort(Sorts.descending("reputation")));
    event.getMongo().aggregateUsers(pipeline).whenComplete((documents, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        List<Map.Entry<User, Integer>> users = new ArrayList<>();
        AtomicInteger userIndex = new AtomicInteger(-1);
        int i = 0;
        for (Document data : documents) {
            User user = event.getShardManager().getUserById(data.getLong("_id"));
            if (user == null) {
                continue;
            }
            if (!event.getGuild().isMember(user) && guild) {
                continue;
            }
            i++;
            users.add(Map.entry(user, data.getInteger("reputation")));
            if (user.getIdLong() == event.getAuthor().getIdLong()) {
                userIndex.set(i);
            }
        }
        if (users.isEmpty()) {
            event.replyFailure("There are no users which fit into this leaderboard").queue();
            return;
        }
        PagedResult<Map.Entry<User, Integer>> paged = new PagedResult<>(event.getBot(), users).setPerPage(10).setCustomFunction(page -> {
            int rank = userIndex.get();
            EmbedBuilder embed = new EmbedBuilder().setTitle("Reputation Leaderboard").setFooter(event.getAuthor().getName() + "'s Rank: " + (rank == -1 ? "N/A" : NumberUtility.getSuffixed(rank)) + " | Page " + page.getPage() + "/" + page.getMaxPage(), event.getAuthor().getEffectiveAvatarUrl());
            page.forEach((entry, index) -> embed.appendDescription(String.format("%d. `%s` - %,d reputation\n", index + 1, MarkdownSanitizer.escape(entry.getKey().getAsTag()), entry.getValue())));
            return new MessageBuilder().setEmbeds(embed.build());
        });
        paged.execute(event);
    });
}
Also used : User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Document(org.bson.Document) Bson(org.bson.conversions.Bson) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PagedResult(com.sx4.bot.paged.PagedResult) Redirects(com.sx4.bot.annotations.command.Redirects) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 95 with Command

use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.

the class GameCommand method list.

@Command(value = "list", aliases = { "game list", "game list @Shea#6653", "game list Shea" }, description = "Lists basic info on all games a user has played on Sx4")
@CommandId(297)
@Redirects({ "games" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void list(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
    User user = member == null ? event.getAuthor() : member.getUser();
    List<Document> games = event.getMongo().getGames(Filters.eq("userId", user.getIdLong()), Projections.include("type", "state")).into(new ArrayList<>());
    if (games.isEmpty()) {
        event.replyFailure("That user has not played any games yet").queue();
        return;
    }
    PagedResult<Document> paged = new PagedResult<>(event.getBot(), games).setAuthor("Game List", null, user.getEffectiveAvatarUrl()).setIndexed(false).setPerPage(15).setSelect().setDisplayFunction(game -> "`" + GameType.fromId(game.getInteger("type")).getName() + "` - " + StringUtility.title(GameState.fromId(game.getInteger("state")).name()));
    paged.execute(event);
}
Also used : User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document) Redirects(com.sx4.bot.annotations.command.Redirects) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId)

Aggregations

Command (com.jockie.bot.core.command.Command)178 Sx4Command (com.sx4.bot.core.Sx4Command)178 CommandId (com.sx4.bot.annotations.command.CommandId)118 Examples (com.sx4.bot.annotations.command.Examples)116 Document (org.bson.Document)113 Bson (org.bson.conversions.Bson)97 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)82 PagedResult (com.sx4.bot.paged.PagedResult)71 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)59 Argument (com.jockie.bot.core.argument.Argument)53 ModuleCategory (com.sx4.bot.category.ModuleCategory)53 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)53 Permission (net.dv8tion.jda.api.Permission)52 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)46 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)43 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)43 User (net.dv8tion.jda.api.entities.User)43 Operators (com.sx4.bot.database.mongo.model.Operators)41 Collectors (java.util.stream.Collectors)36 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)31