Search in sources :

Example 6 with WebGuild

use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.

the class GuildUtils method getGuilds.

public static List<WebGuild> getGuilds(String userId) {
    List<WebGuild> guilds = new ArrayList<>();
    IUser user = Main.client.getUserByID(Long.valueOf(userId));
    for (IGuild g : Main.client.getGuilds()) {
        if (g.getUserByID(Long.valueOf(userId)) != null) {
            WebGuild wg = new WebGuild().fromGuild(g);
            wg.setManageServer(PermissionChecker.hasManageServerRole(g, user));
            wg.setDiscalRole(PermissionChecker.hasSufficientRole(g, user));
            guilds.add(wg);
        }
    }
    return guilds;
}
Also used : ArrayList(java.util.ArrayList) IUser(sx.blah.discord.handle.obj.IUser) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) IGuild(sx.blah.discord.handle.obj.IGuild)

Example 7 with WebGuild

use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.

the class EventEndpoint method updateEvent.

public static String updateEvent(Request request, Response response) {
    JSONObject body = new JSONObject(request.body());
    String eventId = body.getString("id");
    GuildSettings settings;
    if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
        settings = g.getSettings();
    } else {
        long guildId = body.getLong("guild_id");
        settings = DatabaseManager.getManager().getSettings(guildId);
    }
    // Okay, time to update the event
    try {
        Calendar service;
        if (settings.useExternalCalendar()) {
            service = CalendarAuth.getCalendarService(settings);
        } else {
            service = CalendarAuth.getCalendarService();
        }
        CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
        com.google.api.services.calendar.model.Calendar cal = service.calendars().get(calendarData.getCalendarId()).execute();
        Event event = new Event();
        event.setId(eventId);
        event.setVisibility("public");
        event.setSummary(body.getString("summary"));
        event.setDescription(body.getString("description"));
        EventDateTime start = new EventDateTime();
        start.setDateTime(new DateTime(body.getLong("epochStart")));
        event.setStart(start.setTimeZone(cal.getTimeZone()));
        EventDateTime end = new EventDateTime();
        end.setDateTime(new DateTime(body.getLong("epochEnd")));
        event.setEnd(end.setTimeZone(cal.getTimeZone()));
        if (!body.getString("color").equalsIgnoreCase("NONE")) {
            event.setColorId(EventColor.fromNameOrHexOrID(body.getString("color")).getId().toString());
        }
        if (!body.getString("location").equalsIgnoreCase("") || !body.getString("location").equalsIgnoreCase("N/a")) {
            event.setLocation(body.getString("location"));
        }
        JSONObject recur = body.getJSONObject("recurrence");
        if (recur.getBoolean("recur")) {
            // Handle recur
            Recurrence recurrence = new Recurrence();
            recurrence.setFrequency(EventFrequency.fromValue(recur.getString("frequency")));
            recurrence.setCount(recur.getInt("count"));
            recurrence.setInterval(recur.getInt("interval"));
            String[] rr = new String[] { recurrence.toRRule() };
            event.setRecurrence(Arrays.asList(rr));
        }
        EventData ed = new EventData(settings.getGuildID());
        if (!body.getString("image").equalsIgnoreCase("") && ImageUtils.validate(body.getString("image"))) {
            ed.setImageLink(body.getString("image"));
            ed.setEventId(eventId);
            ed.setEventEnd(event.getEnd().getDateTime().getValue());
        }
        if (ed.shouldBeSaved()) {
            DatabaseManager.getManager().updateEventData(ed);
        }
        service.events().update(calendarData.getCalendarId(), eventId, event).execute();
        response.status(200);
        response.body(ResponseUtils.getJsonResponseMessage("Successfully updated event!"));
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Failed to update event!", e, EventEndpoint.class, true);
        e.printStackTrace();
        response.status(500);
        response.body(ResponseUtils.getJsonResponseMessage("Failed to update event!"));
    }
    return response.body();
}
Also used : Recurrence(com.cloudcraftgaming.discal.api.object.event.Recurrence) EventDateTime(com.google.api.services.calendar.model.EventDateTime) Calendar(com.google.api.services.calendar.Calendar) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) EventData(com.cloudcraftgaming.discal.api.object.event.EventData) JSONObject(org.json.JSONObject) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Event(com.google.api.services.calendar.model.Event) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 8 with WebGuild

use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.

the class EventEndpoint method createEvent.

public static String createEvent(Request request, Response response) {
    JSONObject body = new JSONObject(request.body());
    GuildSettings settings;
    if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
        settings = g.getSettings();
    } else {
        long guildId = body.getLong("guild_id");
        settings = DatabaseManager.getManager().getSettings(guildId);
    }
    // Okay, time to update the event
    try {
        Calendar service;
        if (settings.useExternalCalendar()) {
            service = CalendarAuth.getCalendarService(settings);
        } else {
            service = CalendarAuth.getCalendarService();
        }
        CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
        com.google.api.services.calendar.model.Calendar cal = service.calendars().get(calendarData.getCalendarId()).execute();
        Event event = new Event();
        event.setId(KeyGenerator.generateEventId());
        event.setVisibility("public");
        event.setSummary(body.getString("summary"));
        event.setDescription(body.getString("description"));
        EventDateTime start = new EventDateTime();
        start.setDateTime(new DateTime(body.getLong("epochStart")));
        event.setStart(start.setTimeZone(cal.getTimeZone()));
        EventDateTime end = new EventDateTime();
        end.setDateTime(new DateTime(body.getLong("epochEnd")));
        event.setEnd(end.setTimeZone(cal.getTimeZone()));
        if (!body.getString("color").equalsIgnoreCase("NONE")) {
            event.setColorId(EventColor.fromNameOrHexOrID(body.getString("color")).getId().toString());
        }
        if (!body.getString("location").equalsIgnoreCase("") || !body.getString("location").equalsIgnoreCase("N/a")) {
            event.setLocation(body.getString("location"));
        }
        JSONObject recur = body.getJSONObject("recurrence");
        if (recur.getBoolean("recur")) {
            // Handle recur
            Recurrence recurrence = new Recurrence();
            recurrence.setFrequency(EventFrequency.fromValue(recur.getString("frequency")));
            recurrence.setCount(recur.getInt("count"));
            recurrence.setInterval(recur.getInt("interval"));
            String[] rr = new String[] { recurrence.toRRule() };
            event.setRecurrence(Arrays.asList(rr));
        }
        EventData ed = new EventData(settings.getGuildID());
        if (!body.getString("image").equalsIgnoreCase("") && ImageUtils.validate(body.getString("image"))) {
            ed.setImageLink(body.getString("image"));
            ed.setEventEnd(event.getEnd().getDateTime().getValue());
        }
        if (ed.shouldBeSaved()) {
            DatabaseManager.getManager().updateEventData(ed);
        }
        Event confirmed = service.events().insert(calendarData.getCalendarId(), event).execute();
        response.status(200);
        JSONObject respondBody = new JSONObject();
        respondBody.put("Message", "Successfully create event!");
        respondBody.put("id", confirmed.getId());
        response.body(respondBody.toString());
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Failed to update event!", e, EventEndpoint.class, true);
        e.printStackTrace();
        response.status(500);
        response.body(ResponseUtils.getJsonResponseMessage("Failed to update event!"));
    }
    return response.body();
}
Also used : Recurrence(com.cloudcraftgaming.discal.api.object.event.Recurrence) EventDateTime(com.google.api.services.calendar.model.EventDateTime) Calendar(com.google.api.services.calendar.Calendar) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) EventData(com.cloudcraftgaming.discal.api.object.event.EventData) JSONObject(org.json.JSONObject) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Event(com.google.api.services.calendar.model.Event) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 9 with WebGuild

use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.

the class EventEndpoint method getEventsForSelectedDate.

public static String getEventsForSelectedDate(Request request, Response response) {
    JSONObject requestBody = new JSONObject(request.body());
    Long startEpoch = Long.valueOf(requestBody.getString("StartEpoch"));
    Long endEpoch = startEpoch + 86400000L;
    GuildSettings settings;
    if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
        settings = g.getSettings();
    } else {
        long guildId = requestBody.getLong("guild_id");
        settings = DatabaseManager.getManager().getSettings(guildId);
    }
    // okay, lets actually get the month's events.
    try {
        Calendar service;
        if (settings.useExternalCalendar()) {
            service = CalendarAuth.getCalendarService(settings);
        } else {
            service = CalendarAuth.getCalendarService();
        }
        CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
        Events events = service.events().list(calendarData.getCalendarAddress()).setTimeMin(new DateTime(startEpoch)).setTimeMax(new DateTime(endEpoch)).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
        List<Event> items = events.getItems();
        String tz = "Error/Unknown";
        try {
            tz = service.calendars().get(calendarData.getCalendarAddress()).execute().getTimeZone();
        } catch (Exception ignore) {
        }
        List<JSONObject> eventsJson = new ArrayList<>();
        for (Event e : items) {
            JSONObject jo = new JSONObject();
            jo.put("id", e.getId());
            jo.put("epochStart", e.getStart().getDateTime().getValue());
            jo.put("epochEnd", e.getEnd().getDateTime().getValue());
            jo.put("timezone", tz);
            jo.put("summary", e.getSummary());
            jo.put("description", e.getDescription());
            if (e.getLocked() != null) {
                jo.put("location", e.getLocation());
            } else {
                jo.put("location", "N/a");
            }
            jo.put("color", EventColor.fromNameOrHexOrID(e.getColorId()).name());
            jo.put("isParent", !(e.getId().contains("_")));
            if (e.getRecurrence() != null && e.getRecurrence().size() > 0) {
                jo.put("recur", true);
                Recurrence r = new Recurrence().fromRRule(e.getRecurrence().get(0));
                JSONObject rjo = new JSONObject();
                rjo.put("frequency", r.getFrequency().name());
                rjo.put("count", r.getCount());
                rjo.put("interval", r.getInterval());
                jo.put("recurrence", rjo);
            } else {
                jo.put("recur", false);
                JSONObject rjo = new JSONObject();
                rjo.put("frequency", EventFrequency.DAILY.name());
                rjo.put("count", -1);
                rjo.put("interval", 1);
                jo.put("recurrence", rjo);
            }
            EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), e.getId());
            jo.put("image", ed.getImageLink());
            eventsJson.add(jo);
        }
        JSONObject body = new JSONObject();
        body.put("events", eventsJson);
        body.put("count", eventsJson.size());
        response.body(body.toString());
    } catch (Exception e) {
        response.body("Internal server error!");
        Logger.getLogger().exception(null, "[WEB] Failed to retrieve events for specific date!", e, EventEndpoint.class, true);
        return response.body();
    }
    return response.body();
}
Also used : Recurrence(com.cloudcraftgaming.discal.api.object.event.Recurrence) Calendar(com.google.api.services.calendar.Calendar) ArrayList(java.util.ArrayList) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) EventData(com.cloudcraftgaming.discal.api.object.event.EventData) JSONObject(org.json.JSONObject) Events(com.google.api.services.calendar.model.Events) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Event(com.google.api.services.calendar.model.Event) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 10 with WebGuild

use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.

the class DashboardHandler method handleSettingsUpdate.

public static String handleSettingsUpdate(Request request, Response response) {
    try {
        if (request.queryParams().contains("bot-nick")) {
            // Update bot nickname...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setBotNick(request.queryParams("bot-nick"));
                IGuild guild = Main.client.getGuildByID(Long.valueOf(g.getId()));
                guild.setUserNickname(Main.client.getOurUser(), g.getBotNick());
            }
        } else if (request.queryParams().contains("prefix")) {
            // Update prefix...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                g.getSettings().setPrefix(request.queryParams("prefix"));
                DatabaseManager.getManager().updateSettings(g.getSettings());
            }
        } else if (request.queryParams().contains("lang")) {
            // Update lang...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                g.getSettings().setLang(request.queryParams("lang"));
                DatabaseManager.getManager().updateSettings(g.getSettings());
            }
        } else if (request.queryParams().contains("con-role")) {
            // Update control role...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                IGuild guild = Main.client.getGuildByID(Long.valueOf(g.getId()));
                IRole role = guild.getRoleByID(Long.valueOf(request.queryParams("con-role")));
                if (role.isEveryoneRole()) {
                    g.getSettings().setControlRole("everyone");
                } else {
                    g.getSettings().setControlRole(role.getStringID());
                }
                DatabaseManager.getManager().updateSettings(g.getSettings());
                // Update role list to display properly...
                g.getRoles().clear();
                for (IRole r : guild.getRoles()) {
                    g.getRoles().add(new WebRole().fromRole(r, g.getSettings()));
                }
            }
        } else if (request.queryParams().contains("discal-channel")) {
            // Update control role...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isDiscalRole()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                IGuild guild = Main.client.getGuildByID(Long.valueOf(g.getId()));
                if (request.queryParams("discal-channel").equalsIgnoreCase("0")) {
                    // All channels
                    g.getSettings().setDiscalChannel("all");
                } else {
                    g.getSettings().setDiscalChannel(request.queryParams("discal-channel"));
                }
                DatabaseManager.getManager().updateSettings(g.getSettings());
                // Update channel list to display properly...
                g.getChannels().clear();
                WebChannel all = new WebChannel();
                all.setId(0);
                all.setName("All Channels");
                all.setDiscalChannel(g.getSettings().getDiscalChannel().equalsIgnoreCase("all"));
                g.getChannels().add(all);
                for (IChannel c : guild.getChannels()) {
                    g.getChannels().add(new WebChannel().fromChannel(c, g.getSettings()));
                }
            }
        } else if (request.queryParams().contains("branding")) {
            // Update bot nickname...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                if (g.getSettings().isPatronGuild()) {
                    if (request.queryParams().contains("value")) {
                        g.getSettings().setBranded(true);
                    } else {
                        g.getSettings().setBranded(false);
                    }
                    DatabaseManager.getManager().updateSettings(g.getSettings());
                }
            }
        } else if (request.queryParams().contains("simple-ann")) {
            // Update simple announcements...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            // Guess this one never checked for perms so...
            g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
            if (request.queryParams().contains("value")) {
                g.getSettings().setSimpleAnnouncements(true);
            } else {
                g.getSettings().setSimpleAnnouncements(false);
            }
            DatabaseManager.getManager().updateSettings(g.getSettings());
            response.redirect("/dashboard/guild/announcements", 301);
            return response.body();
        }
        // Finally redirect back to the dashboard
        response.redirect("/dashboard/guild", 301);
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Settings update failed!", e, DashboardHandler.class, true);
        halt(500, "Internal Server Exception");
    }
    return response.body();
}
Also used : WebChannel(com.cloudcraftgaming.discal.api.object.web.WebChannel) IChannel(sx.blah.discord.handle.obj.IChannel) IRole(sx.blah.discord.handle.obj.IRole) WebRole(com.cloudcraftgaming.discal.api.object.web.WebRole) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) IGuild(sx.blah.discord.handle.obj.IGuild) HashMap(java.util.HashMap) Map(java.util.Map) JSONException(org.json.JSONException)

Aggregations

WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)14 Map (java.util.Map)13 HashMap (java.util.HashMap)8 JSONException (org.json.JSONException)8 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)6 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)6 JSONObject (org.json.JSONObject)5 DateTime (com.google.api.client.util.DateTime)4 Calendar (com.google.api.services.calendar.Calendar)4 Event (com.google.api.services.calendar.model.Event)4 EventDateTime (com.google.api.services.calendar.model.EventDateTime)4 EventData (com.cloudcraftgaming.discal.api.object.event.EventData)3 Recurrence (com.cloudcraftgaming.discal.api.object.event.Recurrence)3 WebCalendar (com.cloudcraftgaming.discal.api.object.web.WebCalendar)3 ArrayList (java.util.ArrayList)3 IGuild (sx.blah.discord.handle.obj.IGuild)3 Announcement (com.cloudcraftgaming.discal.api.object.announcement.Announcement)2 Calendar (com.google.api.services.calendar.model.Calendar)2 Events (com.google.api.services.calendar.model.Events)2 IUser (sx.blah.discord.handle.obj.IUser)2