Search in sources :

Example 1 with CalendarData

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

the class DatabaseManager method getCalendar.

public CalendarData getCalendar(long guildId, Integer calendarNumber) {
    CalendarData calData = new CalendarData(guildId, calendarNumber);
    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") == calendarNumber) {
                    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 data", e, this.getClass(), true);
    }
    return calData;
}
Also used : CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData)

Example 2 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(long guildId) {
    ArrayList<CalendarData> calendars = new ArrayList<>();
    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()) {
                CalendarData calData = new CalendarData(guildId, res.getInt("CALENDAR_NUMBER"));
                calData.setCalendarId(res.getString("CALENDAR_ID"));
                calData.setCalendarAddress(res.getString("CALENDAR_ADDRESS"));
                calData.setExternal(res.getBoolean("EXTERNAL"));
                calendars.add(calData);
            }
            statement.close();
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get all guild calendars.", e, this.getClass(), true);
    }
    return calendars;
}
Also used : CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) ArrayList(java.util.ArrayList)

Example 3 with CalendarData

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

the class EventListCommand method moduleDay.

@SuppressWarnings("Duplicates")
private void moduleDay(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    if (args.length == 1) {
        // Get the upcoming events in the next 24 hours.
        try {
            Calendar service;
            if (settings.useExternalCalendar()) {
                service = CalendarAuth.getCalendarService(settings);
            } else {
                service = CalendarAuth.getCalendarService();
            }
            DateTime now = new DateTime(System.currentTimeMillis());
            DateTime twentyFourHoursFromNow = new DateTime(now.getValue() + 86400000L);
            CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
            Events events = service.events().list(calendarData.getCalendarAddress()).setMaxResults(20).setTimeMin(now).setTimeMax(twentyFourHoursFromNow).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
            List<Event> items = events.getItems();
            if (items.size() == 0) {
                Message.sendMessage(MessageManager.getMessage("Event.List.Found.None", settings), event);
            } else if (items.size() == 1) {
                Message.sendMessage(EventMessageFormatter.getEventEmbed(items.get(0), settings), MessageManager.getMessage("Event.List.Found.One", settings), event);
            } else {
                // List events by Id only.
                Message.sendMessage(MessageManager.getMessage("Event.List.Found.Many", "%amount%", items.size() + "", settings), event);
                for (Event e : items) {
                    Message.sendMessage(EventMessageFormatter.getCondensedEventEmbed(e, settings), event);
                }
            }
        } catch (Exception e) {
            Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
            Logger.getLogger().exception(event.getAuthor(), "Failed to list events.", e, this.getClass(), true);
            e.printStackTrace();
        }
    }
}
Also used : Events(com.google.api.services.calendar.model.Events) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar) Event(com.google.api.services.calendar.model.Event) MessageReceivedEvent(sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent) DateTime(com.google.api.client.util.DateTime)

Example 4 with CalendarData

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

the class EventListCommand method moduleSimpleList.

@SuppressWarnings("Duplicates")
private void moduleSimpleList(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    if (args.length == 0) {
        try {
            Calendar service;
            if (settings.useExternalCalendar()) {
                service = CalendarAuth.getCalendarService(settings);
            } else {
                service = CalendarAuth.getCalendarService();
            }
            DateTime now = new DateTime(System.currentTimeMillis());
            CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
            Events events = service.events().list(calendarData.getCalendarAddress()).setMaxResults(1).setTimeMin(now).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
            List<Event> items = events.getItems();
            if (items.size() == 0) {
                Message.sendMessage(MessageManager.getMessage("Event.List.Found.None", settings), event);
            } else if (items.size() == 1) {
                Message.sendMessage(EventMessageFormatter.getEventEmbed(items.get(0), settings), MessageManager.getMessage("Event.List.Found.One", settings), event);
            }
        } catch (Exception e) {
            Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
            Logger.getLogger().exception(event.getAuthor(), "Failed to list events.", e, this.getClass(), true);
            e.printStackTrace();
        }
    } else if (args.length == 1) {
        try {
            Integer eventNum = Integer.valueOf(args[0]);
            if (eventNum > 15) {
                Message.sendMessage(MessageManager.getMessage("Event.List.Amount.Over", settings), event);
                return;
            }
            if (eventNum < 1) {
                Message.sendMessage(MessageManager.getMessage("Event.List.Amount.Under", settings), event);
                return;
            }
            try {
                Calendar service;
                if (settings.useExternalCalendar()) {
                    service = CalendarAuth.getCalendarService(settings);
                } else {
                    service = CalendarAuth.getCalendarService();
                }
                DateTime now = new DateTime(System.currentTimeMillis());
                CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
                Events events = service.events().list(calendarData.getCalendarAddress()).setMaxResults(eventNum).setTimeMin(now).setOrderBy("startTime").setSingleEvents(true).execute();
                List<Event> items = events.getItems();
                if (items.size() == 0) {
                    Message.sendMessage(MessageManager.getMessage("Event.List.Found.None", settings), event);
                } else if (items.size() == 1) {
                    Message.sendMessage(EventMessageFormatter.getEventEmbed(items.get(0), settings), MessageManager.getMessage("Event.List.Found.One", settings), event);
                } else {
                    // List events by Id only.
                    Message.sendMessage(MessageManager.getMessage("Event.List.Found.Many", "%amount%", items.size() + "", settings), event);
                    for (Event e : items) {
                        Message.sendMessage(EventMessageFormatter.getCondensedEventEmbed(e, settings), event);
                    }
                }
            } catch (Exception e) {
                Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
                Logger.getLogger().exception(event.getAuthor(), "Failed to list events.", e, this.getClass(), true);
                e.printStackTrace();
            }
        } catch (NumberFormatException e) {
            Message.sendMessage(MessageManager.getMessage("Notification.Args.Value.Integer", settings), event);
        }
    } else {
        Message.sendMessage(MessageManager.getMessage("Event.List.Args.Many", settings), event);
    }
}
Also used : Events(com.google.api.services.calendar.model.Events) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar) Event(com.google.api.services.calendar.model.Event) MessageReceivedEvent(sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent) ArrayList(java.util.ArrayList) List(java.util.List) DateTime(com.google.api.client.util.DateTime)

Example 5 with CalendarData

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

the class AddCalendarCommand 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) {
    if (settings.isDevGuild() || settings.isPatronGuild()) {
        if (PermissionChecker.hasManageServerRole(event)) {
            if (args.length == 0) {
                if (DatabaseManager.getManager().getMainCalendar(settings.getGuildID()).getCalendarAddress().equalsIgnoreCase("primary")) {
                    Message.sendMessage(MessageManager.getMessage("AddCalendar.Start", settings), event);
                    Authorization.getAuth().requestCode(event, settings);
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Calendar.HasCalendar", settings), event);
                }
            } else if (args.length == 1) {
                // Check if arg is calendar ID that is supported, if so, complete the setup.
                if (!DatabaseManager.getManager().getMainCalendar(settings.getGuildID()).getCalendarAddress().equalsIgnoreCase("primary")) {
                    Message.sendMessage(MessageManager.getMessage("Creator.Calendar.HasCalendar", settings), event);
                } else if (settings.getEncryptedAccessToken().equalsIgnoreCase("N/a") && settings.getEncryptedRefreshToken().equalsIgnoreCase("N/a")) {
                    Message.sendMessage(MessageManager.getMessage("AddCalendar.Select.NotAuth", settings), event);
                } else {
                    try {
                        Calendar service = CalendarAuth.getCalendarService(settings);
                        List<CalendarListEntry> items = service.calendarList().list().setMinAccessRole("writer").execute().getItems();
                        boolean valid = false;
                        for (CalendarListEntry i : items) {
                            if (!i.isDeleted() && i.getId().equals(args[0])) {
                                // valid
                                valid = true;
                                break;
                            }
                        }
                        if (valid) {
                            // Update and save.
                            CalendarData data = new CalendarData(event.getGuild().getLongID(), 1);
                            data.setCalendarId(args[0]);
                            data.setCalendarAddress(args[0]);
                            data.setExternal(true);
                            DatabaseManager.getManager().updateCalendar(data);
                            // Update guild settings
                            settings.setUseExternalCalendar(true);
                            DatabaseManager.getManager().updateSettings(settings);
                            Message.sendMessage(MessageManager.getMessage("AddCalendar.Select.Success", settings), event);
                        } else {
                            // Invalid
                            Message.sendMessage(MessageManager.getMessage("AddCalendar.Select.Failure.Invalid", settings), event);
                        }
                    } catch (Exception e) {
                        Message.sendMessage(MessageManager.getMessage("AddCalendar.Select.Failure.Unknown", settings), event);
                        Logger.getLogger().exception(event.getAuthor(), "Failed to connect external calendar!", e, this.getClass(), true);
                    }
                }
            } else {
                Message.sendMessage(MessageManager.getMessage("AddCalendar.Specify", settings), event);
            }
        } else {
            Message.sendMessage(MessageManager.getMessage("Notification.Perm.MANAGE_SERVER", settings), event);
        }
    } else {
        Message.sendMessage(MessageManager.getMessage("Notification.Patron", settings), event);
    }
    return false;
}
Also used : CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar)

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