mirror of
https://github.com/DreamExposure/DisCal-Discord-Bot.git
synced 2026-02-14 23:48:26 -06:00
Better package sorting.
This commit is contained in:
@@ -5,10 +5,10 @@ import com.cloudcraftgaming.discal.api.file.ReadFile;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.network.google.Authorization;
|
||||
import com.cloudcraftgaming.discal.api.object.BotSettings;
|
||||
import com.cloudcraftgaming.discal.eventlisteners.ReadyEventListener;
|
||||
import com.cloudcraftgaming.discal.internal.consolecommand.ConsoleCommandExecutor;
|
||||
import com.cloudcraftgaming.discal.internal.network.discordpw.UpdateListData;
|
||||
import com.cloudcraftgaming.discal.module.command.*;
|
||||
import com.cloudcraftgaming.discal.bot.internal.consolecommand.ConsoleCommandExecutor;
|
||||
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.UpdateListData;
|
||||
import com.cloudcraftgaming.discal.bot.listeners.ReadyEventListener;
|
||||
import com.cloudcraftgaming.discal.bot.module.command.*;
|
||||
import com.cloudcraftgaming.discal.web.endpoints.v1.AnnouncementEndpoint;
|
||||
import com.cloudcraftgaming.discal.web.endpoints.v1.CalendarEndpoint;
|
||||
import com.cloudcraftgaming.discal.web.endpoints.v1.GuildEndpoint;
|
||||
|
||||
@@ -15,7 +15,7 @@ import com.cloudcraftgaming.discal.api.object.json.google.CodeResponse;
|
||||
import com.cloudcraftgaming.discal.api.object.network.google.ClientData;
|
||||
import com.cloudcraftgaming.discal.api.object.network.google.Poll;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.calendar.CalendarMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.calendar.CalendarMessageFormatter;
|
||||
import com.google.api.client.http.HttpStatusCodes;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.CalendarScopes;
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.calendar.calendar;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.message.Message;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarCreatorResponse;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.PreCalendar;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import com.google.api.services.calendar.model.AclRule;
|
||||
import com.google.api.services.calendar.model.Calendar;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/4/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class CalendarCreator {
|
||||
private static CalendarCreator instance;
|
||||
|
||||
private ArrayList<PreCalendar> calendars = new ArrayList<>();
|
||||
|
||||
private CalendarCreator() {
|
||||
} //Prevent initialization
|
||||
|
||||
/**
|
||||
* Gets the instance of the CalendarCreator.
|
||||
*
|
||||
* @return The instance of the CalendarCreator.
|
||||
*/
|
||||
public static CalendarCreator getCreator() {
|
||||
if (instance == null) {
|
||||
instance = new CalendarCreator();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
//Functionals
|
||||
|
||||
/**
|
||||
* Initiates the CalendarCreator for the guild involved in the event.
|
||||
*
|
||||
* @param e The event received upon creation start.
|
||||
* @param calendarName The name of the calendar to create.
|
||||
* @return The PreCalendar object created.
|
||||
*/
|
||||
public PreCalendar init(MessageReceivedEvent e, String calendarName, GuildSettings settings, boolean handleCreatorMessage) {
|
||||
long guildId = e.getMessage().getGuild().getLongID();
|
||||
if (!hasPreCalendar(guildId)) {
|
||||
PreCalendar calendar = new PreCalendar(guildId, calendarName);
|
||||
|
||||
if (handleCreatorMessage) {
|
||||
if (PermissionChecker.botHasMessageManagePerms(e)) {
|
||||
IMessage msg = Message.sendMessage(CalendarMessageFormatter.getPreCalendarEmbed(calendar, settings), MessageManager.getMessage("Creator.Calendar.Create.Init", settings), e);
|
||||
calendar.setCreatorMessage(msg);
|
||||
} else {
|
||||
Message.sendMessage(MessageManager.getMessage("Creator.Notif.MANAGE_MESSAGES", settings), e);
|
||||
}
|
||||
}
|
||||
calendars.add(calendar);
|
||||
return calendar;
|
||||
}
|
||||
return getPreCalendar(e.getMessage().getGuild().getLongID());
|
||||
}
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
public PreCalendar edit(MessageReceivedEvent event, GuildSettings settings, boolean handleCreatorMessage) {
|
||||
long guildId = event.getMessage().getGuild().getLongID();
|
||||
if (!hasPreCalendar(guildId)) {
|
||||
//TODO: Support multiple calendars
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(guildId);
|
||||
|
||||
try {
|
||||
com.google.api.services.calendar.Calendar service;
|
||||
if (settings.useExternalCalendar()) {
|
||||
service = CalendarAuth.getCalendarService(settings);
|
||||
} else {
|
||||
service = CalendarAuth.getCalendarService();
|
||||
}
|
||||
|
||||
Calendar calendar = service.calendars().get(data.getCalendarAddress()).execute();
|
||||
|
||||
PreCalendar preCalendar = new PreCalendar(guildId, calendar);
|
||||
preCalendar.setEditing(true);
|
||||
preCalendar.setCalendarId(data.getCalendarAddress());
|
||||
|
||||
if (handleCreatorMessage) {
|
||||
if (PermissionChecker.botHasMessageManagePerms(event)) {
|
||||
IMessage msg = Message.sendMessage(CalendarMessageFormatter.getPreCalendarEmbed(preCalendar, settings), MessageManager.getMessage("Creator.Calendar.Edit.Init", settings), event);
|
||||
preCalendar.setCreatorMessage(msg);
|
||||
} else {
|
||||
Message.sendMessage(MessageManager.getMessage("Creator.Notif.MANAGE_MESSAGES", settings), event);
|
||||
}
|
||||
}
|
||||
|
||||
calendars.add(preCalendar);
|
||||
return preCalendar;
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(event.getMessage().getAuthor(), "Failed to init calendar editor", e, this.getClass());
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return getPreCalendar(guildId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully closes down the CalendarCreator for the guild involved and DOES NOT create the calendar.
|
||||
*
|
||||
* @param e The event received upon termination.
|
||||
* @return <codfe>true</codfe> if closed successfully, otherwise <code>false</code>.
|
||||
*/
|
||||
public Boolean terminate(MessageReceivedEvent e) {
|
||||
if (hasPreCalendar(e.getMessage().getGuild().getLongID())) {
|
||||
calendars.remove(getPreCalendar(e.getMessage().getGuild().getLongID()));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
ExceptionHandler.sendException(e.getMessage().getAuthor(), "Failed to confirm calendar.", ex, this.getClass());
|
||||
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) {
|
||||
ExceptionHandler.sendException(e.getMessage().getAuthor(), "Failed to update calendar.", ex, this.getClass());
|
||||
CalendarCreatorResponse response = new CalendarCreatorResponse(false);
|
||||
response.setEdited(true);
|
||||
response.setCreatorMessage(preCalendar.getCreatorMessage());
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new CalendarCreatorResponse(false);
|
||||
}
|
||||
|
||||
//Getters
|
||||
|
||||
/**
|
||||
* Gets the PreCalendar for the guild in the creator.
|
||||
*
|
||||
* @param guildId The ID of the guild whose PreCalendar is to be returned.
|
||||
* @return The PreCalendar belonging to the guild.
|
||||
*/
|
||||
public PreCalendar getPreCalendar(long guildId) {
|
||||
for (PreCalendar c : calendars) {
|
||||
if (c.getGuildId() == guildId) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IMessage getCreatorMessage(long guildId) {
|
||||
if (hasPreCalendar(guildId)) {
|
||||
return getPreCalendar(guildId).getCreatorMessage();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
|
||||
/**
|
||||
* Checks whether or not the specified Guild has a PreCalendar in the creator.
|
||||
*
|
||||
* @param guildId The ID of the guild to check for.
|
||||
* @return <code>true</code> if a PreCalendar exists, else <code>false</code>.
|
||||
*/
|
||||
public Boolean hasPreCalendar(long guildId) {
|
||||
for (PreCalendar c : calendars) {
|
||||
if (c.getGuildId() == guildId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasCreatorMessage(long guildId) {
|
||||
return hasPreCalendar(guildId) && getPreCalendar(guildId).getCreatorMessage() != null;
|
||||
}
|
||||
|
||||
//Setters
|
||||
public void setCreatorMessage(IMessage msg) {
|
||||
if (msg != null) {
|
||||
if (hasPreCalendar(msg.getGuild().getLongID())) {
|
||||
getPreCalendar(msg.getGuild().getLongID()).setCreatorMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.calendar.calendar;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.PreCalendar;
|
||||
import com.google.api.services.calendar.model.Calendar;
|
||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||
import sx.blah.discord.util.EmbedBuilder;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/4/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class CalendarMessageFormatter {
|
||||
public static String getCalendarLink(String calId) {
|
||||
URI callURI = URI.create(calId);
|
||||
return "https://calendar.google.com/calendar/embed?src=" + callURI;
|
||||
}
|
||||
|
||||
public static EmbedObject getCalendarLinkEmbed(Calendar cal, GuildSettings settings) {
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Calendar.Link.Title", settings));
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Link.Summary", settings), cal.getSummary(), true);
|
||||
try {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Link.Description", settings), cal.getDescription(), true);
|
||||
} catch (NullPointerException | IllegalArgumentException e) {
|
||||
//Some error, desc probably never set, just ignore no need to log.
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Link.TimeZone", settings), cal.getTimeZone(), false);
|
||||
em.withUrl(CalendarMessageFormatter.getCalendarLink(cal.getId()));
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Calendar.Link.CalendarId", "%id%", cal.getId(), settings));
|
||||
em.withColor(56, 138, 237);
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an EmbedObject for the PreCalendar.
|
||||
*
|
||||
* @param calendar The PreCalendar to create an EmbedObject for.
|
||||
* @return The EmbedObject for the PreCalendar.
|
||||
*/
|
||||
public static EmbedObject getPreCalendarEmbed(PreCalendar calendar, GuildSettings settings) {
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Calendar.Pre.Title", settings));
|
||||
if (calendar.getSummary() != null) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.Summary", settings), calendar.getSummary(), true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.Summary", settings), "***UNSET***", true);
|
||||
}
|
||||
if (calendar.getDescription() != null) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.Description", settings), calendar.getDescription(), false);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.Description", settings), "***UNSET***", false);
|
||||
}
|
||||
if (calendar.getTimezone() != null) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.TimeZone", settings), calendar.getTimezone(), true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.TimeZone", settings), "***UNSET***", true);
|
||||
}
|
||||
if (calendar.isEditing()) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.CalendarId", settings), calendar.getCalendarId(), false);
|
||||
} //No else needed, just don't post it.
|
||||
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Calendar.Pre.Key", settings));
|
||||
em.withColor(56, 138, 237);
|
||||
|
||||
return em.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,355 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.calendar.event;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.enums.event.EventColor;
|
||||
import com.cloudcraftgaming.discal.api.message.Message;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.event.EventCreatorResponse;
|
||||
import com.cloudcraftgaming.discal.api.object.event.PreEvent;
|
||||
import com.cloudcraftgaming.discal.api.utils.EventUtils;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.Event;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/3/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class EventCreator {
|
||||
private static EventCreator instance;
|
||||
|
||||
private ArrayList<PreEvent> events = new ArrayList<>();
|
||||
|
||||
private EventCreator() {
|
||||
} //Prevent initialization.
|
||||
|
||||
/**
|
||||
* Gets the instance of the EventCreator.
|
||||
*
|
||||
* @return The instance of the EventCreator
|
||||
*/
|
||||
public static EventCreator getCreator() {
|
||||
if (instance == null) {
|
||||
instance = new EventCreator();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
//Functionals
|
||||
|
||||
/**
|
||||
* Initiates the EventCreator for a specific guild.
|
||||
*
|
||||
* @param e The event received upon initialization.
|
||||
* @return The PreEvent for the guild.
|
||||
*/
|
||||
public PreEvent init(MessageReceivedEvent e, GuildSettings settings, boolean handleMessage) {
|
||||
if (!hasPreEvent(e.getGuild().getLongID())) {
|
||||
PreEvent event = new PreEvent(e.getGuild().getLongID());
|
||||
try {
|
||||
|
||||
//TODO: Handle multiple calendars...
|
||||
String calId = DatabaseManager.getManager().getMainCalendar(e.getGuild().getLongID()).getCalendarAddress();
|
||||
event.setTimeZone(CalendarAuth.getCalendarService().calendars().get(calId).execute().getTimeZone());
|
||||
} catch (IOException exc) {
|
||||
//Failed to get timezone, ignore safely.
|
||||
}
|
||||
if (handleMessage) {
|
||||
if (PermissionChecker.botHasMessageManagePerms(e)) {
|
||||
IMessage message = Message.sendMessage(EventMessageFormatter.getPreEventEmbed(event, settings), MessageManager.getMessage("Creator.Event.Create.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;
|
||||
}
|
||||
return getPreEvent(e.getGuild().getLongID());
|
||||
}
|
||||
|
||||
public PreEvent init(MessageReceivedEvent e, GuildSettings settings, String summary, boolean handleMessage) {
|
||||
if (!hasPreEvent(e.getGuild().getLongID())) {
|
||||
PreEvent event = new PreEvent(e.getGuild().getLongID());
|
||||
event.setSummary(summary);
|
||||
try {
|
||||
|
||||
//TODO: Handle multiple calendars...
|
||||
String calId = DatabaseManager.getManager().getMainCalendar(e.getGuild().getLongID()).getCalendarAddress();
|
||||
if (!settings.useExternalCalendar()) {
|
||||
event.setTimeZone(CalendarAuth.getCalendarService().calendars().get(calId).execute().getTimeZone());
|
||||
} else {
|
||||
event.setTimeZone(CalendarAuth.getCalendarService(settings).calendars().get(calId).execute().getTimeZone());
|
||||
}
|
||||
} catch (Exception exc) {
|
||||
//Failed to get timezone, ignore safely.
|
||||
}
|
||||
if (handleMessage) {
|
||||
if (PermissionChecker.botHasMessageManagePerms(e)) {
|
||||
IMessage message = Message.sendMessage(EventMessageFormatter.getPreEventEmbed(event, settings), MessageManager.getMessage("Creator.Event.Create.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;
|
||||
}
|
||||
return getPreEvent(e.getGuild().getLongID());
|
||||
}
|
||||
|
||||
//Copy event
|
||||
public PreEvent init(MessageReceivedEvent e, String eventId, GuildSettings settings, boolean handleMessage) {
|
||||
if (!hasPreEvent(e.getGuild().getLongID())) {
|
||||
//TODO: Handle multiple calendars...
|
||||
try {
|
||||
String calId = DatabaseManager.getManager().getMainCalendar(e.getGuild().getLongID()).getCalendarAddress();
|
||||
Calendar service;
|
||||
if (settings.useExternalCalendar()) {
|
||||
service = CalendarAuth.getCalendarService(settings);
|
||||
} else {
|
||||
service = CalendarAuth.getCalendarService();
|
||||
}
|
||||
Event calEvent = service.events().get(calId, eventId).execute();
|
||||
|
||||
PreEvent event = EventUtils.copyEvent(e.getGuild().getLongID(), calEvent);
|
||||
|
||||
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.Copy.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) {
|
||||
//Something failed...
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return getPreEvent(e.getGuild().getLongID());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully terminates the EventCreator for a specific guild.
|
||||
*
|
||||
* @param e The event received upon termination.
|
||||
* @return <code>true</code> if successful, else <code>false</code>.
|
||||
*/
|
||||
public Boolean terminate(MessageReceivedEvent e) {
|
||||
if (hasPreEvent(e.getGuild().getLongID())) {
|
||||
events.remove(getPreEvent(e.getGuild().getLongID()));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()) {
|
||||
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) {
|
||||
ExceptionHandler.sendException(e.getAuthor(), "Failed to create event.", ex, this.getClass());
|
||||
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) {
|
||||
ExceptionHandler.sendException(e.getAuthor(), "Failed to update event.", ex, this.getClass());
|
||||
EventCreatorResponse response = new EventCreatorResponse(false);
|
||||
response.setEdited(true);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new EventCreatorResponse(false);
|
||||
}
|
||||
|
||||
//Getters
|
||||
|
||||
/**
|
||||
* gets the PreEvent for the specified guild.
|
||||
*
|
||||
* @param guildId The ID of the guild.
|
||||
* @return The PreEvent belonging to the guild.
|
||||
*/
|
||||
public PreEvent getPreEvent(long guildId) {
|
||||
for (PreEvent e : events) {
|
||||
if (e.getGuildId() == guildId) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IMessage getCreatorMessage(long guildId) {
|
||||
if (hasPreEvent(guildId)) {
|
||||
return getPreEvent(guildId).getCreatorMessage();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
|
||||
/**
|
||||
* Checks if the specified guild has a PreEvent in the creator.
|
||||
*
|
||||
* @param guildId The ID of the guild.
|
||||
* @return <code>true</code> if a PreEvent exists, otherwise <code>false</code>.
|
||||
*/
|
||||
public Boolean hasPreEvent(long guildId) {
|
||||
for (PreEvent e : events) {
|
||||
if (e.getGuildId() == guildId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasCreatorMessage(long guildId) {
|
||||
return hasPreEvent(guildId) && getPreEvent(guildId).getCreatorMessage() != null;
|
||||
}
|
||||
|
||||
//Setters
|
||||
public void setCreatorMessage(IMessage msg) {
|
||||
if (msg != null) {
|
||||
if (hasPreEvent(msg.getGuild().getLongID())) {
|
||||
getPreEvent(msg.getGuild().getLongID()).setCreatorMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,382 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.calendar.event;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.enums.event.EventColor;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.event.EventCreatorResponse;
|
||||
import com.cloudcraftgaming.discal.api.object.event.EventData;
|
||||
import com.cloudcraftgaming.discal.api.object.event.PreEvent;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.api.utils.ImageUtils;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.Event;
|
||||
import com.google.api.services.calendar.model.EventDateTime;
|
||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||
import sx.blah.discord.util.EmbedBuilder;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/3/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class EventMessageFormatter {
|
||||
|
||||
/**
|
||||
* Gets an EmbedObject for the specified event.
|
||||
*
|
||||
* @param event The event involved.
|
||||
* @param settings The guild's settings
|
||||
* @return The EmbedObject of the event.
|
||||
*/
|
||||
public static EmbedObject getEventEmbed(Event event, GuildSettings settings) {
|
||||
EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), event.getId());
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Event.Info.Title", settings));
|
||||
if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
|
||||
em.withImage(ed.getImageLink());
|
||||
}
|
||||
if (event.getSummary() != null) {
|
||||
String summary = event.getSummary();
|
||||
if (summary.length() > 250) {
|
||||
summary = summary.substring(0, 250);
|
||||
summary = summary + " (continues on Google Calendar View)";
|
||||
}
|
||||
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.Summary", settings), summary, true);
|
||||
}
|
||||
if (event.getDescription() != null) {
|
||||
String description = event.getDescription();
|
||||
if (description.length() > 500) {
|
||||
description = description.substring(0, 500);
|
||||
description = description + " (continues on Google Calendar View)";
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.Description", settings), description, true);
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.StartDate", settings), getHumanReadableDate(event.getStart(), settings, false), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.StartTime", settings), getHumanReadableTime(event.getStart(), settings, false), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.EndDate", settings), getHumanReadableDate(event.getEnd(), settings, false), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.EndTime", settings), getHumanReadableTime(event.getEnd(), settings, false), true);
|
||||
|
||||
try {
|
||||
//TODO: add support for multiple calendars...
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
|
||||
Calendar service;
|
||||
service = settings.useExternalCalendar() ? CalendarAuth.getCalendarService(settings) : CalendarAuth.getCalendarService();
|
||||
String tz = service.calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.TimeZone", settings), tz, true);
|
||||
} catch (Exception e1) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.TimeZone", settings), "Error/Unknown", true);
|
||||
}
|
||||
if (event.getLocation() != null && !event.getLocation().equalsIgnoreCase("")) {
|
||||
if (event.getLocation().length() > 300) {
|
||||
String location = event.getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), event.getLocation(), true);
|
||||
}
|
||||
}
|
||||
//TODO: Add info on recurrence here.
|
||||
em.withUrl(event.getHtmlLink());
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Event.Info.ID", "%id%", event.getId(), settings));
|
||||
try {
|
||||
EventColor ec = EventColor.fromId(Integer.valueOf(event.getColorId()));
|
||||
em.withColor(ec.getR(), ec.getG(), ec.getB());
|
||||
} catch (Exception e) {
|
||||
//Color is null, ignore and add our default.
|
||||
em.withColor(56, 138, 237);
|
||||
}
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an EmbedObject for the specified event.
|
||||
*
|
||||
* @param event The event involved.
|
||||
* @return The EmbedObject of the event.
|
||||
*/
|
||||
public static EmbedObject getCondensedEventEmbed(Event event, GuildSettings settings) {
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Event.Condensed.Title", settings));
|
||||
EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), event.getId());
|
||||
if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
|
||||
em.withThumbnail(ed.getImageLink());
|
||||
}
|
||||
if (event.getSummary() != null) {
|
||||
String summary = event.getSummary();
|
||||
if (summary.length() > 250) {
|
||||
summary = summary.substring(0, 250);
|
||||
summary = summary + " (continues on Google Calendar View)";
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Condensed.Summary", settings), summary, true);
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Condensed.Date", settings), getHumanReadableDate(event.getStart(), settings, false), true);
|
||||
if (event.getLocation() != null && !event.getLocation().equalsIgnoreCase("")) {
|
||||
if (event.getLocation().length() > 300) {
|
||||
String location = event.getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), event.getLocation(), true);
|
||||
}
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Condensed.ID", settings), event.getId(), false);
|
||||
em.withUrl(event.getHtmlLink());
|
||||
try {
|
||||
EventColor ec = EventColor.fromId(Integer.valueOf(event.getColorId()));
|
||||
em.withColor(ec.getR(), ec.getG(), ec.getB());
|
||||
} catch (Exception e) {
|
||||
//Color is null, ignore and add our default.
|
||||
em.withColor(56, 138, 237);
|
||||
}
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an EmbedObject for the specified PreEvent.
|
||||
*
|
||||
* @param event The PreEvent to get an embed for.
|
||||
* @return The EmbedObject of the PreEvent.
|
||||
*/
|
||||
public static EmbedObject getPreEventEmbed(PreEvent event, GuildSettings settings) {
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Event.Pre.Title", settings));
|
||||
if (event.getEventData() != null && event.getEventData().getImageLink() != null && ImageUtils.validate(event.getEventData().getImageLink())) {
|
||||
em.withImage(event.getEventData().getImageLink());
|
||||
}
|
||||
if (event.isEditing()) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Id", settings), event.getEventId(), false);
|
||||
}
|
||||
if (event.getSummary() != null) {
|
||||
String summary = event.getSummary();
|
||||
if (summary.length() > 250) {
|
||||
summary = summary.substring(0, 250);
|
||||
summary = summary + " (continues on Google Calendar View)";
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Summary", settings), summary, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Summary", settings), "NOT SET", true);
|
||||
}
|
||||
if (event.getDescription() != null) {
|
||||
String description = event.getDescription();
|
||||
if (description.length() > 500) {
|
||||
description = description.substring(0, 500);
|
||||
description = description + " (continues on Google Calendar View)";
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Description", settings), description, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Description", settings), "NOT SET", true);
|
||||
}
|
||||
if (event.shouldRecur()) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Recurrence", settings), event.getRecurrence().toHumanReadable(), true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Recurrence", settings), "N/a", true);
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.StartDate", settings), getHumanReadableDate(event.getViewableStartDate(), settings, true), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.StartTime", settings), EventMessageFormatter.getHumanReadableTime(event.getViewableStartDate(), settings, true), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.EndDate", settings), getHumanReadableDate(event.getViewableEndDate(), settings, true), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.EndTime", settings), EventMessageFormatter.getHumanReadableTime(event.getViewableEndDate(), settings, true), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.TimeZone", settings), event.getTimeZone(), true);
|
||||
|
||||
if (event.getLocation() != null && !event.getLocation().equalsIgnoreCase("")) {
|
||||
if (event.getLocation().length() > 300) {
|
||||
String location = event.getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), event.getLocation(), true);
|
||||
}
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), "N/a", true);
|
||||
}
|
||||
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Event.Pre.Key", settings));
|
||||
EventColor ec = event.getColor();
|
||||
em.withColor(ec.getR(), ec.getG(), ec.getB());
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an EmbedObject for the specified CreatorResponse.
|
||||
*
|
||||
* @param ecr The CreatorResponse involved.
|
||||
* @return The EmbedObject for the CreatorResponse.
|
||||
*/
|
||||
public static EmbedObject getEventConfirmationEmbed(EventCreatorResponse ecr, GuildSettings settings) {
|
||||
EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), ecr.getEvent().getId());
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Event.Confirm.Title", settings));
|
||||
if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
|
||||
em.withImage(ed.getImageLink());
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.ID", settings), ecr.getEvent().getId(), false);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Date", settings), getHumanReadableDate(ecr.getEvent().getStart(), settings, false), false);
|
||||
if (ecr.getEvent().getLocation() != null && !ecr.getEvent().getLocation().equalsIgnoreCase("")) {
|
||||
if (ecr.getEvent().getLocation().length() > 300) {
|
||||
String location = ecr.getEvent().getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), ecr.getEvent().getLocation(), true);
|
||||
}
|
||||
}
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Event.Confirm.Footer", settings));
|
||||
em.withUrl(ecr.getEvent().getHtmlLink());
|
||||
try {
|
||||
EventColor ec = EventColor.fromId(Integer.valueOf(ecr.getEvent().getColorId()));
|
||||
em.withColor(ec.getR(), ec.getG(), ec.getB());
|
||||
} catch (Exception e) {
|
||||
//Color is null, ignore and add our default.
|
||||
em.withColor(56, 138, 237);
|
||||
}
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a formatted date.
|
||||
*
|
||||
* @param eventDateTime The object to get the date from.
|
||||
* @return A formatted date.
|
||||
*/
|
||||
public static String getHumanReadableDate(@Nullable EventDateTime eventDateTime, GuildSettings settings, boolean preEvent) {
|
||||
try {
|
||||
if (eventDateTime == null) {
|
||||
return "NOT SET";
|
||||
} else {
|
||||
//Get timezone
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
|
||||
|
||||
String timezone;
|
||||
if (!preEvent) {
|
||||
if (settings.useExternalCalendar()) {
|
||||
timezone = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
} else {
|
||||
timezone = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
}
|
||||
} else {
|
||||
timezone = "America/Chicago";
|
||||
}
|
||||
if (eventDateTime.getDateTime() != null) {
|
||||
long dateTime = eventDateTime.getDateTime().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||
|
||||
return format.format(ldt);
|
||||
|
||||
} else {
|
||||
long dateTime = eventDateTime.getDate().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||
|
||||
return format.format(ldt);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(null, "Failed to format date", e, EventMessageFormatter.class);
|
||||
return "ERROR! Code: E001";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a formatted time.
|
||||
*
|
||||
* @param eventDateTime The object to get the time from.
|
||||
* @return A formatted time.
|
||||
*/
|
||||
public static String getHumanReadableTime(@Nullable EventDateTime eventDateTime, GuildSettings settings, boolean preEvent) {
|
||||
try {
|
||||
if (eventDateTime == null) {
|
||||
return "NOT SET";
|
||||
} else {
|
||||
//Get timezone
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
|
||||
|
||||
String timezone;
|
||||
if (!preEvent) {
|
||||
if (settings.useExternalCalendar()) {
|
||||
timezone = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
} else {
|
||||
timezone = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
}
|
||||
} else {
|
||||
timezone = "America/Chicago";
|
||||
}
|
||||
if (eventDateTime.getDateTime() != null) {
|
||||
long dateTime = eventDateTime.getDateTime().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("hh:mm:ss a");
|
||||
|
||||
return format.format(ldt);
|
||||
|
||||
} else {
|
||||
long dateTime = eventDateTime.getDate().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("hh:mm:ss a");
|
||||
|
||||
return format.format(ldt);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(null, "Failed to format date", e, EventMessageFormatter.class);
|
||||
return "ERROR! Code: E002";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getHumanReadableDateTime(@Nullable EventDateTime eventDateTime, GuildSettings settings, boolean preEvent) {
|
||||
try {
|
||||
if (eventDateTime == null) {
|
||||
return "NOT SET";
|
||||
} else {
|
||||
//Get timezone
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
|
||||
|
||||
String timezone;
|
||||
if (!preEvent) {
|
||||
if (settings.useExternalCalendar()) {
|
||||
timezone = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
} else {
|
||||
timezone = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
}
|
||||
} else {
|
||||
timezone = "America/Chicago";
|
||||
}
|
||||
if (eventDateTime.getDateTime() != null) {
|
||||
long dateTime = eventDateTime.getDateTime().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a");
|
||||
|
||||
return format.format(ldt);
|
||||
|
||||
} else {
|
||||
long dateTime = eventDateTime.getDate().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a");
|
||||
|
||||
return format.format(ldt);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(null, "Failed to format date", e, EventMessageFormatter.class);
|
||||
return "ERROR! Code: E003";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.consolecommand;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.bot.internal.service.ApplicationHandler;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/2/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class ConsoleCommandExecutor {
|
||||
/**
|
||||
* Initiates the listener for commands via the DisCal console window.
|
||||
*/
|
||||
public static void init() {
|
||||
while (true) {
|
||||
System.out.println("Enter a command below: ");
|
||||
String input = System.console().readLine();
|
||||
Boolean cmdValid = false;
|
||||
if (input != null && !input.isEmpty()) {
|
||||
|
||||
//Important commands first.
|
||||
if (input.equalsIgnoreCase("exit")) {
|
||||
ApplicationHandler.exitApplication();
|
||||
return;
|
||||
}
|
||||
if (input.equalsIgnoreCase("restart")) {
|
||||
ApplicationHandler.restartApplication(null);
|
||||
return;
|
||||
}
|
||||
if (input.equalsIgnoreCase("?")) {
|
||||
cmdValid = true;
|
||||
System.out.println("Valid console commands: ");
|
||||
System.out.println("exit");
|
||||
System.out.println("restart");
|
||||
System.out.println("serverCount");
|
||||
System.out.println("silence true/false");
|
||||
System.out.println();
|
||||
}
|
||||
if (input.startsWith("serverCount")) {
|
||||
cmdValid = true;
|
||||
System.out.println("Server count: " + Main.client.getGuilds().size());
|
||||
}
|
||||
|
||||
if (!cmdValid) {
|
||||
System.out.println("Command not found! Use ? to list all commands.");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.network.discordpw;
|
||||
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 3/28/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class TimedUpdate extends TimerTask {
|
||||
/**
|
||||
* The action to be performed by this timer task.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
UpdateListData.updateSiteBotMeta();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.network.discordpw;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.object.BotSettings;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.mashape.unirest.http.HttpResponse;
|
||||
import com.mashape.unirest.http.JsonNode;
|
||||
import com.mashape.unirest.http.Unirest;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/13/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class UpdateListData {
|
||||
private static String token;
|
||||
|
||||
/**
|
||||
* Initiates the data updater with a valid token.
|
||||
*
|
||||
* @param settings BotSettings containing the API token.
|
||||
*/
|
||||
public static void init(BotSettings settings) {
|
||||
token = settings.getBotsPwToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the site meta on bots.discord.pw
|
||||
*/
|
||||
public static void updateSiteBotMeta() {
|
||||
try {
|
||||
Integer serverCount = Main.client.getGuilds().size();
|
||||
|
||||
JSONObject json = new JSONObject().put("server_count", serverCount);
|
||||
|
||||
HttpResponse<JsonNode> response = Unirest.post("https://bots.discord.pw/api/bots/265523588918935552/stats").header("Authorization", token).header("Content-Type", "application/json").body(json).asJson();
|
||||
} catch (Exception e) {
|
||||
//Handle issue.
|
||||
System.out.println("Failed to update Discord PW list metadata!");
|
||||
ExceptionHandler.sendException(null, "Failed to update Discord PW list.", e, UpdateListData.class);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.internal.service;
|
||||
package com.cloudcraftgaming.discal.bot.internal.service;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -25,7 +25,7 @@ public class ApplicationHandler {
|
||||
|
||||
/**
|
||||
* Restart the current Java application
|
||||
*
|
||||
* <p>
|
||||
* Code provided by: https://dzone.com/articles/programmatically-restart-java
|
||||
*
|
||||
* @param runBeforeRestart some custom code to be run before restarting
|
||||
@@ -66,12 +66,12 @@ public class ApplicationHandler {
|
||||
// execute the command in a shutdown hook, to be sure that all the
|
||||
// resources have been disposed before restarting the application
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
try {
|
||||
Runtime.getRuntime().exec(cmd.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}));
|
||||
try {
|
||||
Runtime.getRuntime().exec(cmd.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}));
|
||||
// execute some custom code before restarting
|
||||
if (runBeforeRestart != null) {
|
||||
runBeforeRestart.run();
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.internal.service;
|
||||
package com.cloudcraftgaming.discal.bot.internal.service;
|
||||
|
||||
import java.util.TimerTask;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.cloudcraftgaming.discal.bot.internal.service;
|
||||
|
||||
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.TimedUpdate;
|
||||
import com.cloudcraftgaming.discal.bot.module.announcement.AnnouncementTask;
|
||||
import com.cloudcraftgaming.discal.bot.module.misc.StatusChanger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Timer;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 3/5/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class TimeManager {
|
||||
private static TimeManager instance;
|
||||
|
||||
private final ArrayList<Timer> timers = new ArrayList<>();
|
||||
|
||||
private TimeManager() {
|
||||
} //Prevent initialization
|
||||
|
||||
/**
|
||||
* Gets the instance of the TimeManager that is loaded.
|
||||
*
|
||||
* @return The instance of the TimeManager.
|
||||
*/
|
||||
public static TimeManager getManager() {
|
||||
if (instance == null) {
|
||||
instance = new TimeManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the TimeManager and schedules the appropriate Timers.
|
||||
*/
|
||||
public void init() {
|
||||
Timer timer = new Timer(true);
|
||||
timer.schedule(new StatusChanger(), 10 * 1000, 10 * 1000);
|
||||
timer.schedule(new TimedUpdate(), 60 * 60 * 1000, 60 * 60 * 1000);
|
||||
|
||||
timers.add(timer);
|
||||
|
||||
Timer at = new Timer(true);
|
||||
at.schedule(new AnnouncementTask(), 5 * 1000 * 60, 5 * 1000 * 60);
|
||||
timers.add(at);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully shuts down the TimeManager and exits all timer threads preventing errors.
|
||||
*/
|
||||
public void shutdown() {
|
||||
for (Timer t : timers) {
|
||||
t.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.cloudcraftgaming.discal.eventlisteners;
|
||||
package com.cloudcraftgaming.discal.bot.listeners;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.internal.network.discordpw.UpdateListData;
|
||||
import com.cloudcraftgaming.discal.internal.service.TimeManager;
|
||||
import com.cloudcraftgaming.discal.bot.internal.network.discordpw.UpdateListData;
|
||||
import com.cloudcraftgaming.discal.bot.internal.service.TimeManager;
|
||||
import sx.blah.discord.api.events.EventSubscriber;
|
||||
import sx.blah.discord.handle.impl.events.ReadyEvent;
|
||||
|
||||
@@ -13,11 +13,11 @@ import sx.blah.discord.handle.impl.events.ReadyEvent;
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class ReadyEventListener {
|
||||
@EventSubscriber
|
||||
public void onReadyEvent(ReadyEvent event) {
|
||||
TimeManager.getManager().init();
|
||||
UpdateListData.updateSiteBotMeta();
|
||||
@EventSubscriber
|
||||
public void onReadyEvent(ReadyEvent event) {
|
||||
TimeManager.getManager().init();
|
||||
UpdateListData.updateSiteBotMeta();
|
||||
|
||||
MessageManager.reloadLangs();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.announcement;
|
||||
package com.cloudcraftgaming.discal.bot.module.announcement;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.message.Message;
|
||||
@@ -7,7 +7,7 @@ import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.announcement.Announcement;
|
||||
import com.cloudcraftgaming.discal.api.object.announcement.AnnouncementCreatorResponse;
|
||||
import com.cloudcraftgaming.discal.api.utils.AnnouncementUtils;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.announcement;
|
||||
package com.cloudcraftgaming.discal.bot.module.announcement;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
@@ -13,8 +13,8 @@ import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.event.EventData;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.api.utils.ImageUtils;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.event.EventMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.utils.ChannelUtils;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.event.EventMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.bot.utils.ChannelUtils;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.Event;
|
||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.announcement;
|
||||
package com.cloudcraftgaming.discal.bot.module.announcement;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -9,7 +9,7 @@ import com.cloudcraftgaming.discal.api.object.announcement.Announcement;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.utils.EventUtils;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.utils.GuildUtils;
|
||||
import com.cloudcraftgaming.discal.bot.utils.GuildUtils;
|
||||
import com.google.api.client.util.DateTime;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.Event;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -9,7 +9,7 @@ import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.CalendarListEntry;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.enums.announcement.AnnouncementType;
|
||||
@@ -13,12 +13,12 @@ import com.cloudcraftgaming.discal.api.utils.AnnouncementUtils;
|
||||
import com.cloudcraftgaming.discal.api.utils.EventUtils;
|
||||
import com.cloudcraftgaming.discal.api.utils.GeneralUtils;
|
||||
import com.cloudcraftgaming.discal.api.utils.MessageUtils;
|
||||
import com.cloudcraftgaming.discal.module.announcement.AnnouncementCreator;
|
||||
import com.cloudcraftgaming.discal.module.announcement.AnnouncementMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.utils.ChannelUtils;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.utils.RoleUtils;
|
||||
import com.cloudcraftgaming.discal.utils.UserUtils;
|
||||
import com.cloudcraftgaming.discal.bot.module.announcement.AnnouncementCreator;
|
||||
import com.cloudcraftgaming.discal.bot.module.announcement.AnnouncementMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.bot.utils.ChannelUtils;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.bot.utils.RoleUtils;
|
||||
import com.cloudcraftgaming.discal.bot.utils.UserUtils;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.*;
|
||||
import sx.blah.discord.util.EmbedBuilder;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.message.Message;
|
||||
@@ -10,10 +10,10 @@ import com.cloudcraftgaming.discal.api.object.calendar.PreCalendar;
|
||||
import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.utils.CalendarUtils;
|
||||
import com.cloudcraftgaming.discal.api.utils.GeneralUtils;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.calendar.CalendarCreator;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.calendar.CalendarMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.utils.TimeZoneUtils;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.calendar.CalendarCreator;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.calendar.CalendarMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.bot.utils.TimeZoneUtils;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
import sx.blah.discord.handle.obj.Permissions;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import sx.blah.discord.api.events.EventSubscriber;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -8,7 +8,7 @@ import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.api.utils.MessageUtils;
|
||||
import com.cloudcraftgaming.discal.internal.service.ApplicationHandler;
|
||||
import com.cloudcraftgaming.discal.bot.internal.service.ApplicationHandler;
|
||||
import sx.blah.discord.api.IDiscordClient;
|
||||
import sx.blah.discord.api.IShard;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -7,9 +7,9 @@ import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.utils.GeneralUtils;
|
||||
import com.cloudcraftgaming.discal.utils.ChannelUtils;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.utils.RoleUtils;
|
||||
import com.cloudcraftgaming.discal.bot.utils.ChannelUtils;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.bot.utils.RoleUtils;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IChannel;
|
||||
import sx.blah.discord.handle.obj.IGuild;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -12,9 +12,9 @@ import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.object.event.EventCreatorResponse;
|
||||
import com.cloudcraftgaming.discal.api.object.event.PreEvent;
|
||||
import com.cloudcraftgaming.discal.api.utils.*;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.event.EventCreator;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.event.EventMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.event.EventCreator;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.event.EventMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.bot.utils.PermissionChecker;
|
||||
import com.google.api.client.util.DateTime;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.Event;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -8,7 +8,7 @@ import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.event.EventMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.event.EventMessageFormatter;
|
||||
import com.google.api.client.util.DateTime;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.Event;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.message.Message;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -8,7 +8,7 @@ import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.calendar.CalendarMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.calendar.CalendarMessageFormatter;
|
||||
import com.google.api.services.calendar.model.Calendar;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -9,7 +9,7 @@ import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.object.event.RsvpData;
|
||||
import com.cloudcraftgaming.discal.api.utils.EventUtils;
|
||||
import com.cloudcraftgaming.discal.api.utils.TimeUtils;
|
||||
import com.cloudcraftgaming.discal.utils.UserUtils;
|
||||
import com.cloudcraftgaming.discal.bot.utils.UserUtils;
|
||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IGuild;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.command;
|
||||
package com.cloudcraftgaming.discal.bot.module.command;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
@@ -9,7 +9,7 @@ import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.command.CommandInfo;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.internal.calendar.calendar.CalendarMessageFormatter;
|
||||
import com.cloudcraftgaming.discal.bot.internal.calendar.calendar.CalendarMessageFormatter;
|
||||
import com.google.api.services.calendar.model.Calendar;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.util.EmbedBuilder;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.module.misc;
|
||||
package com.cloudcraftgaming.discal.bot.module.misc;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.cloudcraftgaming.discal.bot.utils;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IChannel;
|
||||
import sx.blah.discord.handle.obj.IGuild;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 3/29/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class ChannelUtils {
|
||||
/**
|
||||
* Checks if the specified channel exists.
|
||||
*
|
||||
* @param nameOrId The channel name or ID.
|
||||
* @param event The event received.
|
||||
* @return <code>true</code> if exists, else <code>false</code>.
|
||||
*/
|
||||
public static Boolean channelExists(String nameOrId, MessageReceivedEvent event) {
|
||||
if (nameOrId.contains("#")) {
|
||||
nameOrId = nameOrId.replace("#", "");
|
||||
}
|
||||
for (IChannel c : event.getGuild().getChannels()) {
|
||||
if (c.getName().equalsIgnoreCase(nameOrId) || c.getStringID().equals(nameOrId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the IChannel from its name.
|
||||
*
|
||||
* @param nameOrId The channel name or ID.
|
||||
* @param event The event received.
|
||||
* @return the IChannel if successful, else <code>null</code>.
|
||||
*/
|
||||
public static IChannel getChannelFromNameOrId(String nameOrId, MessageReceivedEvent event) {
|
||||
if (nameOrId.contains("#")) {
|
||||
nameOrId = nameOrId.replace("#", "");
|
||||
}
|
||||
for (IChannel c : event.getGuild().getChannels()) {
|
||||
if (c.getName().equalsIgnoreCase(nameOrId) || c.getStringID().equals(nameOrId)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the IChannel from its name.
|
||||
*
|
||||
* @param nameOrId The channel name or ID.
|
||||
* @param guildId The ID of the guild this channel belongs to.
|
||||
* @return the IChannel if successful, else <code>null</code>.
|
||||
*/
|
||||
public static IChannel getChannelFromNameOrId(String nameOrId, long guildId) {
|
||||
IGuild guild = Main.client.getGuildByID(guildId);
|
||||
if (nameOrId.contains("#")) {
|
||||
nameOrId = nameOrId.replace("#", "");
|
||||
}
|
||||
for (IChannel c : guild.getChannels()) {
|
||||
if (c.getName().equalsIgnoreCase(nameOrId) || c.getStringID().equals(nameOrId)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the IChannel from its name.
|
||||
*
|
||||
* @param nameOrId The channel name or ID.
|
||||
* @param guildId The ID of the guild this channel belongs to.
|
||||
* @return the IChannel if successful, else <code>null</code>.
|
||||
*/
|
||||
public static String getChannelNameFromNameOrId(String nameOrId, long guildId) {
|
||||
IGuild guild = Main.client.getGuildByID(guildId);
|
||||
if (nameOrId.contains("#")) {
|
||||
nameOrId = nameOrId.replace("#", "");
|
||||
}
|
||||
for (IChannel c : guild.getChannels()) {
|
||||
if (c.getName().equalsIgnoreCase(nameOrId) || c.getStringID().equals(nameOrId)) {
|
||||
return c.getName();
|
||||
}
|
||||
}
|
||||
return "ERROR";
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.utils;
|
||||
package com.cloudcraftgaming.discal.bot.utils;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 11/6/17.
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.cloudcraftgaming.discal.bot.utils;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IChannel;
|
||||
import sx.blah.discord.handle.obj.IRole;
|
||||
import sx.blah.discord.handle.obj.IUser;
|
||||
import sx.blah.discord.handle.obj.Permissions;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/19/17.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class PermissionChecker {
|
||||
/**
|
||||
* Checks if the user who sent the received message has the proper role to use a command.
|
||||
*
|
||||
* @param event The Event received to check for the user and guild.
|
||||
* @return <code>true</code> if the user has the proper role, otherwise <code>false</code>.
|
||||
*/
|
||||
public static boolean hasSufficientRole(MessageReceivedEvent event) {
|
||||
//TODO: Figure out exactly what is causing a NPE here...
|
||||
try {
|
||||
GuildSettings settings = DatabaseManager.getManager().getSettings(event.getGuild().getLongID());
|
||||
if (!settings.getControlRole().equalsIgnoreCase("everyone")) {
|
||||
IUser sender = event.getMessage().getAuthor();
|
||||
String roleId = settings.getControlRole();
|
||||
IRole role = null;
|
||||
|
||||
for (IRole r : event.getMessage().getGuild().getRoles()) {
|
||||
if (r.getStringID().equals(roleId)) {
|
||||
role = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (role != null) {
|
||||
for (IRole r : sender.getRolesForGuild(event.getMessage().getGuild())) {
|
||||
if (r.getStringID().equals(role.getStringID()) || r.getPosition() > role.getPosition()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
//Role not found... reset Db...
|
||||
settings.setControlRole("everyone");
|
||||
DatabaseManager.getManager().updateSettings(settings);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//Something broke so we will harmlessly allow access and email the dev.
|
||||
ExceptionHandler.sendException(event.getMessage().getAuthor(), "Failed to check for sufficient control role.", e, PermissionChecker.class);
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean hasManageServerRole(MessageReceivedEvent event) {
|
||||
return event.getMessage().getAuthor().getPermissionsForGuild(event.getMessage().getGuild()).contains(
|
||||
Permissions.MANAGE_SERVER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user sent the command in a DisCal channel (if set).
|
||||
*
|
||||
* @param event The event received to check for the correct channel.
|
||||
* @return <code>true</code> if in correct channel, otherwise <code>false</code>.
|
||||
*/
|
||||
public static boolean isCorrectChannel(MessageReceivedEvent event) {
|
||||
try {
|
||||
GuildSettings settings = DatabaseManager.getManager().getSettings(event.getGuild().getLongID());
|
||||
if (settings.getDiscalChannel().equalsIgnoreCase("all")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IChannel channel = null;
|
||||
for (IChannel c : event.getMessage().getGuild().getChannels()) {
|
||||
if (c.getStringID().equals(settings.getDiscalChannel())) {
|
||||
channel = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (channel != null) {
|
||||
return event.getMessage().getChannel().getStringID().equals(channel.getStringID());
|
||||
}
|
||||
|
||||
//If we got here, the channel no longer exists, reset data and return true.
|
||||
settings.setDiscalChannel("all");
|
||||
DatabaseManager.getManager().updateSettings(settings);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
//Catch any errors so that the bot always responds...
|
||||
ExceptionHandler.sendException(event.getMessage().getAuthor(), "Failed to check for discal channel.", e, PermissionChecker.class);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean botHasMessageManagePerms(MessageReceivedEvent event) {
|
||||
return Main.getSelfUser().getPermissionsForGuild(event.getMessage().getGuild()).contains(Permissions.MANAGE_MESSAGES);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.utils;
|
||||
package com.cloudcraftgaming.discal.bot.utils;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.utils.GeneralUtils;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.cloudcraftgaming.discal.bot.utils;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.enums.BadTimezone;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 4/7/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class TimeZoneUtils {
|
||||
public static boolean isValid(String value) {
|
||||
try {
|
||||
DateTimeZone tz = DateTimeZone.forID(value);
|
||||
return tz != null && !isBadTz(value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isBadTz(String value) {
|
||||
try {
|
||||
BadTimezone.valueOf(value.replaceAll("/", "_"));
|
||||
return true;
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cloudcraftgaming.discal.utils;
|
||||
package com.cloudcraftgaming.discal.bot.utils;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.utils.GeneralUtils;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
@@ -27,9 +27,8 @@ public class UserUtils {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static long getUser(String toLookFor, IMessage m) {
|
||||
return getUser(toLookFor,m.getGuild());
|
||||
return getUser(toLookFor, m.getGuild());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,7 +95,7 @@ public class UserUtils {
|
||||
return res;
|
||||
}
|
||||
|
||||
public static IUser getUserFromID(String id, IGuild guild) {
|
||||
private static IUser getUserFromID(String id, IGuild guild) {
|
||||
try {
|
||||
return guild.getUserByID(Long.parseUnsignedLong(id));
|
||||
} catch (Exception e) {
|
||||
@@ -1,256 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.internal.calendar.calendar;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.message.Message;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarCreatorResponse;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.PreCalendar;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.google.api.services.calendar.model.AclRule;
|
||||
import com.google.api.services.calendar.model.Calendar;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/4/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class CalendarCreator {
|
||||
private static CalendarCreator instance;
|
||||
|
||||
private ArrayList<PreCalendar> calendars = new ArrayList<>();
|
||||
|
||||
private CalendarCreator() {} //Prevent initialization
|
||||
|
||||
/**
|
||||
* Gets the instance of the CalendarCreator.
|
||||
* @return The instance of the CalendarCreator.
|
||||
*/
|
||||
public static CalendarCreator getCreator() {
|
||||
if (instance == null) {
|
||||
instance = new CalendarCreator();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
//Functionals
|
||||
/**
|
||||
* Initiates the CalendarCreator for the guild involved in the event.
|
||||
* @param e The event received upon creation start.
|
||||
* @param calendarName The name of the calendar to create.
|
||||
* @return The PreCalendar object created.
|
||||
*/
|
||||
public PreCalendar init(MessageReceivedEvent e, String calendarName, GuildSettings settings, boolean handleCreatorMessage) {
|
||||
long guildId = e.getMessage().getGuild().getLongID();
|
||||
if (!hasPreCalendar(guildId)) {
|
||||
PreCalendar calendar = new PreCalendar(guildId, calendarName);
|
||||
|
||||
if (handleCreatorMessage) {
|
||||
if (PermissionChecker.botHasMessageManagePerms(e)) {
|
||||
IMessage msg = Message.sendMessage(CalendarMessageFormatter.getPreCalendarEmbed(calendar, settings), MessageManager.getMessage("Creator.Calendar.Create.Init", settings), e);
|
||||
calendar.setCreatorMessage(msg);
|
||||
} else {
|
||||
Message.sendMessage(MessageManager.getMessage("Creator.Notif.MANAGE_MESSAGES", settings), e);
|
||||
}
|
||||
}
|
||||
calendars.add(calendar);
|
||||
return calendar;
|
||||
}
|
||||
return getPreCalendar(e.getMessage().getGuild().getLongID());
|
||||
}
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
public PreCalendar edit(MessageReceivedEvent event, GuildSettings settings, boolean handleCreatorMessage) {
|
||||
long guildId = event.getMessage().getGuild().getLongID();
|
||||
if (!hasPreCalendar(guildId)) {
|
||||
//TODO: Support multiple calendars
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(guildId);
|
||||
|
||||
try {
|
||||
com.google.api.services.calendar.Calendar service;
|
||||
if (settings.useExternalCalendar()) {
|
||||
service = CalendarAuth.getCalendarService(settings);
|
||||
} else {
|
||||
service = CalendarAuth.getCalendarService();
|
||||
}
|
||||
|
||||
Calendar calendar = service.calendars().get(data.getCalendarAddress()).execute();
|
||||
|
||||
PreCalendar preCalendar = new PreCalendar(guildId, calendar);
|
||||
preCalendar.setEditing(true);
|
||||
preCalendar.setCalendarId(data.getCalendarAddress());
|
||||
|
||||
if (handleCreatorMessage) {
|
||||
if (PermissionChecker.botHasMessageManagePerms(event)) {
|
||||
IMessage msg = Message.sendMessage(CalendarMessageFormatter.getPreCalendarEmbed(preCalendar, settings), MessageManager.getMessage("Creator.Calendar.Edit.Init", settings), event);
|
||||
preCalendar.setCreatorMessage(msg);
|
||||
} else {
|
||||
Message.sendMessage(MessageManager.getMessage("Creator.Notif.MANAGE_MESSAGES", settings), event);
|
||||
}
|
||||
}
|
||||
|
||||
calendars.add(preCalendar);
|
||||
return preCalendar;
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(event.getMessage().getAuthor(), "Failed to init calendar editor", e, this.getClass());
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return getPreCalendar(guildId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully closes down the CalendarCreator for the guild involved and DOES NOT create the calendar.
|
||||
* @param e The event received upon termination.
|
||||
* @return <codfe>true</codfe> if closed successfully, otherwise <code>false</code>.
|
||||
*/
|
||||
public Boolean terminate(MessageReceivedEvent e) {
|
||||
if (hasPreCalendar(e.getMessage().getGuild().getLongID())) {
|
||||
calendars.remove(getPreCalendar(e.getMessage().getGuild().getLongID()));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
ExceptionHandler.sendException(e.getMessage().getAuthor(), "Failed to confirm calendar.", ex, this.getClass());
|
||||
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) {
|
||||
ExceptionHandler.sendException(e.getMessage().getAuthor(), "Failed to update calendar.", ex, this.getClass());
|
||||
CalendarCreatorResponse response = new CalendarCreatorResponse(false);
|
||||
response.setEdited(true);
|
||||
response.setCreatorMessage(preCalendar.getCreatorMessage());
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new CalendarCreatorResponse(false);
|
||||
}
|
||||
|
||||
//Getters
|
||||
/**
|
||||
* Gets the PreCalendar for the guild in the creator.
|
||||
* @param guildId The ID of the guild whose PreCalendar is to be returned.
|
||||
* @return The PreCalendar belonging to the guild.
|
||||
*/
|
||||
public PreCalendar getPreCalendar(long guildId) {
|
||||
for (PreCalendar c : calendars) {
|
||||
if (c.getGuildId() ==guildId) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IMessage getCreatorMessage(long guildId) {
|
||||
if (hasPreCalendar(guildId)) {
|
||||
return getPreCalendar(guildId).getCreatorMessage();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
/**
|
||||
* Checks whether or not the specified Guild has a PreCalendar in the creator.
|
||||
* @param guildId The ID of the guild to check for.
|
||||
* @return <code>true</code> if a PreCalendar exists, else <code>false</code>.
|
||||
*/
|
||||
public Boolean hasPreCalendar(long guildId) {
|
||||
for (PreCalendar c : calendars) {
|
||||
if (c.getGuildId() == guildId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasCreatorMessage(long guildId) {
|
||||
return hasPreCalendar(guildId) && getPreCalendar(guildId).getCreatorMessage() != null;
|
||||
}
|
||||
|
||||
//Setters
|
||||
public void setCreatorMessage(IMessage msg) {
|
||||
if (msg != null) {
|
||||
if (hasPreCalendar(msg.getGuild().getLongID())) {
|
||||
getPreCalendar(msg.getGuild().getLongID()).setCreatorMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.internal.calendar.calendar;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.PreCalendar;
|
||||
import com.google.api.services.calendar.model.Calendar;
|
||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||
import sx.blah.discord.util.EmbedBuilder;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/4/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class CalendarMessageFormatter {
|
||||
public static String getCalendarLink(String calId) {
|
||||
URI callURI = URI.create(calId);
|
||||
return "https://calendar.google.com/calendar/embed?src=" + callURI;
|
||||
}
|
||||
|
||||
public static EmbedObject getCalendarLinkEmbed(Calendar cal, GuildSettings settings) {
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Calendar.Link.Title", settings));
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Link.Summary", settings), cal.getSummary(), true);
|
||||
try {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Link.Description", settings), cal.getDescription(), true);
|
||||
} catch (NullPointerException | IllegalArgumentException e) {
|
||||
//Some error, desc probably never set, just ignore no need to log.
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Link.TimeZone", settings), cal.getTimeZone(), false);
|
||||
em.withUrl(CalendarMessageFormatter.getCalendarLink(cal.getId()));
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Calendar.Link.CalendarId", "%id%", cal.getId(), settings));
|
||||
em.withColor(56, 138, 237);
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an EmbedObject for the PreCalendar.
|
||||
* @param calendar The PreCalendar to create an EmbedObject for.
|
||||
* @return The EmbedObject for the PreCalendar.
|
||||
*/
|
||||
public static EmbedObject getPreCalendarEmbed(PreCalendar calendar, GuildSettings settings) {
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Calendar.Pre.Title", settings));
|
||||
if (calendar.getSummary() != null) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.Summary", settings), calendar.getSummary(), true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.Summary", settings), "***UNSET***", true);
|
||||
}
|
||||
if (calendar.getDescription() != null) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.Description", settings), calendar.getDescription(), false);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.Description", settings), "***UNSET***", false);
|
||||
}
|
||||
if (calendar.getTimezone() != null) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.TimeZone", settings), calendar.getTimezone(), true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.TimeZone", settings), "***UNSET***", true);
|
||||
}
|
||||
if (calendar.isEditing()) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Calendar.Pre.CalendarId", settings), calendar.getCalendarId(), false);
|
||||
} //No else needed, just don't post it.
|
||||
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Calendar.Pre.Key", settings));
|
||||
em.withColor(56, 138, 237);
|
||||
|
||||
return em.build();
|
||||
}
|
||||
}
|
||||
@@ -1,345 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.internal.calendar.event;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.enums.event.EventColor;
|
||||
import com.cloudcraftgaming.discal.api.message.Message;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.event.EventCreatorResponse;
|
||||
import com.cloudcraftgaming.discal.api.object.event.PreEvent;
|
||||
import com.cloudcraftgaming.discal.api.utils.EventUtils;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.utils.PermissionChecker;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.Event;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/3/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class EventCreator {
|
||||
private static EventCreator instance;
|
||||
|
||||
private ArrayList<PreEvent> events = new ArrayList<>();
|
||||
|
||||
private EventCreator() {} //Prevent initialization.
|
||||
|
||||
/**
|
||||
* Gets the instance of the EventCreator.
|
||||
* @return The instance of the EventCreator
|
||||
*/
|
||||
public static EventCreator getCreator() {
|
||||
if (instance == null) {
|
||||
instance = new EventCreator();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
//Functionals
|
||||
/**
|
||||
* Initiates the EventCreator for a specific guild.
|
||||
* @param e The event received upon initialization.
|
||||
* @return The PreEvent for the guild.
|
||||
*/
|
||||
public PreEvent init(MessageReceivedEvent e, GuildSettings settings, boolean handleMessage) {
|
||||
if (!hasPreEvent(e.getGuild().getLongID())) {
|
||||
PreEvent event = new PreEvent(e.getGuild().getLongID());
|
||||
try {
|
||||
|
||||
//TODO: Handle multiple calendars...
|
||||
String calId = DatabaseManager.getManager().getMainCalendar(e.getGuild().getLongID()).getCalendarAddress();
|
||||
event.setTimeZone(CalendarAuth.getCalendarService().calendars().get(calId).execute().getTimeZone());
|
||||
} catch (IOException exc) {
|
||||
//Failed to get timezone, ignore safely.
|
||||
}
|
||||
if (handleMessage) {
|
||||
if (PermissionChecker.botHasMessageManagePerms(e)) {
|
||||
IMessage message = Message.sendMessage(EventMessageFormatter.getPreEventEmbed(event, settings), MessageManager.getMessage("Creator.Event.Create.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;
|
||||
}
|
||||
return getPreEvent(e.getGuild().getLongID());
|
||||
}
|
||||
|
||||
public PreEvent init(MessageReceivedEvent e, GuildSettings settings, String summary, boolean handleMessage) {
|
||||
if (!hasPreEvent(e.getGuild().getLongID())) {
|
||||
PreEvent event = new PreEvent(e.getGuild().getLongID());
|
||||
event.setSummary(summary);
|
||||
try {
|
||||
|
||||
//TODO: Handle multiple calendars...
|
||||
String calId = DatabaseManager.getManager().getMainCalendar(e.getGuild().getLongID()).getCalendarAddress();
|
||||
if (!settings.useExternalCalendar()) {
|
||||
event.setTimeZone(CalendarAuth.getCalendarService().calendars().get(calId).execute().getTimeZone());
|
||||
} else {
|
||||
event.setTimeZone(CalendarAuth.getCalendarService(settings).calendars().get(calId).execute().getTimeZone());
|
||||
}
|
||||
} catch (Exception exc) {
|
||||
//Failed to get timezone, ignore safely.
|
||||
}
|
||||
if (handleMessage) {
|
||||
if (PermissionChecker.botHasMessageManagePerms(e)) {
|
||||
IMessage message = Message.sendMessage(EventMessageFormatter.getPreEventEmbed(event, settings), MessageManager.getMessage("Creator.Event.Create.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;
|
||||
}
|
||||
return getPreEvent(e.getGuild().getLongID());
|
||||
}
|
||||
|
||||
//Copy event
|
||||
public PreEvent init(MessageReceivedEvent e, String eventId, GuildSettings settings, boolean handleMessage) {
|
||||
if (!hasPreEvent(e.getGuild().getLongID())) {
|
||||
//TODO: Handle multiple calendars...
|
||||
try {
|
||||
String calId = DatabaseManager.getManager().getMainCalendar(e.getGuild().getLongID()).getCalendarAddress();
|
||||
Calendar service;
|
||||
if (settings.useExternalCalendar()) {
|
||||
service = CalendarAuth.getCalendarService(settings);
|
||||
} else {
|
||||
service = CalendarAuth.getCalendarService();
|
||||
}
|
||||
Event calEvent = service.events().get(calId, eventId).execute();
|
||||
|
||||
PreEvent event = EventUtils.copyEvent(e.getGuild().getLongID(), calEvent);
|
||||
|
||||
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.Copy.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) {
|
||||
//Something failed...
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return getPreEvent(e.getGuild().getLongID());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully terminates the EventCreator for a specific guild.
|
||||
* @param e The event received upon termination.
|
||||
* @return <code>true</code> if successful, else <code>false</code>.
|
||||
*/
|
||||
public Boolean terminate(MessageReceivedEvent e) {
|
||||
if (hasPreEvent(e.getGuild().getLongID())) {
|
||||
events.remove(getPreEvent(e.getGuild().getLongID()));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()) {
|
||||
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) {
|
||||
ExceptionHandler.sendException(e.getAuthor(), "Failed to create event.", ex, this.getClass());
|
||||
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) {
|
||||
ExceptionHandler.sendException(e.getAuthor(), "Failed to update event.", ex, this.getClass());
|
||||
EventCreatorResponse response = new EventCreatorResponse(false);
|
||||
response.setEdited(true);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new EventCreatorResponse(false);
|
||||
}
|
||||
|
||||
//Getters
|
||||
/**
|
||||
* gets the PreEvent for the specified guild.
|
||||
* @param guildId The ID of the guild.
|
||||
* @return The PreEvent belonging to the guild.
|
||||
*/
|
||||
public PreEvent getPreEvent(long guildId) {
|
||||
for (PreEvent e : events) {
|
||||
if (e.getGuildId() == guildId) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IMessage getCreatorMessage(long guildId) {
|
||||
if (hasPreEvent(guildId)) {
|
||||
return getPreEvent(guildId).getCreatorMessage();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//Booleans/Checkers
|
||||
/**
|
||||
* Checks if the specified guild has a PreEvent in the creator.
|
||||
* @param guildId The ID of the guild.
|
||||
* @return <code>true</code> if a PreEvent exists, otherwise <code>false</code>.
|
||||
*/
|
||||
public Boolean hasPreEvent(long guildId) {
|
||||
for (PreEvent e : events) {
|
||||
if (e.getGuildId() == guildId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasCreatorMessage(long guildId) {
|
||||
return hasPreEvent(guildId) && getPreEvent(guildId).getCreatorMessage() != null;
|
||||
}
|
||||
|
||||
//Setters
|
||||
public void setCreatorMessage(IMessage msg) {
|
||||
if (msg != null) {
|
||||
if (hasPreEvent(msg.getGuild().getLongID())) {
|
||||
getPreEvent(msg.getGuild().getLongID()).setCreatorMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,376 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.internal.calendar.event;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.enums.event.EventColor;
|
||||
import com.cloudcraftgaming.discal.api.message.MessageManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.object.calendar.CalendarData;
|
||||
import com.cloudcraftgaming.discal.api.object.event.EventCreatorResponse;
|
||||
import com.cloudcraftgaming.discal.api.object.event.EventData;
|
||||
import com.cloudcraftgaming.discal.api.object.event.PreEvent;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.cloudcraftgaming.discal.api.utils.ImageUtils;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.model.Event;
|
||||
import com.google.api.services.calendar.model.EventDateTime;
|
||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||
import sx.blah.discord.util.EmbedBuilder;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/3/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class EventMessageFormatter {
|
||||
|
||||
/**
|
||||
* Gets an EmbedObject for the specified event.
|
||||
* @param event The event involved.
|
||||
* @param settings The guild's settings
|
||||
* @return The EmbedObject of the event.
|
||||
*/
|
||||
public static EmbedObject getEventEmbed(Event event, GuildSettings settings) {
|
||||
EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), event.getId());
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Event.Info.Title", settings));
|
||||
if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
|
||||
em.withImage(ed.getImageLink());
|
||||
}
|
||||
if (event.getSummary() != null) {
|
||||
String summary = event.getSummary();
|
||||
if (summary.length() > 250) {
|
||||
summary = summary.substring(0, 250);
|
||||
summary = summary + " (continues on Google Calendar View)";
|
||||
}
|
||||
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.Summary", settings), summary, true);
|
||||
}
|
||||
if (event.getDescription() != null) {
|
||||
String description = event.getDescription();
|
||||
if (description.length() > 500) {
|
||||
description = description.substring(0, 500);
|
||||
description = description + " (continues on Google Calendar View)";
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.Description", settings), description, true);
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.StartDate", settings), getHumanReadableDate(event.getStart(), settings, false), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.StartTime", settings), getHumanReadableTime(event.getStart(), settings, false), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.EndDate", settings), getHumanReadableDate(event.getEnd(), settings, false), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.EndTime", settings), getHumanReadableTime(event.getEnd(), settings, false), true);
|
||||
|
||||
try {
|
||||
//TODO: add support for multiple calendars...
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
|
||||
Calendar service;
|
||||
service = settings.useExternalCalendar() ? CalendarAuth.getCalendarService(settings) : CalendarAuth.getCalendarService();
|
||||
String tz = service.calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.TimeZone", settings), tz, true);
|
||||
} catch (Exception e1) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Info.TimeZone", settings), "Error/Unknown", true);
|
||||
}
|
||||
if (event.getLocation() != null && !event.getLocation().equalsIgnoreCase("")) {
|
||||
if (event.getLocation().length() > 300) {
|
||||
String location = event.getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), event.getLocation(), true);
|
||||
}
|
||||
}
|
||||
//TODO: Add info on recurrence here.
|
||||
em.withUrl(event.getHtmlLink());
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Event.Info.ID", "%id%", event.getId(), settings));
|
||||
try {
|
||||
EventColor ec = EventColor.fromId(Integer.valueOf(event.getColorId()));
|
||||
em.withColor(ec.getR(), ec.getG(), ec.getB());
|
||||
} catch (Exception e) {
|
||||
//Color is null, ignore and add our default.
|
||||
em.withColor(56, 138, 237);
|
||||
}
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an EmbedObject for the specified event.
|
||||
* @param event The event involved.
|
||||
* @return The EmbedObject of the event.
|
||||
*/
|
||||
public static EmbedObject getCondensedEventEmbed(Event event, GuildSettings settings) {
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Event.Condensed.Title", settings));
|
||||
EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), event.getId());
|
||||
if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
|
||||
em.withThumbnail(ed.getImageLink());
|
||||
}
|
||||
if (event.getSummary() != null) {
|
||||
String summary = event.getSummary();
|
||||
if (summary.length() > 250) {
|
||||
summary = summary.substring(0, 250);
|
||||
summary = summary + " (continues on Google Calendar View)";
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Condensed.Summary", settings), summary, true);
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Condensed.Date", settings), getHumanReadableDate(event.getStart(), settings, false), true);
|
||||
if (event.getLocation() != null && !event.getLocation().equalsIgnoreCase("")) {
|
||||
if (event.getLocation().length() > 300) {
|
||||
String location = event.getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), event.getLocation(), true);
|
||||
}
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Condensed.ID", settings), event.getId(), false);
|
||||
em.withUrl(event.getHtmlLink());
|
||||
try {
|
||||
EventColor ec = EventColor.fromId(Integer.valueOf(event.getColorId()));
|
||||
em.withColor(ec.getR(), ec.getG(), ec.getB());
|
||||
} catch (Exception e) {
|
||||
//Color is null, ignore and add our default.
|
||||
em.withColor(56, 138, 237);
|
||||
}
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an EmbedObject for the specified PreEvent.
|
||||
* @param event The PreEvent to get an embed for.
|
||||
* @return The EmbedObject of the PreEvent.
|
||||
*/
|
||||
public static EmbedObject getPreEventEmbed(PreEvent event, GuildSettings settings) {
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Event.Pre.Title", settings));
|
||||
if (event.getEventData() != null && event.getEventData().getImageLink() != null && ImageUtils.validate(event.getEventData().getImageLink())) {
|
||||
em.withImage(event.getEventData().getImageLink());
|
||||
}
|
||||
if (event.isEditing()) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Id", settings), event.getEventId(), false);
|
||||
}
|
||||
if (event.getSummary() != null) {
|
||||
String summary = event.getSummary();
|
||||
if (summary.length() > 250) {
|
||||
summary = summary.substring(0, 250);
|
||||
summary = summary + " (continues on Google Calendar View)";
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Summary", settings), summary, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Summary", settings), "NOT SET", true);
|
||||
}
|
||||
if (event.getDescription() != null) {
|
||||
String description = event.getDescription();
|
||||
if (description.length() > 500) {
|
||||
description = description.substring(0, 500);
|
||||
description = description + " (continues on Google Calendar View)";
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Description", settings), description, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Description", settings), "NOT SET", true);
|
||||
}
|
||||
if (event.shouldRecur()) {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Recurrence", settings), event.getRecurrence().toHumanReadable(), true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.Recurrence", settings), "N/a", true);
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.StartDate", settings), getHumanReadableDate(event.getViewableStartDate(), settings, true), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.StartTime", settings), EventMessageFormatter.getHumanReadableTime(event.getViewableStartDate(), settings, true), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.EndDate", settings), getHumanReadableDate(event.getViewableEndDate(), settings, true), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.EndTime", settings), EventMessageFormatter.getHumanReadableTime(event.getViewableEndDate(), settings, true), true);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Pre.TimeZone", settings), event.getTimeZone(), true);
|
||||
|
||||
if (event.getLocation() != null && !event.getLocation().equalsIgnoreCase("")) {
|
||||
if (event.getLocation().length() > 300) {
|
||||
String location = event.getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), event.getLocation(), true);
|
||||
}
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), "N/a", true);
|
||||
}
|
||||
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Event.Pre.Key", settings));
|
||||
EventColor ec = event.getColor();
|
||||
em.withColor(ec.getR(), ec.getG(), ec.getB());
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an EmbedObject for the specified CreatorResponse.
|
||||
* @param ecr The CreatorResponse involved.
|
||||
* @return The EmbedObject for the CreatorResponse.
|
||||
*/
|
||||
public static EmbedObject getEventConfirmationEmbed(EventCreatorResponse ecr, GuildSettings settings) {
|
||||
EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), ecr.getEvent().getId());
|
||||
EmbedBuilder em = new EmbedBuilder();
|
||||
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
|
||||
em.withAuthorName("DisCal");
|
||||
em.withTitle(MessageManager.getMessage("Embed.Event.Confirm.Title", settings));
|
||||
if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
|
||||
em.withImage(ed.getImageLink());
|
||||
}
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.ID", settings), ecr.getEvent().getId(), false);
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Date", settings), getHumanReadableDate(ecr.getEvent().getStart(), settings, false), false);
|
||||
if (ecr.getEvent().getLocation() != null && !ecr.getEvent().getLocation().equalsIgnoreCase("")) {
|
||||
if (ecr.getEvent().getLocation().length() > 300) {
|
||||
String location = ecr.getEvent().getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
|
||||
} else {
|
||||
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), ecr.getEvent().getLocation(), true);
|
||||
}
|
||||
}
|
||||
em.withFooterText(MessageManager.getMessage("Embed.Event.Confirm.Footer", settings));
|
||||
em.withUrl(ecr.getEvent().getHtmlLink());
|
||||
try {
|
||||
EventColor ec = EventColor.fromId(Integer.valueOf(ecr.getEvent().getColorId()));
|
||||
em.withColor(ec.getR(), ec.getG(), ec.getB());
|
||||
} catch (Exception e) {
|
||||
//Color is null, ignore and add our default.
|
||||
em.withColor(56, 138, 237);
|
||||
}
|
||||
|
||||
return em.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a formatted date.
|
||||
* @param eventDateTime The object to get the date from.
|
||||
* @return A formatted date.
|
||||
*/
|
||||
public static String getHumanReadableDate(@Nullable EventDateTime eventDateTime, GuildSettings settings, boolean preEvent) {
|
||||
try {
|
||||
if (eventDateTime == null) {
|
||||
return "NOT SET";
|
||||
} else {
|
||||
//Get timezone
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
|
||||
|
||||
String timezone;
|
||||
if (!preEvent) {
|
||||
if (settings.useExternalCalendar()) {
|
||||
timezone = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
} else {
|
||||
timezone = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
}
|
||||
} else {
|
||||
timezone = "America/Chicago";
|
||||
}
|
||||
if (eventDateTime.getDateTime() != null) {
|
||||
long dateTime = eventDateTime.getDateTime().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||
|
||||
return format.format(ldt);
|
||||
|
||||
} else {
|
||||
long dateTime = eventDateTime.getDate().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||
|
||||
return format.format(ldt);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(null, "Failed to format date", e, EventMessageFormatter.class);
|
||||
return "ERROR! Code: E001";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a formatted time.
|
||||
* @param eventDateTime The object to get the time from.
|
||||
* @return A formatted time.
|
||||
*/
|
||||
public static String getHumanReadableTime(@Nullable EventDateTime eventDateTime, GuildSettings settings, boolean preEvent) {
|
||||
try {
|
||||
if (eventDateTime == null) {
|
||||
return "NOT SET";
|
||||
} else {
|
||||
//Get timezone
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
|
||||
|
||||
String timezone;
|
||||
if (!preEvent) {
|
||||
if (settings.useExternalCalendar()) {
|
||||
timezone = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
} else {
|
||||
timezone = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
}
|
||||
} else {
|
||||
timezone = "America/Chicago";
|
||||
}
|
||||
if (eventDateTime.getDateTime() != null) {
|
||||
long dateTime = eventDateTime.getDateTime().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("hh:mm:ss a");
|
||||
|
||||
return format.format(ldt);
|
||||
|
||||
} else {
|
||||
long dateTime = eventDateTime.getDate().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("hh:mm:ss a");
|
||||
|
||||
return format.format(ldt);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(null, "Failed to format date", e, EventMessageFormatter.class);
|
||||
return "ERROR! Code: E002";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getHumanReadableDateTime(@Nullable EventDateTime eventDateTime, GuildSettings settings, boolean preEvent) {
|
||||
try {
|
||||
if (eventDateTime == null) {
|
||||
return "NOT SET";
|
||||
} else {
|
||||
//Get timezone
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
|
||||
|
||||
String timezone;
|
||||
if (!preEvent) {
|
||||
if (settings.useExternalCalendar()) {
|
||||
timezone = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
} else {
|
||||
timezone = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute().getTimeZone();
|
||||
}
|
||||
} else {
|
||||
timezone = "America/Chicago";
|
||||
}
|
||||
if (eventDateTime.getDateTime() != null) {
|
||||
long dateTime = eventDateTime.getDateTime().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a");
|
||||
|
||||
return format.format(ldt);
|
||||
|
||||
} else {
|
||||
long dateTime = eventDateTime.getDate().getValue();
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of(timezone));
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a");
|
||||
|
||||
return format.format(ldt);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ExceptionHandler.sendException(null, "Failed to format date", e, EventMessageFormatter.class);
|
||||
return "ERROR! Code: E003";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.internal.consolecommand;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.internal.service.ApplicationHandler;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/2/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class ConsoleCommandExecutor {
|
||||
/**
|
||||
* Initiates the listener for commands via the DisCal console window.
|
||||
*/
|
||||
public static void init() {
|
||||
while (true) {
|
||||
System.out.println("Enter a command below: ");
|
||||
String input = System.console().readLine();
|
||||
Boolean cmdValid = false;
|
||||
if (input != null && !input.isEmpty()) {
|
||||
|
||||
//Important commands first.
|
||||
if (input.equalsIgnoreCase("exit")) {
|
||||
ApplicationHandler.exitApplication();
|
||||
return;
|
||||
}
|
||||
if (input.equalsIgnoreCase("restart")) {
|
||||
ApplicationHandler.restartApplication(null);
|
||||
return;
|
||||
}
|
||||
if (input.equalsIgnoreCase("?")) {
|
||||
cmdValid = true;
|
||||
System.out.println("Valid console commands: ");
|
||||
System.out.println("exit");
|
||||
System.out.println("restart");
|
||||
System.out.println("serverCount");
|
||||
System.out.println("silence true/false");
|
||||
System.out.println();
|
||||
}
|
||||
if (input.startsWith("serverCount")) {
|
||||
cmdValid = true;
|
||||
System.out.println("Server count: " + Main.client.getGuilds().size());
|
||||
}
|
||||
|
||||
if (!cmdValid) {
|
||||
System.out.println("Command not found! Use ? to list all commands.");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.internal.network.discordpw;
|
||||
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 3/28/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class TimedUpdate extends TimerTask {
|
||||
/**
|
||||
* The action to be performed by this timer task.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
UpdateListData.updateSiteBotMeta();
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.internal.network.discordpw;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.object.BotSettings;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import com.mashape.unirest.http.HttpResponse;
|
||||
import com.mashape.unirest.http.JsonNode;
|
||||
import com.mashape.unirest.http.Unirest;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/13/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class UpdateListData {
|
||||
private static String token;
|
||||
|
||||
/**
|
||||
* Initiates the data updater with a valid token.
|
||||
* @param settings BotSettings containing the API token.
|
||||
*/
|
||||
public static void init(BotSettings settings) {
|
||||
token = settings.getBotsPwToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the site meta on bots.discord.pw
|
||||
*/
|
||||
public static void updateSiteBotMeta() {
|
||||
try {
|
||||
Integer serverCount = Main.client.getGuilds().size();
|
||||
|
||||
JSONObject json = new JSONObject().put("server_count", serverCount);
|
||||
|
||||
HttpResponse<JsonNode> response = Unirest.post("https://bots.discord.pw/api/bots/265523588918935552/stats").header("Authorization", token).header("Content-Type", "application/json").body(json).asJson();
|
||||
} catch (Exception e) {
|
||||
//Handle issue.
|
||||
System.out.println("Failed to update Discord PW list metadata!");
|
||||
ExceptionHandler.sendException(null, "Failed to update Discord PW list.", e, UpdateListData.class);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.internal.service;
|
||||
|
||||
import com.cloudcraftgaming.discal.internal.network.discordpw.TimedUpdate;
|
||||
import com.cloudcraftgaming.discal.module.announcement.AnnouncementTask;
|
||||
import com.cloudcraftgaming.discal.module.misc.StatusChanger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Timer;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 3/5/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class TimeManager {
|
||||
private static TimeManager instance;
|
||||
|
||||
private final ArrayList<Timer> timers = new ArrayList<>();
|
||||
|
||||
private TimeManager() {
|
||||
} //Prevent initialization
|
||||
|
||||
/**
|
||||
* Gets the instance of the TimeManager that is loaded.
|
||||
* @return The instance of the TimeManager.
|
||||
*/
|
||||
public static TimeManager getManager() {
|
||||
if (instance == null) {
|
||||
instance = new TimeManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the TimeManager and schedules the appropriate Timers.
|
||||
*/
|
||||
public void init() {
|
||||
Timer timer = new Timer(true);
|
||||
timer.schedule(new StatusChanger(), 10 * 1000, 10 * 1000);
|
||||
timer.schedule(new TimedUpdate(), 60 * 60 * 1000, 60 * 60 * 1000);
|
||||
|
||||
timers.add(timer);
|
||||
|
||||
Timer at = new Timer(true);
|
||||
at.schedule(new AnnouncementTask(), 5 * 1000 * 60, 5 * 1000 * 60);
|
||||
timers.add(at);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully shuts down the TimeManager and exits all timer threads preventing errors.
|
||||
*/
|
||||
public void shutdown() {
|
||||
for (Timer t : timers) {
|
||||
t.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.utils;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IChannel;
|
||||
import sx.blah.discord.handle.obj.IGuild;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 3/29/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class ChannelUtils {
|
||||
/**
|
||||
* Checks if the specified channel exists.
|
||||
* @param nameOrId The channel name or ID.
|
||||
* @param event The event received.
|
||||
* @return <code>true</code> if exists, else <code>false</code>.
|
||||
*/
|
||||
public static Boolean channelExists(String nameOrId, MessageReceivedEvent event) {
|
||||
if (nameOrId.contains("#")) {
|
||||
nameOrId = nameOrId.replace("#", "");
|
||||
}
|
||||
for (IChannel c : event.getGuild().getChannels()) {
|
||||
if (c.getName().equalsIgnoreCase(nameOrId) || c.getStringID().equals(nameOrId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the IChannel from its name.
|
||||
* @param nameOrId The channel name or ID.
|
||||
* @param event The event received.
|
||||
* @return the IChannel if successful, else <code>null</code>.
|
||||
*/
|
||||
public static IChannel getChannelFromNameOrId(String nameOrId, MessageReceivedEvent event) {
|
||||
if (nameOrId.contains("#")) {
|
||||
nameOrId = nameOrId.replace("#", "");
|
||||
}
|
||||
for (IChannel c : event.getGuild().getChannels()) {
|
||||
if (c.getName().equalsIgnoreCase(nameOrId) || c.getStringID().equals(nameOrId)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the IChannel from its name.
|
||||
* @param nameOrId The channel name or ID.
|
||||
* @param guildId The ID of the guild this channel belongs to.
|
||||
* @return the IChannel if successful, else <code>null</code>.
|
||||
*/
|
||||
public static IChannel getChannelFromNameOrId(String nameOrId, long guildId) {
|
||||
IGuild guild = Main.client.getGuildByID(guildId);
|
||||
if (nameOrId.contains("#")) {
|
||||
nameOrId = nameOrId.replace("#", "");
|
||||
}
|
||||
for (IChannel c : guild.getChannels()) {
|
||||
if (c.getName().equalsIgnoreCase(nameOrId) || c.getStringID().equals(nameOrId)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the IChannel from its name.
|
||||
* @param nameOrId The channel name or ID.
|
||||
* @param guildId The ID of the guild this channel belongs to.
|
||||
* @return the IChannel if successful, else <code>null</code>.
|
||||
*/
|
||||
public static String getChannelNameFromNameOrId(String nameOrId, long guildId) {
|
||||
IGuild guild = Main.client.getGuildByID(guildId);
|
||||
if (nameOrId.contains("#")) {
|
||||
nameOrId = nameOrId.replace("#", "");
|
||||
}
|
||||
for (IChannel c : guild.getChannels()) {
|
||||
if (c.getName().equalsIgnoreCase(nameOrId) || c.getStringID().equals(nameOrId)) {
|
||||
return c.getName();
|
||||
}
|
||||
}
|
||||
return "ERROR";
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.utils;
|
||||
|
||||
import com.cloudcraftgaming.discal.Main;
|
||||
import com.cloudcraftgaming.discal.api.database.DatabaseManager;
|
||||
import com.cloudcraftgaming.discal.api.object.GuildSettings;
|
||||
import com.cloudcraftgaming.discal.api.utils.ExceptionHandler;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IChannel;
|
||||
import sx.blah.discord.handle.obj.IRole;
|
||||
import sx.blah.discord.handle.obj.IUser;
|
||||
import sx.blah.discord.handle.obj.Permissions;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/19/17.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class PermissionChecker {
|
||||
/**
|
||||
* Checks if the user who sent the received message has the proper role to use a command.
|
||||
* @param event The Event received to check for the user and guild.
|
||||
* @return <code>true</code> if the user has the proper role, otherwise <code>false</code>.
|
||||
*/
|
||||
public static boolean hasSufficientRole(MessageReceivedEvent event) {
|
||||
//TODO: Figure out exactly what is causing a NPE here...
|
||||
try {
|
||||
GuildSettings settings = DatabaseManager.getManager().getSettings(event.getGuild().getLongID());
|
||||
if (!settings.getControlRole().equalsIgnoreCase("everyone")) {
|
||||
IUser sender = event.getMessage().getAuthor();
|
||||
String roleId = settings.getControlRole();
|
||||
IRole role = null;
|
||||
|
||||
for (IRole r : event.getMessage().getGuild().getRoles()) {
|
||||
if (r.getStringID().equals(roleId)) {
|
||||
role = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (role != null) {
|
||||
for (IRole r : sender.getRolesForGuild(event.getMessage().getGuild())) {
|
||||
if (r.getStringID().equals(role.getStringID()) || r.getPosition() > role.getPosition()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
//Role not found... reset Db...
|
||||
settings.setControlRole("everyone");
|
||||
DatabaseManager.getManager().updateSettings(settings);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//Something broke so we will harmlessly allow access and email the dev.
|
||||
ExceptionHandler.sendException(event.getMessage().getAuthor(), "Failed to check for sufficient control role.", e, PermissionChecker.class);
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean hasManageServerRole(MessageReceivedEvent event) {
|
||||
return event.getMessage().getAuthor().getPermissionsForGuild(event.getMessage().getGuild()).contains(
|
||||
Permissions.MANAGE_SERVER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user sent the command in a DisCal channel (if set).
|
||||
* @param event The event received to check for the correct channel.
|
||||
* @return <code>true</code> if in correct channel, otherwise <code>false</code>.
|
||||
*/
|
||||
public static boolean isCorrectChannel(MessageReceivedEvent event) {
|
||||
try {
|
||||
GuildSettings settings = DatabaseManager.getManager().getSettings(event.getGuild().getLongID());
|
||||
if (settings.getDiscalChannel().equalsIgnoreCase("all")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IChannel channel = null;
|
||||
for (IChannel c : event.getMessage().getGuild().getChannels()) {
|
||||
if (c.getStringID().equals(settings.getDiscalChannel())) {
|
||||
channel = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (channel != null) {
|
||||
return event.getMessage().getChannel().getStringID().equals(channel.getStringID());
|
||||
}
|
||||
|
||||
//If we got here, the channel no longer exists, reset data and return true.
|
||||
settings.setDiscalChannel("all");
|
||||
DatabaseManager.getManager().updateSettings(settings);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
//Catch any errors so that the bot always responds...
|
||||
ExceptionHandler.sendException(event.getMessage().getAuthor(), "Failed to check for discal channel.", e, PermissionChecker.class);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean botHasMessageManagePerms(MessageReceivedEvent event) {
|
||||
return Main.getSelfUser().getPermissionsForGuild(event.getMessage().getGuild()).contains(Permissions.MANAGE_MESSAGES);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.cloudcraftgaming.discal.utils;
|
||||
|
||||
import com.cloudcraftgaming.discal.api.enums.BadTimezone;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 4/7/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class TimeZoneUtils {
|
||||
public static boolean isValid(String value) {
|
||||
try {
|
||||
DateTimeZone tz = DateTimeZone.forID(value);
|
||||
return tz != null && !isBadTz(value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isBadTz(String value) {
|
||||
try {
|
||||
BadTimezone.valueOf(value.replaceAll("/", "_"));
|
||||
return true;
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user