Search in sources :

Example 1 with User

use of tk.ardentbot.core.misc.web.models.User 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)

Aggregations

ArrayList (java.util.ArrayList)1 CommandFactory (tk.ardentbot.core.executor.CommandFactory)1 BotException (tk.ardentbot.core.misc.logging.BotException)1 Command (tk.ardentbot.core.misc.web.models.Command)1 Status (tk.ardentbot.core.misc.web.models.Status)1 User (tk.ardentbot.core.misc.web.models.User)1 InternalStats (tk.ardentbot.utils.discord.InternalStats)1