Search in sources :

Example 11 with CalendarData

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

the class EventMessageFormatter method getEventEmbed.

/**
 * Gets an EmbedObject for the specified event.
 *
 * @param event    The event involved.
 * @param settings The guild's settings
 * @return The EmbedObject of the event.
 */
public static EmbedObject getEventEmbed(Event event, GuildSettings settings) {
    EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), event.getId());
    EmbedBuilder em = new EmbedBuilder();
    em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
    em.withAuthorName("DisCal");
    em.withTitle(MessageManager.getMessage("Embed.Event.Info.Title", settings));
    if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
        em.withImage(ed.getImageLink());
    }
    if (event.getSummary() != null) {
        String summary = event.getSummary();
        if (summary.length() > 250) {
            summary = summary.substring(0, 250);
            summary = summary + " (continues on Google Calendar View)";
        }
        em.appendField(MessageManager.getMessage("Embed.Event.Info.Summary", settings), summary, true);
    }
    if (event.getDescription() != null) {
        String description = event.getDescription();
        if (description.length() > 500) {
            description = description.substring(0, 500);
            description = description + " (continues on Google Calendar View)";
        }
        em.appendField(MessageManager.getMessage("Embed.Event.Info.Description", settings), description, true);
    }
    em.appendField(MessageManager.getMessage("Embed.Event.Info.StartDate", settings), getHumanReadableDate(event.getStart(), settings, false), true);
    em.appendField(MessageManager.getMessage("Embed.Event.Info.StartTime", settings), getHumanReadableTime(event.getStart(), settings, false), true);
    em.appendField(MessageManager.getMessage("Embed.Event.Info.EndDate", settings), getHumanReadableDate(event.getEnd(), settings, false), true);
    em.appendField(MessageManager.getMessage("Embed.Event.Info.EndTime", settings), getHumanReadableTime(event.getEnd(), settings, false), true);
    try {
        // TODO: add support for multiple calendars...
        CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
        Calendar service;
        service = settings.useExternalCalendar() ? CalendarAuth.getCalendarService(settings) : CalendarAuth.getCalendarService();
        String tz = service.calendars().get(data.getCalendarAddress()).execute().getTimeZone();
        em.appendField(MessageManager.getMessage("Embed.Event.Info.TimeZone", settings), tz, true);
    } catch (Exception e1) {
        em.appendField(MessageManager.getMessage("Embed.Event.Info.TimeZone", settings), "Error/Unknown", true);
    }
    if (event.getLocation() != null && !event.getLocation().equalsIgnoreCase("")) {
        if (event.getLocation().length() > 300) {
            String location = event.getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
            em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
        } else {
            em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), event.getLocation(), true);
        }
    }
    // TODO: Add info on recurrence here.
    em.withUrl(event.getHtmlLink());
    em.withFooterText(MessageManager.getMessage("Embed.Event.Info.ID", "%id%", event.getId(), settings));
    try {
        EventColor ec = EventColor.fromId(Integer.valueOf(event.getColorId()));
        em.withColor(ec.getR(), ec.getG(), ec.getB());
    } catch (Exception e) {
        // Color is null, ignore and add our default.
        em.withColor(56, 138, 237);
    }
    return em.build();
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar) EventColor(com.cloudcraftgaming.discal.api.enums.event.EventColor) EventData(com.cloudcraftgaming.discal.api.object.event.EventData)

Example 12 with CalendarData

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

the class EventMessageFormatter method getHumanReadableDate.

/**
 * Gets a formatted date.
 *
 * @param eventDateTime The object to get the date from.
 * @return A formatted date.
 */
public static String getHumanReadableDate(@Nullable EventDateTime eventDateTime, GuildSettings settings, boolean preEvent) {
    try {
        if (eventDateTime == null) {
            return "NOT SET";
        } else {
            // Get timezone
            CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
            String timezone;
            if (!preEvent) {
                if (settings.useExternalCalendar()) {
                    timezone = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute().getTimeZone();
                } else {
                    timezone = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute().getTimeZone();
                }
            } else {
                timezone = "America/Chicago";
            }
            if (eventDateTime.getDateTime() != null) {
                long dateTime = eventDateTime.getDateTime().getValue();
                LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                return format.format(ldt);
            } else {
                long dateTime = eventDateTime.getDate().getValue();
                LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                return format.format(ldt);
            }
        }
    } catch (Exception e) {
        Logger.getLogger().exception(null, "Failed to format date", e, EventMessageFormatter.class, true);
        return "ERROR! Code: E001";
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 13 with CalendarData

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

the class EventMessageFormatter method getHumanReadableTime.

/**
 * Gets a formatted time.
 *
 * @param eventDateTime The object to get the time from.
 * @return A formatted time.
 */
public static String getHumanReadableTime(@Nullable EventDateTime eventDateTime, GuildSettings settings, boolean preEvent) {
    try {
        if (eventDateTime == null) {
            return "NOT SET";
        } else {
            // Get timezone
            CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
            String timezone;
            if (!preEvent) {
                if (settings.useExternalCalendar()) {
                    timezone = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute().getTimeZone();
                } else {
                    timezone = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute().getTimeZone();
                }
            } else {
                timezone = "America/Chicago";
            }
            if (eventDateTime.getDateTime() != null) {
                long dateTime = eventDateTime.getDateTime().getValue();
                LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
                DateTimeFormatter format = DateTimeFormatter.ofPattern("hh:mm:ss a");
                return format.format(ldt);
            } else {
                long dateTime = eventDateTime.getDate().getValue();
                LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
                DateTimeFormatter format = DateTimeFormatter.ofPattern("hh:mm:ss a");
                return format.format(ldt);
            }
        }
    } catch (Exception e) {
        Logger.getLogger().exception(null, "Failed to format date", e, EventMessageFormatter.class, true);
        return "ERROR! Code: E002";
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 14 with CalendarData

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

the class AnnouncementMessageFormatter method getCondensedAnnouncementEmbed.

/**
 * Gets the EmbedObject for a Condensed Announcement.
 * @param a The Announcement to embed.
 * @return The EmbedObject for a Condensed Announcement.
 */
public static EmbedObject getCondensedAnnouncementEmbed(Announcement a, GuildSettings settings) {
    EmbedBuilder em = new EmbedBuilder();
    em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
    em.withAuthorName("DisCal");
    em.withTitle(MessageManager.getMessage("Embed.Announcement.Condensed.Title", settings));
    em.appendField(MessageManager.getMessage("Embed.Announcement.Condensed.ID", settings), a.getAnnouncementId().toString(), false);
    em.appendField(MessageManager.getMessage("Embed.Announcement.Condensed.Time", settings), condensedTime(a), false);
    if (a.getAnnouncementType().equals(AnnouncementType.SPECIFIC)) {
        em.appendField(MessageManager.getMessage("Embed.Announcement.Condensed.EventID", settings), a.getEventId(), false);
        try {
            Calendar service;
            if (settings.useExternalCalendar()) {
                service = CalendarAuth.getCalendarService(settings);
            } else {
                service = CalendarAuth.getCalendarService();
            }
            // TODO: Handle multiple calendars...
            CalendarData data = DatabaseManager.getManager().getMainCalendar(a.getGuildId());
            Event event = service.events().get(data.getCalendarAddress(), a.getEventId()).execute();
            EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), event.getId());
            if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
                em.withThumbnail(ed.getImageLink());
            }
            if (event.getSummary() != null) {
                String summary = event.getSummary();
                if (summary.length() > 250) {
                    summary = summary.substring(0, 250);
                    summary = summary + " (continues on Google Calendar View)";
                }
                em.appendField(MessageManager.getMessage("Embed.Announcement.Condensed.Summary", settings), summary, true);
            }
        } catch (Exception e) {
            // Failed to get from google cal.
            Logger.getLogger().exception(null, "Failed to get event for announcement.", e, AnnouncementMessageFormatter.class, true);
        }
    } else if (a.getAnnouncementType().equals(AnnouncementType.COLOR)) {
        em.appendField(MessageManager.getMessage("Embed.Announcement.Condensed.Color", settings), a.getEventColor().name(), true);
    } else if (a.getAnnouncementType().equals(AnnouncementType.RECUR)) {
        em.appendField(MessageManager.getMessage("Embed.Announcement.Condensed.RecurID", settings), a.getEventId(), true);
    }
    em.withFooterText(MessageManager.getMessage("Embed.Announcement.Condensed.Type", "%type%", a.getAnnouncementType().name(), settings));
    if (a.getAnnouncementType().equals(AnnouncementType.COLOR)) {
        EventColor c = a.getEventColor();
        em.withColor(c.getR(), c.getG(), c.getB());
    } else {
        em.withColor(56, 138, 237);
    }
    em.appendField(MessageManager.getMessage("Embed.Announcement.Info.Enabled", settings), a.isEnabled() + "", true);
    return em.build();
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar) EventColor(com.cloudcraftgaming.discal.api.enums.event.EventColor) Event(com.google.api.services.calendar.model.Event) EventData(com.cloudcraftgaming.discal.api.object.event.EventData)

Example 15 with CalendarData

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

the class AnnouncementTask method run.

@Override
public void run() {
    Logger.getLogger().announcement("Starting announcement loop!");
    try {
        // Get the default stuff.
        try {
            discalService = CalendarAuth.getCalendarService();
        } catch (IOException e) {
            Logger.getLogger().exception(null, "Failed to get service! 00a0101", e, this.getClass(), true);
        }
        // NOTE: This list EXCLUDES disabled announcements!!!!!!!
        ArrayList<Announcement> allAnnouncements = DatabaseManager.getManager().getEnabledAnnouncements();
        for (Announcement a : allAnnouncements) {
            Logger.getLogger().announcement("starting an announcement", a.getGuildId() + "", a.getAnnouncementId() + "", "N/a");
            // Check if guild is part of DisCal's guilds. This way we can clear out the database...
            if (!GuildUtils.active(a.getGuildId())) {
                DatabaseManager.getManager().deleteAnnouncement(a.getAnnouncementId().toString());
                continue;
            }
            // Get everything we need ready.
            GuildSettings settings = getSettings(a);
            CalendarData calendar = getCalendarData(a);
            Calendar service;
            try {
                service = getService(settings);
            } catch (Exception e) {
                Logger.getLogger().exception(null, "Failed to handle custom service! 00a102", e, this.getClass(), true);
                continue;
            }
            // Now we can check the announcement type and do all the actual logic here.
            switch(a.getAnnouncementType()) {
                case SPECIFIC:
                    if (EventUtils.eventExists(settings, a.getEventId())) {
                        try {
                            Event e = service.events().get(calendar.getCalendarId(), a.getEventId()).execute();
                            if (inRange(a, e)) {
                                // We can announce it.
                                AnnouncementMessageFormatter.sendAnnouncementMessage(a, e, calendar, settings);
                                // And now lets delete it
                                DatabaseManager.getManager().deleteAnnouncement(a.getAnnouncementId().toString());
                            }
                        } catch (IOException e) {
                            // Event getting error, we know it exists tho
                            Logger.getLogger().exception(null, "Failed to get event! 00a103", e, this.getClass(), true);
                        }
                    } else {
                        // Event is gone, we can just delete this shit.
                        DatabaseManager.getManager().deleteAnnouncement(a.getAnnouncementId().toString());
                    }
                    break;
                case UNIVERSAL:
                    for (Event e : getEvents(settings, calendar, service, a)) {
                        if (inRange(a, e)) {
                            // It fits! Let's do it!
                            AnnouncementMessageFormatter.sendAnnouncementMessage(a, e, calendar, settings);
                        }
                    }
                    break;
                case COLOR:
                    for (Event e : getEvents(settings, calendar, service, a)) {
                        if (a.getEventColor() == EventColor.fromNameOrHexOrID(e.getColorId())) {
                            if (inRange(a, e)) {
                                // It fits! Let's do it!
                                AnnouncementMessageFormatter.sendAnnouncementMessage(a, e, calendar, settings);
                            }
                        }
                    }
                    break;
                case RECUR:
                    for (Event e : getEvents(settings, calendar, service, a)) {
                        if (inRange(a, e)) {
                            if (e.getId().contains("_") && e.getId().split("_")[0].equals(a.getEventId())) {
                                // It fits! Lets announce!
                                AnnouncementMessageFormatter.sendAnnouncementMessage(a, e, calendar, settings);
                            }
                        }
                    }
                    break;
            }
            Logger.getLogger().announcement("finished an announcement", a.getGuildId() + "", a.getAnnouncementId() + "", "N/a");
        }
        // Just clear everything immediately.
        allSettings.clear();
        calendars.clear();
        customServices.clear();
        allEvents.clear();
        Logger.getLogger().announcement("Finished announcement loop!");
    } catch (Exception e) {
        Logger.getLogger().exception(null, "SOMETHING BAD IN THE ANNOUNCER!!!!! PANIC!!!", e, this.getClass(), true);
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar) Event(com.google.api.services.calendar.model.Event) IOException(java.io.IOException) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings) IOException(java.io.IOException)

Aggregations

CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)29 Calendar (com.google.api.services.calendar.Calendar)11 Event (com.google.api.services.calendar.model.Event)9 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)6 WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)6 DateTime (com.google.api.client.util.DateTime)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 JSONObject (org.json.JSONObject)6 EventData (com.cloudcraftgaming.discal.api.object.event.EventData)5 Calendar (com.google.api.services.calendar.model.Calendar)5 EventDateTime (com.google.api.services.calendar.model.EventDateTime)4 Events (com.google.api.services.calendar.model.Events)4 LocalDateTime (java.time.LocalDateTime)4 DateTimeFormatter (java.time.format.DateTimeFormatter)4 JSONException (org.json.JSONException)4 Recurrence (com.cloudcraftgaming.discal.api.object.event.Recurrence)3 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)3 EventColor (com.cloudcraftgaming.discal.api.enums.event.EventColor)2 PreCalendar (com.cloudcraftgaming.discal.api.object.calendar.PreCalendar)2