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);
}
}
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations