use of ws.nmathe.saber.Main.getGuildSettingsManager in project Saber-Bot by notem.
the class ScheduleSyncer method run.
public void run() {
Logging.info(this.getClass(), "Running schedule syncer. . .");
Bson query = and(ne("sync_address", "off"), lte("sync_time", new Date()));
Main.getDBDriver().getScheduleCollection().find(query).projection(fields(include("_id", "sync_time", "sync_address", "sync_user", "guildId"))).forEach((Consumer<? super Document>) document -> {
executor.execute(() -> {
try {
String guildId = document.getString("guildId");
JDA jda = Main.getShardManager().getJDA(guildId);
if (jda == null)
return;
if (JDA.Status.valueOf("CONNECTED") != jda.getStatus())
return;
String scheduleId = document.getString("_id");
Date syncTime = Date.from(ZonedDateTime.ofInstant(document.getDate("sync_time").toInstant(), Main.getScheduleManager().getTimeZone(scheduleId)).plusDays(1).toInstant());
Main.getDBDriver().getScheduleCollection().updateOne(eq("_id", scheduleId), set("sync_time", syncTime));
String address = document.getString("sync_address");
Credential credential = document.get("sync_user") == null ? GoogleAuth.authorize() : GoogleAuth.getCredential(document.getString("sync_user"));
Calendar service = GoogleAuth.getCalendarService(credential);
TextChannel channel = jda.getTextChannelById(document.getString("_id"));
if (channel == null)
return;
if (Main.getCalendarConverter().checkValidAddress(address, service)) {
Main.getCalendarConverter().importCalendar(address, channel, service);
Logging.info(this.getClass(), "Synchronized schedule #" + channel.getName() + " [" + document.getString("_id") + "] on '" + channel.getGuild().getName() + "' [" + channel.getGuild().getId() + "]");
} else {
GuildSettingsManager.GuildSettings gs = Main.getGuildSettingsManager().getGuildSettings(guildId);
TextChannel control = Main.getShardManager().getJDA(guildId).getTextChannelById(gs.getCommandChannelId());
String content = "**Warning:** I failed to auto-sync <#" + scheduleId + "> to *" + address + "*!\n" + "Please make sure that the calendar address is still correct and that the calendar privacy settings have not changed!";
MessageUtilities.sendMsg(content, control, null);
Logging.warn(this.getClass(), "Failed to synchronize schedule #" + channel.getName() + " [" + document.getString("_id") + "] on '" + channel.getGuild().getName() + "' [" + channel.getGuild().getId() + "]");
}
} catch (Exception e) {
Logging.exception(this.getClass(), e);
}
});
});
}
Aggregations