Search in sources :

Example 11 with BotException

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

the class AsyncCommandExecutor method run.

/**
     * Send the announcement if one exists, else run
     * the command
     */
@Override
public void run() {
    try {
        Shard shard = GuildUtils.getShard(guild);
        if (Ardent.announcement != null) {
            if (!Ardent.sentAnnouncement.get(guild.getId())) {
                Ardent.sentAnnouncement.replace(guild.getId(), true);
                shard.executorService.schedule(() -> {
                    try {
                        command.sendTranslatedMessage(Ardent.announcement, channel, user);
                    } catch (Exception e) {
                        new BotException(e);
                    }
                }, 5, TimeUnit.SECONDS);
            }
        }
        command.getBotCommand().onUsage(guild, channel, author, message, args);
        shard.factory.addCommandUsage(command.getName());
    } catch (Throwable e) {
        new BotException(e);
    }
}
Also used : BotException(tk.ardentbot.core.misc.logging.BotException) Shard(tk.ardentbot.main.Shard) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 12 with BotException

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

the class SparkServer method setup.

/**
     * Sets up the web server and the endpoints
     */
public static void setup() {
    if (!testingBot)
        port(666);
    else
        return;
    Spark.exception(Exception.class, (exception, request, response) -> {
        exception.printStackTrace();
    });
    get("/api/commands", (rq, rs) -> {
        CommandFactory factory = shard0.factory;
        ArrayList<Command> commands = new ArrayList<>();
        factory.getBaseCommands().forEach(command -> {
            try {
                commands.add(new Command(command.getName(), command.getCategory(), command.getDescription()));
            } catch (Exception e) {
                new BotException(e);
            }
        });
        return shard0.gson.toJson(commands);
    });
    get("/api/staff", (rq, rs) -> {
        ArrayList<User> developers = new ArrayList<>();
        for (String id : Ardent.developers) {
            try {
                long u = Long.parseLong(id);
                net.dv8tion.jda.core.entities.User user = UserUtils.getUserById(id);
                String avatarUrl = user.getAvatarUrl();
                if (avatarUrl == null)
                    avatarUrl = getDefaultImage();
                developers.add(new User(id, user.getName(), user.getDiscriminator(), avatarUrl, "developer"));
            } catch (NumberFormatException ignored) {
            }
        }
        ArrayList<User> translators = new ArrayList<>();
        for (String id : Ardent.translators) {
            try {
                net.dv8tion.jda.core.entities.User user = UserUtils.getUserById(id);
                String avatarUrl = user.getAvatarUrl();
                if (avatarUrl == null)
                    avatarUrl = getDefaultImage();
                translators.add(new User(id, user.getName(), user.getDiscriminator(), avatarUrl, "translator"));
            } catch (NumberFormatException ignored) {
            }
        }
        ArrayList<User> moderators = new ArrayList<>();
        for (String id : Ardent.moderators) {
            try {
                net.dv8tion.jda.core.entities.User user = UserUtils.getUserById(id);
                String avatarUrl = user.getAvatarUrl();
                if (avatarUrl == null)
                    avatarUrl = getDefaultImage();
                moderators.add(new User(id, user.getName(), user.getDiscriminator(), avatarUrl, "moderator"));
            } catch (NumberFormatException ignored) {
            }
        }
        ArrayList<ArrayList<User>> staff = new ArrayList<>();
        staff.add(developers);
        staff.add(translators);
        staff.add(moderators);
        return shard0.gson.toJson(staff);
    });
    get("/api/status", (rq, rs) -> {
        InternalStats internalStats = InternalStats.collect();
        return shard0.gson.toJson(new Status(internalStats.getMessagesReceived(), internalStats.getCommandsReceived(), internalStats.getUptime(), internalStats.getLoadedCommands(), internalStats.getGuilds(), internalStats.getUsers(), internalStats.getRoleCount(), internalStats.getTextChannelCount(), internalStats.getVoiceChannelCount(), internalStats.getMusicPlayers()));
    });
}
Also used : Status(tk.ardentbot.core.misc.web.models.Status) User(tk.ardentbot.core.misc.web.models.User) ArrayList(java.util.ArrayList) CommandFactory(tk.ardentbot.core.executor.CommandFactory) BotException(tk.ardentbot.core.misc.logging.BotException) InternalStats(tk.ardentbot.utils.discord.InternalStats) Command(tk.ardentbot.core.misc.web.models.Command) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 13 with BotException

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

the class Marry method noArgs.

@Override
public void noArgs(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
    if (args.length == 1) {
        Marriage marriage = getMarriage(user);
        if (marriage == null)
            sendTranslatedMessage("You're not married! Marry someone with /marry @(User)", channel, user);
        else {
            sendEditedTranslation("**{0}** is married to **{1}**", user, channel, UserUtils.getUserById(marriage.getUser_one()).getName(), UserUtils.getUserById(marriage.getUser_two()).getName());
        }
    } else {
        List<User> mentionedUsers = message.getMentionedUsers();
        if (mentionedUsers.size() == 0) {
            sendTranslatedMessage("You need to mention someone!", channel, user);
            return;
        }
        User toMarryTo = mentionedUsers.get(0);
        Marriage marriage = getMarriage(user);
        if (marriage != null) {
            sendTranslatedMessage("Polygamy isn't allowed >.>", channel, user);
            return;
        }
        if (toMarryTo.isBot()) {
            sendTranslatedMessage("You can't marry a bot, but nice try! As for me, I'll forever love Adam.", channel, user);
            return;
        }
        if (getMarriage(toMarryTo) != null) {
            sendTranslatedMessage("That person is already married!", channel, user);
            return;
        }
        if (toMarryTo.getId().equals(user.getId())) {
            sendTranslatedMessage("Why are you even trying that??", channel, user);
            return;
        }
        sendEditedTranslation("{0}, {1} is proposing to you! Type `yes` to accept or `no` to brutally reject them", user, channel, toMarryTo.getAsMention(), user.getAsMention());
        longInteractiveOperation(channel, message, toMarryTo, 90, replyMessage -> {
            String reply = replyMessage.getContent();
            if (reply.equalsIgnoreCase("yes")) {
                Marriage m = getMarriage(toMarryTo);
                if (m != null) {
                    sendTranslatedMessage("Polygamy isn't allowed >.>", channel, user);
                    return;
                }
                sendTranslatedMessage("Congratulations! You're now married!", channel, user);
                r.db("data").table("marriages").insert(r.json(gson.toJson(new Marriage(user.getId(), toMarryTo.getId())))).run(connection);
            } else if (reply.equalsIgnoreCase("no")) {
                try {
                    sendEditedTranslation("Damn, {1} rejected you, {0} :frowning:", user, channel, user.getAsMention(), toMarryTo.getName());
                } catch (Exception e) {
                    new BotException(e);
                }
            } else {
                sendTranslatedMessage("Received an invalid response, cancelling marriage...", channel, user);
            }
        });
    }
}
Also used : User(net.dv8tion.jda.core.entities.User) BotException(tk.ardentbot.core.misc.logging.BotException) Marriage(tk.ardentbot.rethink.models.Marriage) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 14 with BotException

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

the class MessageUtils method getDefaultEmbed.

public static EmbedBuilder getDefaultEmbed(User author) {
    try {
        final Random random = new Random();
        final float hue = random.nextFloat();
        final float saturation = (random.nextInt(2000) + 1000) / 10000f;
        final float luminance = 2f;
        final Color color = Color.getHSBColor(hue, saturation, luminance);
        EmbedBuilder builder = new EmbedBuilder();
        builder.setColor(color);
        builder.setFooter("Requested by {0}".replace("{0}", author.getName() + "#" + author.getDiscriminator()), author.getAvatarUrl());
        return builder;
    } catch (Exception e) {
        new BotException(e);
        return null;
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Random(java.util.Random) BotException(tk.ardentbot.core.misc.logging.BotException) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 15 with BotException

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

the class Music method loadAndPlay.

static void loadAndPlay(Message message, User user, Command command, final TextChannel channel, String trackUrl, final VoiceChannel voiceChannel, boolean search, boolean useEmbedSelect) {
    if (trackUrl.contains("spotify.com")) {
        String[] parsed = trackUrl.split("/track/");
        if (parsed.length == 2) {
            final TrackRequest request = spotifyApi.getTrack(parsed[1]).build();
            try {
                trackUrl = request.get().getName();
            } catch (Exception e) {
                new BotException(e);
            }
        }
    }
    Guild guild = channel.getGuild();
    GuildMusicManager musicManager = getGuildAudioPlayer(channel.getGuild(), channel);
    String finalTrackUrl = trackUrl;
    GuildUtils.getShard(guild).playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            if (!UserUtils.hasTierTwoPermissions(user) && !EntityGuild.get(guild).isPremium()) {
                try {
                    if (!shouldContinue(user, guild, channel, track)) {
                        return;
                    }
                } catch (Exception e) {
                    new BotException(e);
                }
            }
            try {
                command.sendTranslatedMessage("Adding {0} to the queue".replace("{0}", track.getInfo().title) + " " + getDuration(track), channel, user);
            } catch (Exception e) {
                new BotException(e);
            }
            play(user, guild, voiceChannel, musicManager, track, channel);
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            List<AudioTrack> tracks = playlist.getTracks();
            if (playlist.isSearchResult()) {
                try {
                    if (!useEmbedSelect) {
                        AudioTrack[] possible;
                        if (playlist.getTracks().size() >= 5)
                            possible = playlist.getTracks().subList(0, 5).toArray(new AudioTrack[5]);
                        else
                            possible = playlist.getTracks().toArray(new AudioTrack[playlist.getTracks().size()]);
                        ArrayList<String> names = new ArrayList<>();
                        for (AudioTrack audioTrack : possible) {
                            names.add(audioTrack.getInfo().title);
                        }
                        Message embed = command.sendEmbed(command.chooseFromList("Choose Song", guild, user, command, names.toArray(new String[5])), channel, user);
                        interactiveOperation(channel, message, selectionMessage -> {
                            try {
                                AudioTrack selected = possible[Integer.parseInt(selectionMessage.getContent()) - 1];
                                if (!UserUtils.hasTierTwoPermissions(user) && !EntityGuild.get(guild).isPremium()) {
                                    try {
                                        if (!shouldContinue(user, guild, channel, selected)) {
                                            return;
                                        }
                                    } catch (Exception e) {
                                        new BotException(e);
                                    }
                                }
                                try {
                                    embed.delete().queue();
                                    selectionMessage.delete().queue();
                                } catch (Exception ignored) {
                                }
                                play(user, guild, voiceChannel, musicManager, selected, channel);
                                command.sendTranslatedMessage("Adding {0} to the queue".replace("{0}", selected.getInfo().title) + " " + getDuration(selected), channel, user);
                            } catch (Exception e) {
                                command.sendTranslatedMessage("Invalid response", channel, user);
                            }
                        });
                    } else {
                        AudioTrack track = playlist.getTracks().get(0);
                        play(user, guild, voiceChannel, musicManager, track, channel);
                        command.sendTranslatedMessage("Adding {0} to the queue".replace("{0}", track.getInfo().title) + " " + getDuration(track), channel, user);
                    }
                } catch (Exception e) {
                    new BotException(e);
                }
            } else {
                if (!UserUtils.hasTierTwoPermissions(user) && !EntityGuild.get(guild).isPremium()) {
                    try {
                        if (!shouldContinue(user, guild, channel, 1)) {
                            return;
                        }
                    } catch (Exception e) {
                        new BotException(e);
                    }
                }
                try {
                    command.sendTranslatedMessage("Adding {0} songs to the queue".replace("{0}", String.valueOf(tracks.size())), channel, user);
                } catch (Exception e) {
                    new BotException(e);
                }
                for (AudioTrack track : tracks) {
                    play(user, guild, voiceChannel, musicManager, track, channel);
                }
            }
        }

        @Override
        public void noMatches() {
            if (!search) {
                loadAndPlay(message, user, command, channel, "ytsearch: " + finalTrackUrl, voiceChannel, true, useEmbedSelect);
            } else {
                try {
                    command.sendTranslatedMessage("I couldn't find a song with that name", channel, user);
                } catch (Exception e) {
                    new BotException(e);
                }
            }
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            try {
                sendTo(channel, guild).sendMessage("I wasn't able to play that track... **Reason: **" + exception.getLocalizedMessage()).queue();
            } catch (Exception e) {
                new BotException(e);
            }
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) TrackRequest(com.wrapper.spotify.methods.TrackRequest) MessageUtils(tk.ardentbot.utils.discord.MessageUtils) MusicSettingsModel(tk.ardentbot.rethink.models.MusicSettingsModel) Database.r(tk.ardentbot.rethink.Database.r) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) ArrayList(java.util.ArrayList) EntityGuild(tk.ardentbot.utils.rpg.EntityGuild) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) GuildUtils(tk.ardentbot.utils.discord.GuildUtils) SQLException(java.sql.SQLException) Permission(net.dv8tion.jda.core.Permission) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) Region(net.dv8tion.jda.core.Region) ShardManager(tk.ardentbot.main.ShardManager) Command(tk.ardentbot.core.executor.Command) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) UserUtils(tk.ardentbot.utils.discord.UserUtils) Subcommand(tk.ardentbot.core.executor.Subcommand) AudioManager(net.dv8tion.jda.core.managers.AudioManager) BaseCommand(tk.ardentbot.core.executor.BaseCommand) BotException(tk.ardentbot.core.misc.logging.BotException) Shard(tk.ardentbot.main.Shard) AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Ardent.spotifyApi(tk.ardentbot.main.Ardent.spotifyApi) Database.connection(tk.ardentbot.rethink.Database.connection) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) Pair(tk.ardentbot.utils.javaAdditions.Pair) WordUtils(org.apache.commons.lang.WordUtils) ArrayList(java.util.ArrayList) EntityGuild(tk.ardentbot.utils.rpg.EntityGuild) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) SQLException(java.sql.SQLException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) BotException(tk.ardentbot.core.misc.logging.BotException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) TrackRequest(com.wrapper.spotify.methods.TrackRequest) BotException(tk.ardentbot.core.misc.logging.BotException) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) ArrayList(java.util.ArrayList) List(java.util.List) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

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