Search in sources :

Example 16 with BotException

use of tk.ardentbot.core.misc.logging.BotException in project Ardent by adamint.

the class Blackjack method showResults.

public void showResults(int bet, Hand yourHand, Hand dealerHand, Guild guild, MessageChannel channel, User user, Message message, String[] args) {
    try {
        EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
        builder.setAuthor("Blackjack | Game over", user.getEffectiveAvatarUrl(), user.getEffectiveAvatarUrl());
        while (dealerHand.total() < 17) dealerHand.generate();
        if (yourHand.total() > 21) {
            builder.setDescription("You busted and lost {0} :frowning:".replace("{0}", RPGUtils.formatMoney(bet)));
            Profile.get(user).removeMoney(bet);
        } else if (dealerHand.total() > 21) {
            builder.setDescription("I busted! You won {0}".replace("{0}", RPGUtils.formatMoney(bet)));
            Profile.get(user).addMoney(bet);
        } else if (dealerHand.total() == yourHand.total())
            builder.setDescription("We tied! You don't win or lose anything >.>");
        else if (dealerHand.total() < yourHand.total()) {
            builder.setDescription("You had a higher numbered hand than me! You win {0}".replace("{0}", RPGUtils.formatMoney(bet)));
            Profile.get(user).addMoney(bet);
        } else {
            builder.setDescription("I had a higher numbered hand! You lose {0}".replace("{0}", RPGUtils.formatMoney(bet)));
            Profile.get(user).removeMoney(bet);
        }
        builder.addField("Your hand", yourHand.readable(), true);
        builder.addField("My hand", dealerHand.readable(), true);
        sendEmbed(builder, channel, user);
        sessions.remove(user.getId());
    } catch (Exception e) {
        new BotException(e);
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) BotException(tk.ardentbot.core.misc.logging.BotException) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 17 with BotException

use of tk.ardentbot.core.misc.logging.BotException in project Ardent by adamint.

the class Trivia method dispatchRound.

public static void dispatchRound(Guild guild, TextChannel channel, User creator, TriviaGame currentGame, ScheduledExecutorService ex) {
    try {
        Shard shard = GuildUtils.getShard(guild);
        currentGame.incrementRounds();
        if (currentGame.getRound() >= currentGame.getTotalRounds()) {
            currentGame.finish(shard, shard.help);
            return;
        }
        if (!gamesInSession.contains(currentGame))
            return;
        channel.sendMessage(currentGame.displayScores(shard, shard.help).build()).queue();
        TriviaQuestion triviaQuestion = triviaQuestions.get(new SecureRandom().nextInt(triviaQuestions.size()));
        currentGame.setCurrentTriviaQuestion(triviaQuestion);
        StringBuilder question = new StringBuilder();
        question.append("**" + triviaQuestion.getCategory() + "**: " + triviaQuestion.getQuestion());
        shard.help.sendTranslatedMessage(question.toString(), channel, creator);
        int currentRound = currentGame.getRound();
        ex.schedule(() -> {
            if (!currentGame.isAnsweredCurrentQuestion() && currentRound == currentGame.getRound() && !(currentRound == currentGame.getTotalRounds())) {
                try {
                    shard.help.sendEditedTranslation("No one got it right! The correct answer was {0}", creator, channel, triviaQuestion.getAnswers().get(0));
                    if (currentGame.getRound() + 1 < currentGame.getTotalRounds())
                        dispatchRound(guild, channel, creator, currentGame, ex);
                    else
                        currentGame.finish(shard, shard.help);
                } catch (Exception e) {
                    new BotException(e);
                }
            }
        }, 17, TimeUnit.SECONDS);
    } catch (Exception ignored) {
    }
}
Also used : BotException(tk.ardentbot.core.misc.logging.BotException) TriviaQuestion(tk.ardentbot.utils.models.TriviaQuestion) SecureRandom(java.security.SecureRandom) Shard(tk.ardentbot.main.Shard) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 18 with BotException

use of tk.ardentbot.core.misc.logging.BotException in project Ardent by adamint.

the class Botname method noArgs.

@Override
public void noArgs(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
    if (args.length == 1) {
        sendTranslatedMessage("Enter what you want me to nickname myself", channel, user);
        interactiveOperation(channel, message, howLongMessage -> {
            String howLong = howLongMessage.getRawContent();
            try {
                guild.getController().setNickname(guild.getSelfMember(), howLong).queue(aVoid -> {
                    sendTranslatedMessage("Changed my nickname!", channel, user);
                });
            } catch (Exception e) {
                new BotException(e);
            }
        });
    } else {
        String name = message.getContent().replace(GuildUtils.getPrefix(guild) + args[0] + " ", "");
        if (name.equalsIgnoreCase("reset")) {
            name = "";
        }
        try {
            guild.getController().setNickname(guild.getSelfMember(), name).queue(aVoid -> {
                try {
                    sendTranslatedMessage("Changed my nickname!", channel, user);
                } catch (Exception e) {
                    new BotException(e);
                }
            });
        } catch (PermissionException ex) {
            sendTranslatedMessage("I couldn't change my nickname, make sure I have permission to do that", channel, user);
        }
    }
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) BotException(tk.ardentbot.core.misc.logging.BotException) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 19 with BotException

use of tk.ardentbot.core.misc.logging.BotException in project Ardent by adamint.

the class Admin method noArgs.

@Override
public void noArgs(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
    if (Ardent.developers.contains(user.getId())) {
        if (args.length > 1) {
            if (args[1].equalsIgnoreCase("update")) {
                update(this, channel);
            } else if (args[1].equalsIgnoreCase("softupdate")) {
                ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
                ex.scheduleAtFixedRate(() -> {
                    long connections = InternalStats.collect().getMusicPlayers();
                    if (connections <= 1 || (secondsWaitedForRestart >= (60 * 60 * 3))) {
                        if (connections <= 3) {
                            try {
                                update(Admin.this, channel);
                            } catch (Exception e) {
                                new BotException(e);
                            }
                        }
                    }
                    secondsWaitedForRestart += 5;
                }, 5, 5, TimeUnit.SECONDS);
            } else if (args[1].equalsIgnoreCase("getloc")) {
                sendTranslatedMessage(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(), channel, user);
            } else if (args[1].equalsIgnoreCase("usage")) {
                Map<Guild, Integer> guildsByUsage = UsageUtils.sortedGuildsByCommandUsage(10);
                StringBuilder sb = new StringBuilder();
                guildsByUsage.forEach((key, value) -> {
                    sb.append(key.getName() + " (" + key.getId() + ") : " + value + "\n");
                });
                sendTranslatedMessage(sb.toString(), channel, user);
            } else if (args[1].equalsIgnoreCase("announce")) {
                String msg = message.getRawContent().replace(GuildUtils.getPrefix(guild) + args[0] + " " + args[1] + " ", "");
                if (Ardent.announcement != null)
                    Ardent.sentAnnouncement.clear();
                Ardent.announcement = "** == Important Announcement ==**\n" + msg;
                for (Shard shard : getShards()) {
                    shard.jda.getGuilds().forEach(g -> {
                        Ardent.sentAnnouncement.put(g.getId(), false);
                    });
                }
            } else if (args[1].equalsIgnoreCase("addpatron")) {
                List<User> mentioned = message.getMentionedUsers();
                if (mentioned.size() == 1) {
                    User u = mentioned.get(0);
                    String[] uArgs = message.getRawContent().split(" ");
                    if (uArgs.length == 4) {
                        String role = uArgs[3];
                        if (role.equalsIgnoreCase("tier1") || role.equalsIgnoreCase("tier2") || role.equalsIgnoreCase("tier3")) {
                            r.table("patrons").insert(r.hashMap("user_id", u.getId()).with("tier", role)).run(connection);
                            channel.sendMessage("done").queue();
                        }
                    }
                }
            } else if (args[1].equalsIgnoreCase("addstaff")) {
                List<User> mentioned = message.getMentionedUsers();
                User u = mentioned.get(0);
                String roleToAdd = args[3];
                r.table("staff").insert(r.json(gson.toJson(new Staff(u.getId(), roleToAdd)))).run(connection);
                channel.sendMessage("done").queue();
            } else if (args[1].equalsIgnoreCase("stop")) {
                for (Shard shard : getShards()) {
                    shard.jda.shutdown(true);
                }
                System.exit(0);
            } else if (args[1].equalsIgnoreCase("restart")) {
                for (Shard shard : getShards()) {
                    shard.jda.shutdown(true);
                }
                channel.sendMessage("This subcommand is untested and could fail.").queue();
                Runtime.getRuntime().exec("java -jar " + getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
                System.exit(0);
            } else if (args[1].equalsIgnoreCase("setgameurl")) {
                Ardent.gameUrl = args[2];
                sendTranslatedMessage("Updated the game URL", channel, user);
            } else if (args[1].equalsIgnoreCase("addmoney")) {
                String[] raw = message.getRawContent().split(" ");
                if (raw.length == 4) {
                    try {
                        User mentioned1 = message.getMentionedUsers().get(0);
                        double amount = Double.parseDouble(raw[3]);
                        Profile.get(mentioned1).addMoney(amount);
                        sendTranslatedMessage("added " + amount + " to " + mentioned1.getName(), channel, user);
                    } catch (Exception e) {
                        sendTranslatedMessage("bad", channel, user);
                    }
                } else
                    sendTranslatedMessage("bad", channel, user);
            } else if (args[1].equalsIgnoreCase("disable")) {
                if (args.length == 2) {
                    StringBuilder disabledCommands = new StringBuilder();
                    disabledCommands.append("**Disabled Commands/Features:**\n");
                    Ardent.disabledCommands.forEach(s -> {
                        disabledCommands.append(" **>** ").append(s).append("\n");
                    });
                    sendTranslatedMessage(disabledCommands.toString(), channel, user);
                } else if (args.length == 4) {
                    try {
                        boolean disable = Boolean.parseBoolean(args[2]);
                        System.out.println(disable);
                        String identifier = args[3];
                        if (disable) {
                            if (Ardent.disabledCommands.contains(identifier))
                                sendTranslatedMessage("This feature is already disabled", channel, user);
                            else {
                                Ardent.disabledCommands.add(identifier);
                                sendTranslatedMessage("disabled " + identifier, channel, user);
                            }
                        } else {
                            if (!Ardent.disabledCommands.contains(identifier)) {
                                sendTranslatedMessage("This feature is already enabled", channel, user);
                            } else {
                                Ardent.disabledCommands.remove(identifier);
                                sendTranslatedMessage("enabled " + identifier, channel, user);
                            }
                        }
                    } catch (Exception e) {
                        sendTranslatedMessage("/admin restrict true/false command_identifier", channel, user);
                    }
                } else
                    sendTranslatedMessage("/admin restrict true/false command_identifier", channel, user);
            }
        }
    }
}
Also used : GuildMusicManager(tk.ardentbot.commands.music.GuildMusicManager) Command(tk.ardentbot.core.executor.Command) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) Staff(tk.ardentbot.rethink.models.Staff) Database.r(tk.ardentbot.rethink.Database.r) Profile(tk.ardentbot.utils.rpg.profiles.Profile) BotException(tk.ardentbot.core.misc.logging.BotException) Shard(tk.ardentbot.main.Shard) Executors(java.util.concurrent.Executors) UsageUtils(tk.ardentbot.utils.discord.UsageUtils) TimeUnit(java.util.concurrent.TimeUnit) GuildUtils(tk.ardentbot.utils.discord.GuildUtils) List(java.util.List) ShardManager.getShards(tk.ardentbot.main.ShardManager.getShards) Music(tk.ardentbot.commands.music.Music) Database.connection(tk.ardentbot.rethink.Database.connection) Map(java.util.Map) InternalStats(tk.ardentbot.utils.discord.InternalStats) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Ardent(tk.ardentbot.main.Ardent) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Staff(tk.ardentbot.rethink.models.Staff) BotException(tk.ardentbot.core.misc.logging.BotException) BotException(tk.ardentbot.core.misc.logging.BotException) List(java.util.List) Shard(tk.ardentbot.main.Shard) Map(java.util.Map)

Example 20 with BotException

use of tk.ardentbot.core.misc.logging.BotException in project Ardent by adamint.

the class SoftBan method noArgs.

@Override
public void noArgs(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
    Member userMember = guild.getMember(user);
    if (userMember.hasPermission(Permission.BAN_MEMBERS)) {
        List<User> mentioned = message.getMentionedUsers();
        try {
            User u = mentioned.get(0);
            String userString = u.toString();
            String uToSend = UserUtils.getNameWithDiscriminator(userString);
            try {
                guild.getController().ban(u, 7).queue();
                guild.getController().unban(u).queue();
                sendTranslatedMessage("Soft-banned **" + uToSend + "**", channel, user);
            } catch (PermissionException ex) {
                sendTranslatedMessage("I don't have permissions to ban!", channel, user);
            }
        } catch (Exception e) {
            new BotException(e);
        }
    } else {
        sendTranslatedMessage("You don't have the: `Ban Members` permission", channel, user);
    }
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) BotException(tk.ardentbot.core.misc.logging.BotException) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) BotException(tk.ardentbot.core.misc.logging.BotException)

Aggregations

BotException (tk.ardentbot.core.misc.logging.BotException)24 Shard (tk.ardentbot.main.Shard)10 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)8 List (java.util.List)6 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Command (tk.ardentbot.core.executor.Command)5 Database.connection (tk.ardentbot.rethink.Database.connection)5 Database.r (tk.ardentbot.rethink.Database.r)5 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)4 Permission (net.dv8tion.jda.core.Permission)4 Subcommand (tk.ardentbot.core.executor.Subcommand)4 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)3 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)3 Guild (net.dv8tion.jda.core.entities.Guild)3 GuildUtils (tk.ardentbot.utils.discord.GuildUtils)3 UserUtils (tk.ardentbot.utils.discord.UserUtils)3 Cursor (com.rethinkdb.net.Cursor)2 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2