Search in sources :

Example 1 with Main.getGuildSettingsManager

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);
            }
        });
    });
}
Also used : Document(org.bson.Document) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Date(java.util.Date) Projections.fields(com.mongodb.client.model.Projections.fields) Logging(ws.nmathe.saber.utils.Logging) ZonedDateTime(java.time.ZonedDateTime) Updates.set(com.mongodb.client.model.Updates.set) GoogleAuth(ws.nmathe.saber.core.google.GoogleAuth) Executors(java.util.concurrent.Executors) Projections.include(com.mongodb.client.model.Projections.include) Bson(org.bson.conversions.Bson) Consumer(java.util.function.Consumer) Filters(com.mongodb.client.model.Filters) GuildSettingsManager(ws.nmathe.saber.core.settings.GuildSettingsManager) MessageUtilities(ws.nmathe.saber.utils.MessageUtilities) JDA(net.dv8tion.jda.core.JDA) Credential(com.google.api.client.auth.oauth2.Credential) Calendar(com.google.api.services.calendar.Calendar) Main(ws.nmathe.saber.Main) Main.getGuildSettingsManager(ws.nmathe.saber.Main.getGuildSettingsManager) ExecutorService(java.util.concurrent.ExecutorService) Credential(com.google.api.client.auth.oauth2.Credential) TextChannel(net.dv8tion.jda.core.entities.TextChannel) JDA(net.dv8tion.jda.core.JDA) Calendar(com.google.api.services.calendar.Calendar) Date(java.util.Date) Bson(org.bson.conversions.Bson)

Aggregations

Credential (com.google.api.client.auth.oauth2.Credential)1 Calendar (com.google.api.services.calendar.Calendar)1 Filters (com.mongodb.client.model.Filters)1 Projections.fields (com.mongodb.client.model.Projections.fields)1 Projections.include (com.mongodb.client.model.Projections.include)1 Updates.set (com.mongodb.client.model.Updates.set)1 ZonedDateTime (java.time.ZonedDateTime)1 Date (java.util.Date)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Consumer (java.util.function.Consumer)1 JDA (net.dv8tion.jda.core.JDA)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 Document (org.bson.Document)1 Bson (org.bson.conversions.Bson)1 Main (ws.nmathe.saber.Main)1 Main.getGuildSettingsManager (ws.nmathe.saber.Main.getGuildSettingsManager)1 GoogleAuth (ws.nmathe.saber.core.google.GoogleAuth)1 GuildSettingsManager (ws.nmathe.saber.core.settings.GuildSettingsManager)1 Logging (ws.nmathe.saber.utils.Logging)1