use of sx.blah.discord.util.EmbedBuilder in project DisCal-Discord-Bot by NovaFox161.
the class EventMessageFormatter method getCondensedEventEmbed.
/**
* 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();
}
use of sx.blah.discord.util.EmbedBuilder in project DisCal-Discord-Bot by NovaFox161.
the class EventMessageFormatter method getEventConfirmationEmbed.
/**
* 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();
}
use of sx.blah.discord.util.EmbedBuilder in project DisCal-Discord-Bot by NovaFox161.
the class CalendarMessageFormatter method getCalendarLinkEmbed.
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();
}
use of sx.blah.discord.util.EmbedBuilder in project DisCal-Discord-Bot by NovaFox161.
the class Authorization method pollForAuth.
void pollForAuth(Poll poll) {
GuildSettings settings = DatabaseManager.getManager().getSettings(poll.getGuild().getLongID());
try {
String body = "client_id=" + clientData.getClientId() + "&client_secret=" + clientData.getClientSecret() + "&code=" + poll.getDevice_code() + "&grant_type=http://oauth.net/grant_type/device/1.0";
// Execute
com.mashape.unirest.http.HttpResponse<JsonNode> response = Unirest.post("https://www.googleapis.com/oauth2/v4/token").header("Content-Type", "application/x-www-form-urlencoded").body(body).asJson();
// Handle response.
if (response.getStatus() == 403) {
// Handle access denied
Message.sendDirectMessage(MessageManager.getMessage("AddCalendar.Auth.Poll.Failure.Deny", settings), poll.getUser());
} else if (response.getStatus() == 400) {
try {
// See if auth is pending, if so, just reschedule.
Type type = new TypeToken<AuthPollResponseError>() {
}.getType();
AuthPollResponseError apre = new Gson().fromJson(response.getBody().toString(), type);
if (apre.error.equalsIgnoreCase("authorization_pending")) {
// Response pending
PollManager.getManager().scheduleNextPoll(poll);
} else if (apre.error.equalsIgnoreCase("expired_token")) {
Message.sendDirectMessage(MessageManager.getMessage("AddCalendar.Auth.Poll.Failure.Expired", settings), poll.getUser());
} else {
Message.sendDirectMessage(MessageManager.getMessage("Notification.Error.Network", settings), poll.getUser());
Logger.getLogger().debug(poll.getUser(), "Poll Failure!", "Status code: " + response.getStatus() + " | " + response.getStatusText() + " | " + response.getBody().toString(), this.getClass(), true);
}
} catch (Exception e) {
// Auth is not pending, error occurred.
Logger.getLogger().exception(poll.getUser(), "Failed to poll for authorization to google account.", e, this.getClass(), true);
Logger.getLogger().debug(poll.getUser(), "More info on failure", "Status code: " + response.getStatus() + " | " + response.getStatusText() + " | " + response.getBody().toString(), this.getClass(), true);
Message.sendDirectMessage(MessageManager.getMessage("Notification.Error.Network", settings), poll.getUser());
}
} else if (response.getStatus() == 429) {
// We got rate limited... oops. Let's just poll half as often.
poll.setInterval(poll.getInterval() * 2);
PollManager.getManager().scheduleNextPoll(poll);
} else if (response.getStatus() == HttpStatusCodes.STATUS_CODE_OK) {
// Access granted
Type type = new TypeToken<AuthPollResponseGrant>() {
}.getType();
AuthPollResponseGrant aprg = new Gson().fromJson(response.getBody().toString(), type);
// Save credentials securely.
GuildSettings gs = DatabaseManager.getManager().getSettings(poll.getGuild().getLongID());
AESEncryption encryption = new AESEncryption(gs);
gs.setEncryptedAccessToken(encryption.encrypt(aprg.access_token));
gs.setEncryptedRefreshToken(encryption.encrypt(aprg.refresh_token));
DatabaseManager.getManager().updateSettings(gs);
try {
Calendar service = CalendarAuth.getCalendarService(gs);
List<CalendarListEntry> items = service.calendarList().list().setMinAccessRole("writer").execute().getItems();
Message.sendDirectMessage(MessageManager.getMessage("AddCalendar.Auth.Poll.Success", settings), poll.getUser());
for (CalendarListEntry i : items) {
if (!i.isDeleted()) {
EmbedBuilder em = new EmbedBuilder();
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
em.withAuthorName("DisCal");
em.withTitle(MessageManager.getMessage("Embed.AddCalendar.List.Title", settings));
em.appendField(MessageManager.getMessage("Embed.AddCalendar.List.Name", settings), i.getSummary(), false);
em.appendField(MessageManager.getMessage("Embed.AddCalendar.List.TimeZone", settings), i.getTimeZone(), false);
em.appendField(MessageManager.getMessage("Embed.AddCalendar.List.ID", settings), i.getId(), false);
em.withUrl(CalendarMessageFormatter.getCalendarLink(i.getId()));
em.withColor(56, 138, 237);
Message.sendDirectMessage(em.build(), poll.getUser());
}
}
// Response will be handled in guild, and will check. We already saved the tokens anyway.
} catch (IOException e1) {
// Failed to get calendars list and check for calendars.
Logger.getLogger().exception(poll.getUser(), "Failed to list calendars from external account!", e1, this.getClass(), true);
Message.sendDirectMessage(MessageManager.getMessage("AddCalendar.Auth.Poll.Failure.ListCalendars", settings), poll.getUser());
}
} else {
// Unknown network error...
Message.sendDirectMessage(MessageManager.getMessage("Notification.Error.Network", settings), poll.getUser());
Logger.getLogger().debug(poll.getUser(), "Network error; poll failure", "Status code: " + response.getStatus() + " | " + response.getStatusText() + " | " + response.getBody().toString(), this.getClass(), true);
}
} catch (Exception e) {
// Handle exception.
Logger.getLogger().exception(poll.getUser(), "Failed to poll for authorization to google account", e, this.getClass(), true);
Message.sendDirectMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), poll.getUser());
}
}
use of sx.blah.discord.util.EmbedBuilder in project DisCal-Discord-Bot by NovaFox161.
the class TimeCommand method calendarTime.
private void calendarTime(MessageReceivedEvent event, GuildSettings settings) {
try {
// TODO: Handle multiple calendars...
CalendarData data = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
if (data.getCalendarAddress().equalsIgnoreCase("primary")) {
// Does not have a calendar.
Message.sendMessage(MessageManager.getMessage("Creator.Calendar.NoCalendar", settings), event);
} else {
Calendar cal;
if (settings.useExternalCalendar()) {
cal = CalendarAuth.getCalendarService(settings).calendars().get(data.getCalendarAddress()).execute();
} else {
cal = CalendarAuth.getCalendarService().calendars().get(data.getCalendarAddress()).execute();
}
LocalDateTime ldt = LocalDateTime.now(ZoneId.of(cal.getTimeZone()));
// Okay... format and then we can go from there...
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a");
String thisIsTheCorrectTime = format.format(ldt);
// Build embed and send.
EmbedBuilder em = new EmbedBuilder();
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
em.withAuthorName("DisCal");
em.withTitle(MessageManager.getMessage("Embed.Time.Title", settings));
em.appendField(MessageManager.getMessage("Embed.Time.Time", settings), thisIsTheCorrectTime, false);
em.appendField(MessageManager.getMessage("Embed.Time.TimeZone", settings), cal.getTimeZone(), false);
em.withFooterText(MessageManager.getMessage("Embed.Time.Footer", settings));
em.withUrl(CalendarMessageFormatter.getCalendarLink(cal.getId()));
em.withColor(56, 138, 237);
Message.sendMessage(em.build(), event);
}
} catch (Exception e) {
Logger.getLogger().exception(event.getAuthor(), "Failed to connect to Google Cal.", e, this.getClass(), true);
Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
}
}
Aggregations