Search in sources :

Example 21 with CalendarData

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

the class LinkCalendarCommand method issueCommand.

/**
 * Issues the command this Object is responsible for.
 * @param args The command arguments.
 * @param event The event received.
 * @return <code>true</code> if successful, else <code>false</code>.
 */
@Override
public Boolean issueCommand(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    try {
        // TODO: Handle multiple calendars...
        CalendarData data = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
        if (data.getCalendarAddress().equalsIgnoreCase("primary")) {
            // Does not have a calendar.
            Message.sendMessage(MessageManager.getMessage("Creator.Calendar.NoCalendar", settings), event);
        } else {
            Calendar cal;
            if (settings.useExternalCalendar()) {
                cal = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute();
            } else {
                cal = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute();
            }
            Message.sendMessage(CalendarMessageFormatter.getCalendarLinkEmbed(cal, settings), event);
        }
    } catch (Exception e) {
        Logger.getLogger().exception(event.getAuthor(), "Failed to connect to Google Cal.", e, this.getClass(), true);
        Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
    }
    return false;
}
Also used : CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.model.Calendar)

Example 22 with CalendarData

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

the class TimeCommand method calendarTime.

private void calendarTime(MessageReceivedEvent event, GuildSettings settings) {
    try {
        // TODO: Handle multiple calendars...
        CalendarData data = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
        if (data.getCalendarAddress().equalsIgnoreCase("primary")) {
            // Does not have a calendar.
            Message.sendMessage(MessageManager.getMessage("Creator.Calendar.NoCalendar", settings), event);
        } else {
            Calendar cal;
            if (settings.useExternalCalendar()) {
                cal = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute();
            } else {
                cal = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute();
            }
            LocalDateTime ldt = LocalDateTime.now(ZoneId.of(cal.getTimeZone()));
            // Okay... format and then we can go from there...
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a");
            String thisIsTheCorrectTime = format.format(ldt);
            // Build embed and send.
            EmbedBuilder em = new EmbedBuilder();
            em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
            em.withAuthorName("DisCal");
            em.withTitle(MessageManager.getMessage("Embed.Time.Title", settings));
            em.appendField(MessageManager.getMessage("Embed.Time.Time", settings), thisIsTheCorrectTime, false);
            em.appendField(MessageManager.getMessage("Embed.Time.TimeZone", settings), cal.getTimeZone(), false);
            em.withFooterText(MessageManager.getMessage("Embed.Time.Footer", settings));
            em.withUrl(CalendarMessageFormatter.getCalendarLink(cal.getId()));
            em.withColor(56, 138, 237);
            Message.sendMessage(em.build(), event);
        }
    } catch (Exception e) {
        Logger.getLogger().exception(event.getAuthor(), "Failed to connect to Google Cal.", e, this.getClass(), true);
        Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.model.Calendar) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 23 with CalendarData

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

the class DatabaseManager method getAllCalendars.

public ArrayList<CalendarData> getAllCalendars() {
    ArrayList<CalendarData> calendars = new ArrayList<>();
    try {
        if (databaseInfo.getMySQL().checkConnection()) {
            String calendarTableName = String.format("%scalendars", databaseInfo.getPrefix());
            PreparedStatement stmt = databaseInfo.getConnection().prepareStatement("SELECT * FROM " + calendarTableName);
            ResultSet res = stmt.executeQuery();
            while (res.next()) {
                CalendarData calData = new CalendarData(Long.valueOf(res.getString("GUILD_ID")), res.getInt("CALENDAR_NUMBER"));
                calData.setCalendarId(res.getString("CALENDAR_ID"));
                calData.setCalendarAddress(res.getString("CALENDAR_ADDRESS"));
                calData.setExternal(res.getBoolean("EXTERNAL"));
                calendars.add(calData);
            }
            stmt.close();
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get all calendars!", e, this.getClass(), true);
    }
    return calendars;
}
Also used : CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) ArrayList(java.util.ArrayList)

Example 24 with CalendarData

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

the class DatabaseManager method getMainCalendar.

public CalendarData getMainCalendar(long guildId) {
    CalendarData calData = new CalendarData(guildId, 1);
    try {
        if (databaseInfo.getMySQL().checkConnection()) {
            String calendarTableName = String.format("%scalendars", databaseInfo.getPrefix());
            String query = "SELECT * FROM " + calendarTableName + " WHERE GUILD_ID = '" + String.valueOf(guildId) + "';";
            PreparedStatement statement = databaseInfo.getConnection().prepareStatement(query);
            ResultSet res = statement.executeQuery();
            while (res.next()) {
                if (res.getInt("CALENDAR_NUMBER") == 1) {
                    calData.setCalendarId(res.getString("CALENDAR_ID"));
                    calData.setCalendarAddress(res.getString("CALENDAR_ADDRESS"));
                    calData.setExternal(res.getBoolean("EXTERNAL"));
                    break;
                }
            }
            statement.close();
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get calendar settings.", e, this.getClass(), true);
    }
    return calData;
}
Also used : CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData)

Example 25 with CalendarData

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

the class CalendarEndpoint method listCalendars.

public static String listCalendars(Request request, Response response) {
    try {
        JSONObject jsonMain = new JSONObject(request.body());
        Long guildId = jsonMain.getLong("guild_id");
        ArrayList<JSONObject> cals = new ArrayList<>();
        for (CalendarData cal : DatabaseManager.getManager().getAllCalendars(guildId)) {
            if (!cal.getCalendarAddress().equalsIgnoreCase("primary")) {
                JSONObject body = new JSONObject();
                body.put("number", cal.getCalendarNumber());
                body.put("external", cal.isExternal());
                body.put("id", cal.getCalendarId());
                body.put("address", cal.getCalendarAddress());
                cals.add(body);
            }
        }
        JSONObject body = new JSONObject();
        body.put("count", cals.size());
        body.put("calendars", cals);
        response.type("application/json");
        response.status(200);
        response.body(body.toString());
    } catch (JSONException e) {
        e.printStackTrace();
        halt(400, "Bad Request");
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB-API] Internal list calendars error", e, CalendarEndpoint.class, true);
        halt(500, "Internal Server Error");
    }
    return response.body();
}
Also used : JSONObject(org.json.JSONObject) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

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