use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData in project DisCal-Discord-Bot by NovaFox161.
the class LinkCalendarCommand method issueCommand.
/**
* Issues the command this Object is responsible for.
* @param args The command arguments.
* @param event The event received.
* @return <code>true</code> if successful, else <code>false</code>.
*/
@Override
public Boolean issueCommand(String[] args, 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();
}
Message.sendMessage(CalendarMessageFormatter.getCalendarLinkEmbed(cal, settings), 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);
}
return false;
}
use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData 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);
}
}
use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData in project DisCal-Discord-Bot by NovaFox161.
the class DatabaseManager method getAllCalendars.
public ArrayList<CalendarData> getAllCalendars() {
ArrayList<CalendarData> calendars = new ArrayList<>();
try {
if (databaseInfo.getMySQL().checkConnection()) {
String calendarTableName = String.format("%scalendars", databaseInfo.getPrefix());
PreparedStatement stmt = databaseInfo.getConnection().prepareStatement("SELECT * FROM " + calendarTableName);
ResultSet res = stmt.executeQuery();
while (res.next()) {
CalendarData calData = new CalendarData(Long.valueOf(res.getString("GUILD_ID")), res.getInt("CALENDAR_NUMBER"));
calData.setCalendarId(res.getString("CALENDAR_ID"));
calData.setCalendarAddress(res.getString("CALENDAR_ADDRESS"));
calData.setExternal(res.getBoolean("EXTERNAL"));
calendars.add(calData);
}
stmt.close();
}
} catch (SQLException e) {
Logger.getLogger().exception(null, "Failed to get all calendars!", e, this.getClass(), true);
}
return calendars;
}
use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData in project DisCal-Discord-Bot by NovaFox161.
the class DatabaseManager method getMainCalendar.
public CalendarData getMainCalendar(long guildId) {
CalendarData calData = new CalendarData(guildId, 1);
try {
if (databaseInfo.getMySQL().checkConnection()) {
String calendarTableName = String.format("%scalendars", databaseInfo.getPrefix());
String query = "SELECT * FROM " + calendarTableName + " WHERE GUILD_ID = '" + String.valueOf(guildId) + "';";
PreparedStatement statement = databaseInfo.getConnection().prepareStatement(query);
ResultSet res = statement.executeQuery();
while (res.next()) {
if (res.getInt("CALENDAR_NUMBER") == 1) {
calData.setCalendarId(res.getString("CALENDAR_ID"));
calData.setCalendarAddress(res.getString("CALENDAR_ADDRESS"));
calData.setExternal(res.getBoolean("EXTERNAL"));
break;
}
}
statement.close();
}
} catch (SQLException e) {
Logger.getLogger().exception(null, "Failed to get calendar settings.", e, this.getClass(), true);
}
return calData;
}
use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData in project DisCal-Discord-Bot by NovaFox161.
the class CalendarEndpoint method listCalendars.
public static String listCalendars(Request request, Response response) {
try {
JSONObject jsonMain = new JSONObject(request.body());
Long guildId = jsonMain.getLong("guild_id");
ArrayList<JSONObject> cals = new ArrayList<>();
for (CalendarData cal : DatabaseManager.getManager().getAllCalendars(guildId)) {
if (!cal.getCalendarAddress().equalsIgnoreCase("primary")) {
JSONObject body = new JSONObject();
body.put("number", cal.getCalendarNumber());
body.put("external", cal.isExternal());
body.put("id", cal.getCalendarId());
body.put("address", cal.getCalendarAddress());
cals.add(body);
}
}
JSONObject body = new JSONObject();
body.put("count", cals.size());
body.put("calendars", cals);
response.type("application/json");
response.status(200);
response.body(body.toString());
} catch (JSONException e) {
e.printStackTrace();
halt(400, "Bad Request");
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB-API] Internal list calendars error", e, CalendarEndpoint.class, true);
halt(500, "Internal Server Error");
}
return response.body();
}
Aggregations