Search in sources :

Example 1 with EventData

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

the class DatabaseManager method getEventData.

public EventData getEventData(long guildId, String eventId) {
    EventData data = new EventData(guildId);
    if (eventId.contains("_")) {
        eventId = eventId.split("_")[0];
    }
    data.setEventId(eventId);
    try {
        if (databaseInfo.getMySQL().checkConnection()) {
            String eventTableName = String.format("%sevents", databaseInfo.getPrefix());
            String query = "SELECT * FROM " + eventTableName + " WHERE GUILD_ID= '" + String.valueOf(guildId) + "';";
            PreparedStatement statement = databaseInfo.getConnection().prepareStatement(query);
            ResultSet res = statement.executeQuery();
            while (res.next()) {
                if (res.getString("EVENT_ID").equals(eventId)) {
                    data.setEventEnd(res.getLong("EVENT_END"));
                    data.setImageLink(res.getString("IMAGE_LINK"));
                    break;
                }
            }
            statement.close();
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get event data", e, this.getClass(), true);
    }
    return data;
}
Also used : EventData(com.cloudcraftgaming.discal.api.object.event.EventData)

Example 2 with EventData

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

the class DatabaseManager method getAllEventData.

public ArrayList<EventData> getAllEventData() {
    ArrayList<EventData> allData = new ArrayList<>();
    try {
        if (databaseInfo.getMySQL().checkConnection()) {
            String eventTableName = String.format("%sevents", databaseInfo.getPrefix());
            PreparedStatement stmt = databaseInfo.getConnection().prepareStatement("SELECT * FROM " + eventTableName);
            ResultSet res = stmt.executeQuery();
            while (res.next()) {
                if (res.getString("EVENT_ID") != null) {
                    EventData data = new EventData(Long.valueOf(res.getString("GUILD_ID")));
                    data.setEventId(res.getString("EVENT_ID"));
                    data.setEventEnd(res.getLong("EVENT_END"));
                    data.setImageLink(res.getString("IMAGE_LINK"));
                    allData.add(data);
                }
            }
            stmt.close();
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get all event data", e, this.getClass(), true);
    }
    return allData;
}
Also used : ArrayList(java.util.ArrayList) EventData(com.cloudcraftgaming.discal.api.object.event.EventData)

Example 3 with EventData

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

the class AnnouncementMessageFormatter method sendAnnouncementMessage.

/**
 * Sends an embed with the announcement info in a proper format.
 *
 * @param announcement The announcement to send info about.
 * @param event        the calendar event the announcement is for.
 * @param data         The BotData belonging to the guild.
 */
static void sendAnnouncementMessage(Announcement announcement, Event event, CalendarData data, GuildSettings settings) {
    EmbedBuilder em = new EmbedBuilder();
    em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
    IGuild guild = Main.client.getGuildByID(announcement.getGuildId());
    assert guild != null;
    // Set all of the stuff for embeds regardless of announcement settings
    if (settings.isBranded()) {
        em.withAuthorName(guild.getName());
    } else {
        em.withAuthorName("DisCal");
    }
    em.withTitle(MessageManager.getMessage("Embed.Announcement.Announce.Title", settings));
    EventData ed = DatabaseManager.getManager().getEventData(announcement.getGuildId(), event.getId());
    if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
        em.withImage(ed.getImageLink());
    }
    em.withUrl(event.getHtmlLink());
    try {
        EventColor ec = EventColor.fromNameOrHexOrID(event.getColorId());
        em.withColor(ec.getR(), ec.getG(), ec.getB());
    } catch (Exception e) {
        // I dunno, color probably null.
        em.withColor(56, 138, 237);
    }
    if (!settings.usingSimpleAnnouncements()) {
        em.withFooterText(MessageManager.getMessage("Embed.Announcement.Announce.ID", "%id%", announcement.getAnnouncementId().toString(), settings));
    }
    if (announcement.isInfoOnly()) {
        // Only send info...
        em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Info", settings), announcement.getInfo(), false);
    } else {
        // Requires all announcement data
        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.Announce.Summary", settings), summary, true);
        }
        if (event.getDescription() != null) {
            String description = event.getDescription();
            if (description.length() > 250) {
                description = description.substring(0, 250);
                description = description + " (continues on Google Calendar View)";
            }
            em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Description", settings), description, true);
        }
        if (!settings.usingSimpleAnnouncements()) {
            em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Date", settings), EventMessageFormatter.getHumanReadableDate(event.getStart(), settings, false), true);
            em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Time", settings), EventMessageFormatter.getHumanReadableTime(event.getStart(), settings, false), true);
            try {
                Calendar service = CalendarAuth.getCalendarService();
                String tz = service.calendars().get(data.getCalendarAddress()).execute().getTimeZone();
                em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.TimeZone", settings), tz, true);
            } catch (Exception e1) {
                em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.TimeZone", settings), "Unknown *Error Occurred", true);
            }
        } else {
            String start = EventMessageFormatter.getHumanReadableDate(event.getStart(), settings, false) + " at " + EventMessageFormatter.getHumanReadableTime(event.getStart(), settings, false);
            try {
                Calendar service = CalendarAuth.getCalendarService();
                String tz = service.calendars().get(data.getCalendarAddress()).execute().getTimeZone();
                start = start + " " + tz;
            } catch (Exception e1) {
                start = start + " (TZ UNKNOWN/ERROR)";
            }
            em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Start", settings), start, false);
        }
        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);
            }
        }
        if (!settings.usingSimpleAnnouncements()) {
            em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.EventID", settings), event.getId(), false);
        }
        if (!announcement.getInfo().equalsIgnoreCase("None")) {
            em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Info", settings), announcement.getInfo(), false);
        }
    }
    IChannel channel = null;
    try {
        channel = guild.getChannelByID(Long.valueOf(announcement.getAnnouncementChannelId()));
    } catch (Exception e) {
        Logger.getLogger().exception(null, "Failed to find announcement channel! Will auto delete announcement to prevent issue! | Announcement: " + announcement.getAnnouncementId() + " | Guild: " + announcement.getGuildId(), e, AnnouncementMessageFormatter.class, true);
    }
    if (channel == null) {
        // Channel does not exist or could not be found, automatically delete announcement to prevent issues.
        DatabaseManager.getManager().deleteAnnouncement(announcement.getAnnouncementId().toString());
        return;
    }
    Message.sendMessageAsync(em.build(), getSubscriberMentions(announcement, guild), channel);
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) IChannel(sx.blah.discord.handle.obj.IChannel) Calendar(com.google.api.services.calendar.Calendar) EventColor(com.cloudcraftgaming.discal.api.enums.event.EventColor) IGuild(sx.blah.discord.handle.obj.IGuild) EventData(com.cloudcraftgaming.discal.api.object.event.EventData)

Example 4 with EventData

use of com.cloudcraftgaming.discal.api.object.event.EventData 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 5 with EventData

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

the class AnnouncementMessageFormatter method getFormatAnnouncementEmbed.

/**
 * Gets the EmbedObject for an Announcement.
 * @param a The Announcement to embed.
 * @return The EmbedObject for the Announcement.
 */
public static EmbedObject getFormatAnnouncementEmbed(Announcement a, GuildSettings settings) {
    EmbedBuilder em = new EmbedBuilder();
    em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
    em.withAuthorName("DisCal");
    em.withTitle(MessageManager.getMessage("Embed.Announcement.Info.Title", settings));
    try {
        em.appendField(MessageManager.getMessage("Embed.Announcement.Info.ID", settings), a.getAnnouncementId().toString(), true);
    } catch (NullPointerException e) {
        em.appendField(MessageManager.getMessage("Embed.Announcement.Info.ID", settings), "ID IS NULL???", true);
    }
    em.appendField(MessageManager.getMessage("Embed.Announcement.Info.Type", settings), a.getAnnouncementType().name(), true);
    if (a.getAnnouncementType().equals(AnnouncementType.SPECIFIC)) {
        em.appendField(MessageManager.getMessage("Embed.Announcement.Info.EventID", settings), a.getEventId(), true);
        EventData ed = DatabaseManager.getManager().getEventData(a.getGuildId(), a.getEventId());
        if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
            em.withImage(ed.getImageLink());
        }
    } else if (a.getAnnouncementType().equals(AnnouncementType.COLOR)) {
        em.appendField(MessageManager.getMessage("Embed.Announcement.Info.Color", settings), a.getEventColor().name(), true);
    } else if (a.getAnnouncementType().equals(AnnouncementType.RECUR)) {
        em.appendField(MessageManager.getMessage("Embed.Announcement.Info.RecurID", settings), a.getEventId(), true);
        EventData ed = DatabaseManager.getManager().getEventData(a.getGuildId(), a.getEventId());
        if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
            em.withImage(ed.getImageLink());
        }
    }
    em.appendField(MessageManager.getMessage("Embed.Announcement.Info.Hours", settings), String.valueOf(a.getHoursBefore()), true);
    em.appendField(MessageManager.getMessage("Embed.Announcement.Info.Minutes", settings), String.valueOf(a.getMinutesBefore()), true);
    em.appendField(MessageManager.getMessage("Embed.Announcement.Info.Channel", settings), ChannelUtils.getChannelNameFromNameOrId(a.getAnnouncementChannelId(), a.getGuildId()), true);
    em.appendField(MessageManager.getMessage("Embed.Announcement.Info.Info", settings), a.getInfo(), false);
    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) EventColor(com.cloudcraftgaming.discal.api.enums.event.EventColor) EventData(com.cloudcraftgaming.discal.api.object.event.EventData)

Aggregations

EventData (com.cloudcraftgaming.discal.api.object.event.EventData)12 EventColor (com.cloudcraftgaming.discal.api.enums.event.EventColor)7 Calendar (com.google.api.services.calendar.Calendar)7 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)7 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)5 Event (com.google.api.services.calendar.model.Event)4 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)3 Recurrence (com.cloudcraftgaming.discal.api.object.event.Recurrence)3 WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)3 DateTime (com.google.api.client.util.DateTime)3 EventDateTime (com.google.api.services.calendar.model.EventDateTime)3 Map (java.util.Map)3 JSONObject (org.json.JSONObject)3 ArrayList (java.util.ArrayList)2 IGuild (sx.blah.discord.handle.obj.IGuild)2 Events (com.google.api.services.calendar.model.Events)1 IChannel (sx.blah.discord.handle.obj.IChannel)1