Search in sources :

Example 11 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class HttpHandler method unban.

@Route(route = "/unban", method = "POST")
public static void unban(String cookie, HttpServletRequest req, HttpServletResponse res) throws SQLException, IOException {
    ResultSet session = DISCORD_AUTH.getSessionByCookie(cookie);
    if (session.next()) {
        JSONObject body = getBody(req);
        long gid = Long.parseLong(body.getString("guild"));
        Guild g = emolgajda.getGuildById(gid);
        if (g == null) {
            replyError(res, "No matching guild");
            return;
        }
        if (!g.retrieveMemberById(session.getLong("userid")).complete().hasPermission(Permission.MANAGE_SERVER)) {
            replyError(res, "No permission to unban users for guild %s", g.getName());
            return;
        }
        res.getWriter().println(DBManagers.BAN.unban(g, Long.parseLong(body.getString("member"))));
    } else {
        res.getWriter().println(new JSONObject().put("error", "invalidcookie").toString());
    }
}
Also used : JSONObject(org.jsolf.JSONObject) ResultSet(java.sql.ResultSet) Guild(net.dv8tion.jda.api.entities.Guild)

Example 12 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class HttpHandler method exchangeCodeRoute.

@Route(route = "/discordauth", needsCookie = false)
public static void exchangeCodeRoute(String cookie, HttpServletRequest req, HttpServletResponse res) throws IOException {
    Map<String, String> map = getQueryMap(req.getQueryString());
    if (!map.containsKey("code")) {
        res.sendRedirect("http://localhost:4200");
    }
    String code = map.get("code");
    JSONObject tokens = exchangeCode(code);
    logger.info("tokens = " + tokens);
    JSONObject data = getUserInfo(tokens.getString("access_token"));
    logger.info("data = " + data);
    res.sendRedirect("https://emolga.epizy.com/?c=" + DISCORD_AUTH.generateCookie(tokens, Long.parseLong(data.getString("id"))));
// .send(new JSONObject().put("key", "token").put("token", DISCORD_AUTH.generateCookie(tokens, Long.parseLong(data.getString("id")))).toString());
}
Also used : JSONObject(org.jsolf.JSONObject)

Example 13 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class HttpHandler method warnedUsers.

@Route(route = "/guilddata/(\\d+)/warned")
public static void warnedUsers(String cookie, HttpServletRequest req, HttpServletResponse res, List<String> args) throws SQLException, IOException {
    ResultSet session = DISCORD_AUTH.getSessionByCookie(cookie);
    if (session.next()) {
        long gid = Long.parseLong(args.get(0));
        Guild g = emolgajda.getGuildById(gid);
        if (g == null) {
            replyError(res, "No matching guild");
            return;
        }
        if (!g.retrieveMemberById(session.getLong("userid")).complete().hasPermission(Permission.MANAGE_SERVER)) {
            replyError(res, "No permission to view warned users for guild %s", g.getName());
            return;
        }
        res.getWriter().println(DBManagers.WARNS.getWarns(g));
    } else {
        res.getWriter().println(new JSONObject().put("error", "invalidcookie").toString());
    }
}
Also used : JSONObject(org.jsolf.JSONObject) ResultSet(java.sql.ResultSet) Guild(net.dv8tion.jda.api.entities.Guild)

Example 14 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class HttpHandler method guilds.

@Route(route = "/guilds")
public static void guilds(String cookie, HttpServletRequest req, HttpServletResponse res) throws SQLException, IOException {
    if (guildCache.containsKey(cookie)) {
        logger.info("FROM CACHE");
        res.getWriter().println(guildCache.get(cookie));
        return;
    }
    ResultSet session = DISCORD_AUTH.getSessionByCookie(cookie);
    if (session.next()) {
        String token = getAccessToken(session);
        JSONArray guilds = getGuilds(token);
        logger.info("token = " + token);
        List<String> gl = emolgajda.getGuilds().stream().map(Guild::getId).toList();
        JSONArray arr = new JSONArray();
        List<JSONObject> joined = new LinkedList<>();
        List<JSONObject> notJoined = new LinkedList<>();
        for (JSONObject json : guilds.toJSONList()) {
            JSONObject o = new JSONObject();
            if ((json.getLong("permissions") & Permission.MANAGE_SERVER.getRawValue()) > 0) {
                o.put("id", json.getString("id"));
                o.put("name", json.getString("name"));
                o.put("url", json.isNull("icon") ? "assets/images/defaultservericon.png" : "https://cdn.discordapp.com/icons/" + json.getLong("id") + "/" + json.getString("icon") + ".png");
                boolean j = gl.contains(json.getString("id"));
                o.put("joined", j);
                if (j)
                    joined.add(o);
                else
                    notJoined.add(o);
            }
        }
        joined.forEach(arr::put);
        notJoined.forEach(arr::put);
        guildCache.put(cookie, arr);
        res.getWriter().println(arr);
    } else {
        res.getWriter().println(new JSONObject().put("error", "invalidcookie").toString());
    }
}
Also used : JSONObject(org.jsolf.JSONObject) ResultSet(java.sql.ResultSet) JSONArray(org.jsolf.JSONArray)

Example 15 with JSONObject

use of org.jsolf.JSONObject in project Emolga by TecToast.

the class HttpHandler method bannedUsers.

@Route(route = "/guilddata/(\\d+)/banned")
public static void bannedUsers(String cookie, HttpServletRequest req, HttpServletResponse res, List<String> args) throws SQLException, IOException {
    ResultSet session = DISCORD_AUTH.getSessionByCookie(cookie);
    if (session.next()) {
        long gid = Long.parseLong(args.get(0));
        Guild g = emolgajda.getGuildById(gid);
        if (g == null) {
            replyError(res, "No matching guild");
            return;
        }
        if (!g.retrieveMemberById(session.getLong("userid")).complete().hasPermission(Permission.MANAGE_SERVER)) {
            replyError(res, "No permission to view banned users for guild %s", g.getName());
            return;
        }
        res.getWriter().println(DBManagers.BAN.getBans(g));
    } else {
        res.getWriter().println(new JSONObject().put("error", "invalidcookie").toString());
    }
}
Also used : JSONObject(org.jsolf.JSONObject) ResultSet(java.sql.ResultSet) Guild(net.dv8tion.jda.api.entities.Guild)

Aggregations

JSONObject (org.jsolf.JSONObject)72 TextChannel (net.dv8tion.jda.api.entities.TextChannel)23 Member (net.dv8tion.jda.api.entities.Member)20 Command (de.tectoast.emolga.commands.Command)19 JSONArray (org.jsolf.JSONArray)19 RequestBuilder (de.tectoast.emolga.utils.RequestBuilder)16 CommandCategory (de.tectoast.emolga.commands.CommandCategory)15 GuildCommandEvent (de.tectoast.emolga.commands.GuildCommandEvent)15 Draft (de.tectoast.emolga.utils.draft.Draft)14 Guild (net.dv8tion.jda.api.entities.Guild)13 DraftPokemon (de.tectoast.emolga.utils.draft.DraftPokemon)12 Collectors (java.util.stream.Collectors)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Tierlist (de.tectoast.emolga.utils.draft.Tierlist)10 Coord (de.tectoast.emolga.utils.records.Coord)9 Draft.getIndex (de.tectoast.emolga.utils.draft.Draft.getIndex)8 ResultSet (java.sql.ResultSet)8 java.util (java.util)8 ArrayList (java.util.ArrayList)7