Search in sources :

Example 1 with PreEvent

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

the class EventCommand method moduleRecur.

// Event recurrence settings
private void moduleRecur(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    long guildId = event.getGuild().getLongID();
    if (args.length == 2) {
        String valueString = args[1];
        if (EventCreator.getCreator().hasPreEvent(guildId)) {
            PreEvent pre = EventCreator.getCreator().getPreEvent(guildId);
            if (pre.isEditing() && pre.getEventId().contains("_")) {
                if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                    Message.deleteMessage(event);
                    Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                    EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.Recur.Failure.Child", "%id%", pre.getEventId().split("_")[0], settings), event));
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Event.Recur.Failure.Child", "%id%", pre.getEventId().split("_")[0], settings), event);
                }
                return;
            }
            try {
                boolean value = Boolean.valueOf(valueString);
                EventCreator.getCreator().getPreEvent(guildId).setShouldRecur(value);
                if (value) {
                    if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                        Message.deleteMessage(event);
                        Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                        EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.Recur.True", settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Event.Recur.True", settings), event);
                    }
                } else {
                    if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                        Message.deleteMessage(event);
                        Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                        EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.Recur.False", settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Event.Recur.False", settings), event);
                    }
                }
            } catch (Exception e) {
                // Could not convert to boolean
                if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                    Message.deleteMessage(event);
                    Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                    EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.Recur.Failure.Invalid", settings), event));
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Event.Recur.Failure.Invalid", settings), event);
                }
            }
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Event.NotInit", settings), event);
        }
    } else {
        if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
            Message.deleteMessage(event);
            Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
            EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.Recur.Specify", settings), event));
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Event.Recur.Specify", settings), event);
        }
    }
}
Also used : PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent) ParseException(java.text.ParseException)

Example 2 with PreEvent

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

the class EventCommand method moduleEdit.

private void moduleEdit(String[] args, MessageReceivedEvent event, CalendarData calendarData, GuildSettings settings) {
    long guildId = event.getGuild().getLongID();
    if (!calendarData.getCalendarAddress().equalsIgnoreCase("primary")) {
        if (!EventCreator.getCreator().hasPreEvent(guildId)) {
            if (args.length == 2) {
                String eventId = args[1];
                if (EventUtils.eventExists(settings, eventId)) {
                    PreEvent preEvent = EventCreator.getCreator().edit(event, eventId, settings, true);
                    if (preEvent.getCreatorMessage() == null) {
                        Message.sendMessage(EventMessageFormatter.getPreEventEmbed(preEvent, settings), MessageManager.getMessage("Creator.Event.Edit.Init", settings), event);
                    }
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Event.NotFound", settings), event);
                }
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Event.Edit.Specify", settings), event);
            }
        } else {
            if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                Message.deleteMessage(event);
                Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.AlreadyInit", settings), event));
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Event.AlreadyInit", settings), event);
            }
        }
    } else {
        Message.sendMessage(MessageManager.getMessage("Creator.Event.NoCalendar", settings), event);
    }
}
Also used : PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent)

Example 3 with PreEvent

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

the class CreatorCleaner method run.

@Override
public void run() {
    try {
        // 60 minutes
        long target = 60 * 1000 * 60;
        ArrayList<PreCalendar> cals = new ArrayList<>();
        ArrayList<PreEvent> events = new ArrayList<>();
        ArrayList<Announcement> ans = new ArrayList<>();
        // Run through calendar creator
        for (PreCalendar cal : CalendarCreator.getCreator().getAllPreCalendars()) {
            long difference = System.currentTimeMillis() - cal.getLastEdit();
            if (difference <= target) {
                // Last edited 60+ minutes ago, delete from creator and free up RAM.
                cals.add(cal);
            }
        }
        // Run through event creator
        for (PreEvent event : EventCreator.getCreator().getAllPreEvents()) {
            long difference = System.currentTimeMillis() - event.getLastEdit();
            if (difference <= target) {
                // Last edited 60+ minutes ago, delete from creator and free up RAM.
                events.add(event);
            }
        }
        // Run through announcement creator
        for (Announcement an : AnnouncementCreator.getCreator().getAllAnnouncements()) {
            long difference = System.currentTimeMillis() - an.getLastEdit();
            if (difference <= target) {
                // Last edited 60+ minutes ago, delete from creator and free up RAM.
                ans.add(an);
            }
        }
        // Okay, actually go through it all and delete
        for (PreCalendar c : cals) {
            CalendarCreator.getCreator().terminate(c.getGuildId());
        }
        for (PreEvent e : events) {
            EventCreator.getCreator().terminate(e.getGuildId());
        }
        for (Announcement a : ans) {
            AnnouncementCreator.getCreator().terminate(a.getGuildId());
        }
    } catch (Exception e) {
        Logger.getLogger().exception(null, "Error in CreatorCleaner", e, this.getClass(), true);
    }
}
Also used : PreCalendar(com.cloudcraftgaming.discal.api.object.calendar.PreCalendar) Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent) ArrayList(java.util.ArrayList)

Example 4 with PreEvent

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

the class EventCreator method confirmEvent.

/**
 * Confirms the event in the creator for the specific guild.
 *
 * @param e The event received upon confirmation.
 * @return The response containing detailed info about the confirmation.
 */
public EventCreatorResponse confirmEvent(MessageReceivedEvent e, GuildSettings settings) {
    if (hasPreEvent(e.getGuild().getLongID())) {
        long guildId = e.getGuild().getLongID();
        PreEvent preEvent = getPreEvent(guildId);
        if (preEvent.hasRequiredValues()) {
            Event event = new Event();
            event.setSummary(preEvent.getSummary());
            event.setDescription(preEvent.getDescription());
            event.setStart(preEvent.getStartDateTime().setTimeZone(preEvent.getTimeZone()));
            event.setEnd(preEvent.getEndDateTime().setTimeZone(preEvent.getTimeZone()));
            event.setVisibility("public");
            if (!preEvent.getColor().equals(EventColor.NONE)) {
                event.setColorId(String.valueOf(preEvent.getColor().getId()));
            }
            if (preEvent.getLocation() != null && !preEvent.getLocation().equalsIgnoreCase("")) {
                event.setLocation(preEvent.getLocation());
            }
            // Set recurrence
            if (preEvent.shouldRecur()) {
                String[] recurrence = new String[] { preEvent.getRecurrence().toRRule() };
                event.setRecurrence(Arrays.asList(recurrence));
            }
            // TODO handle multiple calendars...
            String calendarId = DatabaseManager.getManager().getMainCalendar(guildId).getCalendarAddress();
            if (!preEvent.isEditing()) {
                event.setId(KeyGenerator.generateEventId());
                try {
                    Event confirmed;
                    if (settings.useExternalCalendar()) {
                        confirmed = CalendarAuth.getCalendarService(settings).events().insert(calendarId, event).execute();
                    } else {
                        confirmed = CalendarAuth.getCalendarService().events().insert(calendarId, event).execute();
                    }
                    if (preEvent.getEventData().shouldBeSaved()) {
                        preEvent.getEventData().setEventId(confirmed.getId());
                        preEvent.getEventData().setEventEnd(confirmed.getEnd().getDateTime().getValue());
                        DatabaseManager.getManager().updateEventData(preEvent.getEventData());
                    }
                    terminate(e);
                    EventCreatorResponse response = new EventCreatorResponse(true, confirmed);
                    response.setEdited(false);
                    return response;
                } catch (Exception ex) {
                    Logger.getLogger().exception(e.getAuthor(), "Failed to create event.", ex, this.getClass(), true);
                    EventCreatorResponse response = new EventCreatorResponse(false);
                    response.setEdited(false);
                    return response;
                }
            } else {
                try {
                    Event confirmed;
                    if (settings.useExternalCalendar()) {
                        confirmed = CalendarAuth.getCalendarService(settings).events().update(calendarId, preEvent.getEventId(), event).execute();
                    } else {
                        confirmed = CalendarAuth.getCalendarService().events().update(calendarId, preEvent.getEventId(), event).execute();
                    }
                    if (preEvent.getEventData().shouldBeSaved()) {
                        preEvent.getEventData().setEventId(confirmed.getId());
                        preEvent.getEventData().setEventEnd(confirmed.getEnd().getDateTime().getValue());
                        DatabaseManager.getManager().updateEventData(preEvent.getEventData());
                    }
                    terminate(e);
                    EventCreatorResponse response = new EventCreatorResponse(true, confirmed);
                    response.setEdited(true);
                    return response;
                } catch (Exception ex) {
                    Logger.getLogger().exception(e.getAuthor(), "Failed to update event.", ex, this.getClass(), true);
                    EventCreatorResponse response = new EventCreatorResponse(false);
                    response.setEdited(true);
                    return response;
                }
            }
        }
    }
    return new EventCreatorResponse(false);
}
Also used : EventCreatorResponse(com.cloudcraftgaming.discal.api.object.event.EventCreatorResponse) PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent) Event(com.google.api.services.calendar.model.Event) MessageReceivedEvent(sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent) PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent) IOException(java.io.IOException)

Example 5 with PreEvent

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

the class EventCreator method edit.

public PreEvent edit(MessageReceivedEvent e, String eventId, GuildSettings settings, boolean handleMessage) {
    long guildId = e.getGuild().getLongID();
    if (!hasPreEvent(guildId)) {
        // TODO: Handle multiple calendars...
        try {
            String calId = DatabaseManager.getManager().getMainCalendar(guildId).getCalendarAddress();
            Calendar service;
            if (settings.useExternalCalendar()) {
                service = CalendarAuth.getCalendarService(settings);
            } else {
                service = CalendarAuth.getCalendarService();
            }
            Event calEvent = service.events().get(calId, eventId).execute();
            PreEvent event = new PreEvent(guildId, calEvent);
            event.setEditing(true);
            try {
                event.setTimeZone(service.calendars().get(calId).execute().getTimeZone());
            } catch (IOException e1) {
            // Failed to get tz, ignore safely.
            }
            if (handleMessage) {
                if (PermissionChecker.botHasMessageManagePerms(e)) {
                    IMessage message = Message.sendMessage(EventMessageFormatter.getPreEventEmbed(event, settings), MessageManager.getMessage("Creator.Event.Edit.Init", settings), e);
                    event.setCreatorMessage(message);
                    Message.deleteMessage(e);
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Notif.MANAGE_MESSAGES", settings), e);
                }
            }
            events.add(event);
            return event;
        } catch (Exception exc) {
        // Oops
        }
        return null;
    }
    return getPreEvent(guildId);
}
Also used : PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent) Calendar(com.google.api.services.calendar.Calendar) IMessage(sx.blah.discord.handle.obj.IMessage) Event(com.google.api.services.calendar.model.Event) MessageReceivedEvent(sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent) PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

PreEvent (com.cloudcraftgaming.discal.api.object.event.PreEvent)10 IOException (java.io.IOException)4 IMessage (sx.blah.discord.handle.obj.IMessage)3 Event (com.google.api.services.calendar.model.Event)2 MessageReceivedEvent (sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent)2 Announcement (com.cloudcraftgaming.discal.api.object.announcement.Announcement)1 PreCalendar (com.cloudcraftgaming.discal.api.object.calendar.PreCalendar)1 EventCreatorResponse (com.cloudcraftgaming.discal.api.object.event.EventCreatorResponse)1 Calendar (com.google.api.services.calendar.Calendar)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1