Search in sources :

Example 26 with Announcement

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

the class AnnouncementEndpoint method getAnnouncement.

public static String getAnnouncement(Request request, Response response) {
    try {
        JSONObject jsonMain = new JSONObject(request.body());
        Long guildId = jsonMain.getLong("guild_id");
        String announcementId = jsonMain.getString("id");
        Announcement a = DatabaseManager.getManager().getAnnouncement(UUID.fromString(announcementId), guildId);
        if (a != null) {
            response.type("application/json");
            response.status(200);
            JSONObject body = new JSONObject();
            body.put("channel", a.getAnnouncementChannelId());
            body.put("event_id", a.getEventId());
            body.put("event_color", a.getEventColor().name());
            body.put("type", a.getAnnouncementType().name());
            body.put("hours", a.getHoursBefore());
            body.put("minutes", a.getMinutesBefore());
            body.put("info", a.getInfo());
            body.put("enabled", a.isEnabled());
            body.put("info_only", a.isInfoOnly());
            body.put("subscribers_role", a.getSubscriberRoleIds());
            body.put("subscribers_user", a.getSubscriberUserIds());
            response.body(body.toString());
        } else {
            response.type("application/json");
            response.status(404);
            response.body(ResponseUtils.getJsonResponseMessage("Announcement not found."));
        }
    } catch (JSONException e) {
        e.printStackTrace();
        halt(400, "Bad Request");
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB-API] Internal get announcement error", e, AnnouncementEndpoint.class, true);
        halt(500, "Internal Server Error");
    }
    return response.body();
}
Also used : JSONObject(org.json.JSONObject) Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

Example 27 with Announcement

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

the class DashboardHandler method handleAnnouncementCreate.

public static String handleAnnouncementCreate(Request request, Response response) {
    try {
        String channelId = request.queryParams("channel");
        AnnouncementType type = AnnouncementType.fromValue(request.queryParams("type"));
        // Skip event ID or color here, only get it later if needed.
        String minutesRaw = request.queryParams("minutes");
        String hoursRaw = request.queryParams("hours");
        String info = request.queryParams("info");
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        if (g.isDiscalRole()) {
            Announcement a = new Announcement(Long.valueOf(g.getId()));
            a.setAnnouncementChannelId(channelId);
            a.setMinutesBefore(Integer.valueOf(minutesRaw));
            a.setHoursBefore(Integer.valueOf(hoursRaw));
            a.setInfo(info);
            a.setAnnouncementType(type);
            if (type == AnnouncementType.COLOR) {
                a.setEventColor(EventColor.fromNameOrHexOrID(request.queryParams("color")));
            } else if (type == AnnouncementType.SPECIFIC) {
                a.setEventId(request.queryParams("event-id"));
            } else if (type == AnnouncementType.RECUR) {
                String value = request.queryParams("event-id");
                if (value.contains("_")) {
                    String[] stuff = value.split("_");
                    value = stuff[0];
                }
                a.setEventId(value);
            }
            // Create announcement
            DatabaseManager.getManager().updateAnnouncement(a);
            // Update WebGuild to display correctly...
            g.getAnnouncements().clear();
            g.getAnnouncements().addAll(DatabaseManager.getManager().getAnnouncements(Long.valueOf(g.getId())));
        }
        // Finally redirect back to the dashboard
        response.redirect("/dashboard/guild/announcements", 301);
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Announcement create failed!", e, DashboardHandler.class, true);
        halt(500, "Internal Server Exception");
    }
    return response.body();
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) HashMap(java.util.HashMap) Map(java.util.Map) JSONException(org.json.JSONException) AnnouncementType(com.cloudcraftgaming.discal.api.enums.announcement.AnnouncementType)

Example 28 with Announcement

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

the class DashboardHandler method handleAnnouncementUpdate.

public static String handleAnnouncementUpdate(Request request, Response response) {
    try {
        String announcementId = request.queryParams("id");
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        if (g.isManageServer()) {
            Announcement a = DatabaseManager.getManager().getAnnouncement(UUID.fromString(announcementId), Long.valueOf(g.getId()));
            a.setAnnouncementChannelId(request.queryParams("channel"));
            a.setAnnouncementType(AnnouncementType.fromValue(request.queryParams("type")));
            if (a.getAnnouncementType() == AnnouncementType.COLOR) {
                a.setEventColor(EventColor.fromNameOrHexOrID(request.queryParams("color")));
            } else if (a.getAnnouncementType() == AnnouncementType.SPECIFIC || a.getAnnouncementType() == AnnouncementType.RECUR) {
                String value = request.queryParams("event-id");
                if (value.contains("_")) {
                    String[] stuff = value.split("_");
                    value = stuff[0];
                }
                a.setEventId(value);
            }
            a.setMinutesBefore(Integer.valueOf(request.queryParams("minutes")));
            a.setHoursBefore(Integer.valueOf(request.queryParams("hours")));
            a.setInfo(request.queryParams("info"));
            DatabaseManager.getManager().updateAnnouncement(a);
            // Update announcements list to display correctly.
            g.getAnnouncements().clear();
            g.getAnnouncements().addAll(DatabaseManager.getManager().getAnnouncements(Long.valueOf(g.getId())));
        }
        response.redirect("/dashboard/guild/announcements", 301);
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Failed to update/edit announcement!", e, DashboardHandler.class, true);
    }
    return response.body();
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) HashMap(java.util.HashMap) Map(java.util.Map) JSONException(org.json.JSONException)

Aggregations

Announcement (com.cloudcraftgaming.discal.api.object.announcement.Announcement)28 ArrayList (java.util.ArrayList)7 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)4 IMessage (sx.blah.discord.handle.obj.IMessage)3 WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)2 AnnouncementType (com.cloudcraftgaming.discal.api.enums.announcement.AnnouncementType)1 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)1 AnnouncementCreatorResponse (com.cloudcraftgaming.discal.api.object.announcement.AnnouncementCreatorResponse)1 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)1 PreCalendar (com.cloudcraftgaming.discal.api.object.calendar.PreCalendar)1 PreEvent (com.cloudcraftgaming.discal.api.object.event.PreEvent)1 Calendar (com.google.api.services.calendar.Calendar)1 Event (com.google.api.services.calendar.model.Event)1 IOException (java.io.IOException)1