Search in sources :

Example 1 with CalendarCreatorResponse

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

the class CalendarCommand method moduleConfirm.

private void moduleConfirm(MessageReceivedEvent event, CalendarData calendarData, GuildSettings settings) {
    long guildId = event.getGuild().getLongID();
    if (CalendarCreator.getCreator().hasPreCalendar(guildId)) {
        CalendarCreatorResponse response = CalendarCreator.getCreator().confirmCalendar(event, settings);
        if (response.isSuccessful()) {
            if (response.isEdited()) {
                if (response.getCreatorMessage() != null) {
                    Message.deleteMessage(event);
                    Message.deleteMessage(CalendarCreator.getCreator().getCreatorMessage(guildId));
                    CalendarCreator.getCreator().setCreatorMessage(Message.sendMessage(CalendarMessageFormatter.getCalendarLinkEmbed(response.getCalendar(), settings), MessageManager.getMessage("Creator.Calendar.Confirm.Edit.Success", settings), event));
                } else {
                    Message.sendMessage(CalendarMessageFormatter.getCalendarLinkEmbed(response.getCalendar(), settings), MessageManager.getMessage("Creator.Calendar.Confirm.Edit.Success", settings), event);
                }
            } else {
                if (response.getCalendar() != null) {
                    Message.deleteMessage(event);
                    Message.deleteMessage(CalendarCreator.getCreator().getCreatorMessage(guildId));
                    CalendarCreator.getCreator().setCreatorMessage(Message.sendMessage(CalendarMessageFormatter.getCalendarLinkEmbed(response.getCalendar(), settings), MessageManager.getMessage("Creator.Calendar.Confirm.Create.Success", settings), event));
                } else {
                    Message.sendMessage(CalendarMessageFormatter.getCalendarLinkEmbed(response.getCalendar(), settings), MessageManager.getMessage("Creator.Calendar.Confirm.Create.Success", settings), event);
                }
            }
        } else {
            if (response.isEdited()) {
                if (response.getCreatorMessage() != null) {
                    Message.deleteMessage(event);
                    Message.deleteMessage(CalendarCreator.getCreator().getCreatorMessage(guildId));
                    CalendarCreator.getCreator().setCreatorMessage(Message.sendMessage(CalendarMessageFormatter.getPreCalendarEmbed(CalendarCreator.getCreator().getPreCalendar(guildId), settings), MessageManager.getMessage("Creator.Calendar.Confirm.Edit.Failure", settings), event));
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Calendar.Confirm.Edit.Failure", settings), event);
                }
            } else {
                if (response.getCreatorMessage() != null) {
                    Message.deleteMessage(event);
                    Message.deleteMessage(CalendarCreator.getCreator().getCreatorMessage(guildId));
                    CalendarCreator.getCreator().setCreatorMessage(Message.sendMessage(CalendarMessageFormatter.getPreCalendarEmbed(CalendarCreator.getCreator().getPreCalendar(guildId), settings), MessageManager.getMessage("Creator.Calendar.Confirm.Create.Failure", settings), event));
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Calendar.Confirm.Create.Failure", settings), event);
                }
            }
        }
    } else {
        if (calendarData.getCalendarId().equalsIgnoreCase("primary")) {
            Message.sendMessage(MessageManager.getMessage("Creator.Calendar.NoCalendar", settings), event);
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Calendar.HasCalendar", settings), event);
        }
    }
}
Also used : CalendarCreatorResponse(com.cloudcraftgaming.discal.api.object.calendar.CalendarCreatorResponse)

Example 2 with CalendarCreatorResponse

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

the class CalendarCreator method confirmCalendar.

/**
 * Confirms the calendar and creates it within Google Calendar.
 *
 * @param e The event received upon confirmation.
 * @return A CalendarCreatorResponse Object with detailed info about the confirmation.
 */
public CalendarCreatorResponse confirmCalendar(MessageReceivedEvent e, GuildSettings settings) {
    if (hasPreCalendar(e.getMessage().getGuild().getLongID())) {
        long guildId = e.getMessage().getGuild().getLongID();
        PreCalendar preCalendar = getPreCalendar(guildId);
        if (preCalendar.hasRequiredValues()) {
            if (!preCalendar.isEditing()) {
                Calendar calendar = new Calendar();
                calendar.setSummary(preCalendar.getSummary());
                calendar.setDescription(preCalendar.getDescription());
                calendar.setTimeZone(preCalendar.getTimezone());
                try {
                    com.google.api.services.calendar.Calendar service;
                    if (settings.useExternalCalendar()) {
                        service = CalendarAuth.getCalendarService(settings);
                    } else {
                        service = CalendarAuth.getCalendarService();
                    }
                    Calendar confirmed = service.calendars().insert(calendar).execute();
                    AclRule rule = new AclRule();
                    AclRule.Scope scope = new AclRule.Scope();
                    scope.setType("default");
                    rule.setScope(scope).setRole("reader");
                    service.acl().insert(confirmed.getId(), rule).execute();
                    CalendarData calendarData = new CalendarData(guildId, 1);
                    calendarData.setCalendarId(confirmed.getId());
                    calendarData.setCalendarAddress(confirmed.getId());
                    DatabaseManager.getManager().updateCalendar(calendarData);
                    terminate(e);
                    CalendarCreatorResponse response = new CalendarCreatorResponse(true, confirmed);
                    response.setEdited(false);
                    response.setCreatorMessage(preCalendar.getCreatorMessage());
                    return response;
                } catch (Exception ex) {
                    Logger.getLogger().exception(e.getMessage().getAuthor(), "Failed to confirm calendar.", ex, this.getClass(), true);
                    CalendarCreatorResponse response = new CalendarCreatorResponse(false);
                    response.setEdited(false);
                    response.setCreatorMessage(preCalendar.getCreatorMessage());
                    return response;
                }
            } else {
                // Editing calendar...
                Calendar calendar = new Calendar();
                calendar.setSummary(preCalendar.getSummary());
                calendar.setDescription(preCalendar.getDescription());
                calendar.setTimeZone(preCalendar.getTimezone());
                try {
                    com.google.api.services.calendar.Calendar service;
                    if (settings.useExternalCalendar()) {
                        service = CalendarAuth.getCalendarService(settings);
                    } else {
                        service = CalendarAuth.getCalendarService();
                    }
                    Calendar confirmed = service.calendars().update(preCalendar.getCalendarId(), calendar).execute();
                    AclRule rule = new AclRule();
                    AclRule.Scope scope = new AclRule.Scope();
                    scope.setType("default");
                    rule.setScope(scope).setRole("reader");
                    service.acl().insert(confirmed.getId(), rule).execute();
                    terminate(e);
                    CalendarCreatorResponse response = new CalendarCreatorResponse(true, confirmed);
                    response.setEdited(true);
                    response.setCreatorMessage(preCalendar.getCreatorMessage());
                    return response;
                } catch (Exception ex) {
                    Logger.getLogger().exception(e.getMessage().getAuthor(), "Failed to update calendar.", ex, this.getClass(), true);
                    CalendarCreatorResponse response = new CalendarCreatorResponse(false);
                    response.setEdited(true);
                    response.setCreatorMessage(preCalendar.getCreatorMessage());
                    return response;
                }
            }
        }
    }
    return new CalendarCreatorResponse(false);
}
Also used : PreCalendar(com.cloudcraftgaming.discal.api.object.calendar.PreCalendar) PreCalendar(com.cloudcraftgaming.discal.api.object.calendar.PreCalendar) Calendar(com.google.api.services.calendar.model.Calendar) CalendarCreatorResponse(com.cloudcraftgaming.discal.api.object.calendar.CalendarCreatorResponse) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) AclRule(com.google.api.services.calendar.model.AclRule)

Aggregations

CalendarCreatorResponse (com.cloudcraftgaming.discal.api.object.calendar.CalendarCreatorResponse)2 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)1 PreCalendar (com.cloudcraftgaming.discal.api.object.calendar.PreCalendar)1 AclRule (com.google.api.services.calendar.model.AclRule)1 Calendar (com.google.api.services.calendar.model.Calendar)1