Search in sources :

Example 16 with Announcement

use of com.cloudcraftgaming.discal.api.object.announcement.Announcement in project DisCal-Discord-Bot by NovaFox161.

the class AnnouncementTask method run.

@Override
public void run() {
    Logger.getLogger().announcement("Starting announcement loop!");
    try {
        // Get the default stuff.
        try {
            discalService = CalendarAuth.getCalendarService();
        } catch (IOException e) {
            Logger.getLogger().exception(null, "Failed to get service! 00a0101", e, this.getClass(), true);
        }
        // NOTE: This list EXCLUDES disabled announcements!!!!!!!
        ArrayList<Announcement> allAnnouncements = DatabaseManager.getManager().getEnabledAnnouncements();
        for (Announcement a : allAnnouncements) {
            Logger.getLogger().announcement("starting an announcement", a.getGuildId() + "", a.getAnnouncementId() + "", "N/a");
            // Check if guild is part of DisCal's guilds. This way we can clear out the database...
            if (!GuildUtils.active(a.getGuildId())) {
                DatabaseManager.getManager().deleteAnnouncement(a.getAnnouncementId().toString());
                continue;
            }
            // Get everything we need ready.
            GuildSettings settings = getSettings(a);
            CalendarData calendar = getCalendarData(a);
            Calendar service;
            try {
                service = getService(settings);
            } catch (Exception e) {
                Logger.getLogger().exception(null, "Failed to handle custom service! 00a102", e, this.getClass(), true);
                continue;
            }
            // Now we can check the announcement type and do all the actual logic here.
            switch(a.getAnnouncementType()) {
                case SPECIFIC:
                    if (EventUtils.eventExists(settings, a.getEventId())) {
                        try {
                            Event e = service.events().get(calendar.getCalendarId(), a.getEventId()).execute();
                            if (inRange(a, e)) {
                                // We can announce it.
                                AnnouncementMessageFormatter.sendAnnouncementMessage(a, e, calendar, settings);
                                // And now lets delete it
                                DatabaseManager.getManager().deleteAnnouncement(a.getAnnouncementId().toString());
                            }
                        } catch (IOException e) {
                            // Event getting error, we know it exists tho
                            Logger.getLogger().exception(null, "Failed to get event! 00a103", e, this.getClass(), true);
                        }
                    } else {
                        // Event is gone, we can just delete this shit.
                        DatabaseManager.getManager().deleteAnnouncement(a.getAnnouncementId().toString());
                    }
                    break;
                case UNIVERSAL:
                    for (Event e : getEvents(settings, calendar, service, a)) {
                        if (inRange(a, e)) {
                            // It fits! Let's do it!
                            AnnouncementMessageFormatter.sendAnnouncementMessage(a, e, calendar, settings);
                        }
                    }
                    break;
                case COLOR:
                    for (Event e : getEvents(settings, calendar, service, a)) {
                        if (a.getEventColor() == EventColor.fromNameOrHexOrID(e.getColorId())) {
                            if (inRange(a, e)) {
                                // It fits! Let's do it!
                                AnnouncementMessageFormatter.sendAnnouncementMessage(a, e, calendar, settings);
                            }
                        }
                    }
                    break;
                case RECUR:
                    for (Event e : getEvents(settings, calendar, service, a)) {
                        if (inRange(a, e)) {
                            if (e.getId().contains("_") && e.getId().split("_")[0].equals(a.getEventId())) {
                                // It fits! Lets announce!
                                AnnouncementMessageFormatter.sendAnnouncementMessage(a, e, calendar, settings);
                            }
                        }
                    }
                    break;
            }
            Logger.getLogger().announcement("finished an announcement", a.getGuildId() + "", a.getAnnouncementId() + "", "N/a");
        }
        // Just clear everything immediately.
        allSettings.clear();
        calendars.clear();
        customServices.clear();
        allEvents.clear();
        Logger.getLogger().announcement("Finished announcement loop!");
    } catch (Exception e) {
        Logger.getLogger().exception(null, "SOMETHING BAD IN THE ANNOUNCER!!!!! PANIC!!!", e, this.getClass(), true);
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar) Event(com.google.api.services.calendar.model.Event) IOException(java.io.IOException) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings) IOException(java.io.IOException)

Example 17 with Announcement

use of com.cloudcraftgaming.discal.api.object.announcement.Announcement in project DisCal-Discord-Bot by NovaFox161.

the class AnnouncementCommand method moduleEdit.

private void moduleEdit(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    long guildId = event.getGuild().getLongID();
    if (!AnnouncementCreator.getCreator().hasAnnouncement(guildId)) {
        if (args.length == 2) {
            String anId = args[1];
            if (AnnouncementUtils.announcementExists(anId, event)) {
                Announcement announcement = AnnouncementCreator.getCreator().edit(event, anId, settings);
                if (announcement.getCreatorMessage() == null) {
                    Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(announcement, settings), MessageManager.getMessage("Creator.Announcement.Edit.Init", settings), event);
                } else {
                    Message.deleteMessage(event);
                }
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.CannotFind.Announcement", settings), event);
            }
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Edit.Specify", settings), event);
        }
    } else {
        if (AnnouncementCreator.getCreator().hasCreatorMessage(guildId)) {
            Message.deleteMessage(AnnouncementCreator.getCreator().getCreatorMessage(guildId));
            Message.deleteMessage(event);
            AnnouncementCreator.getCreator().setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(AnnouncementCreator.getCreator().getAnnouncement(guildId), settings), MessageManager.getMessage("Creator.Announcement.AlreadyInit", settings), event));
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Announcement.AlreadyInit", settings), event);
        }
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Example 18 with Announcement

use of com.cloudcraftgaming.discal.api.object.announcement.Announcement in project DisCal-Discord-Bot by NovaFox161.

the class AnnouncementCommand method moduleInfoOnly.

private void moduleInfoOnly(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    long guildId = event.getGuild().getLongID();
    if (args.length == 2) {
        if (AnnouncementCreator.getCreator().hasAnnouncement(guildId)) {
            Message.sendMessage(MessageManager.getMessage("Announcement.InfoOnly.Creator", settings), event);
        } else {
            String value = args[1];
            if (!AnnouncementUtils.announcementExists(value, event)) {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.CannotFind.Announcement", settings), event);
            } else {
                Announcement a = DatabaseManager.getManager().getAnnouncement(UUID.fromString(value), guildId);
                a.setInfoOnly(!a.isInfoOnly());
                DatabaseManager.getManager().updateAnnouncement(a);
                Message.sendMessage(MessageManager.getMessage("Announcement.InfoOnly.Success", "%value%", a.isInfoOnly() + "", settings), event);
            }
        }
    } else {
        Message.sendMessage(MessageManager.getMessage("Announcement.InfoOnly.Specify", settings), event);
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Example 19 with Announcement

use of com.cloudcraftgaming.discal.api.object.announcement.Announcement in project DisCal-Discord-Bot by NovaFox161.

the class AnnouncementCommand method moduleUnsubscribeRewriteArgsTwo.

private void moduleUnsubscribeRewriteArgsTwo(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    IMessage message = event.getMessage();
    IGuild guild = message.getGuild();
    IUser user = message.getAuthor();
    String value = args[1];
    if (args[1].length() <= 32) {
        if (AnnouncementCreator.getCreator().hasAnnouncement(guild.getLongID())) {
            Announcement a = AnnouncementCreator.getCreator().getAnnouncement(guild.getLongID());
            IUser u = guild.getUserByID(UserUtils.getUser(value.matches("<@?!?#?&?[0-9]+>") ? value.replaceAll("<@?!?#?&?[0-9]+>", "") : value, event.getGuild()));
            IRole r = guild.getRoleByID(RoleUtils.getRole(value.matches("<@?!?#?&?[0-9]+>") ? value.replaceAll("<@?!?#?&?[0-9]+>", "") : value, event.getGuild()));
            if (value.equalsIgnoreCase("everyone") || value.equalsIgnoreCase("here")) {
                String men = value.toLowerCase();
                if (a.getSubscriberRoleIds().contains(men)) {
                    a.getSubscriberRoleIds().remove(men);
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Success", "%value%", men, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Success", "%value%", men, settings), event);
                    }
                } else {
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Not", "%value%", men, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Not", "%value%", men, settings), event);
                    }
                }
            } else if (u != null) {
                String username = u.getName();
                if (a.getSubscriberUserIds().contains(u.getStringID())) {
                    a.getSubscriberUserIds().remove(u.getStringID());
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Success", "%value%", username, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Success", "%value%", username, settings), event);
                    }
                } else {
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Not", "%value%", username, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Not", "%value%", username, settings), event);
                    }
                }
            } else if (r != null) {
                String username = r.getName();
                if (a.getSubscriberRoleIds().contains(r.getStringID())) {
                    a.getSubscriberRoleIds().remove(r.getStringID());
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Success", "%value%", username, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Success", "%value%", username, settings), event);
                    }
                } else {
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Not", "%value%", username, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Not", "%value%", username, settings), event);
                    }
                }
            }
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Specify", settings), event);
        }
    } else {
        if (AnnouncementUtils.announcementExists(value, event)) {
            String senderId = user.getStringID();
            Announcement a = DatabaseManager.getManager().getAnnouncement(UUID.fromString(value), guild.getLongID());
            if (!a.getSubscriberUserIds().contains(senderId)) {
                a.getSubscriberUserIds().remove(senderId);
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Self.Success", settings), event);
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Self.Not", settings), event);
            }
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Announcement.CannotFind.Announcement", settings), event);
        }
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Example 20 with Announcement

use of com.cloudcraftgaming.discal.api.object.announcement.Announcement in project DisCal-Discord-Bot by NovaFox161.

the class AnnouncementCommand method moduleSubscribeRewriteArgsTwo.

private void moduleSubscribeRewriteArgsTwo(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    IMessage message = event.getMessage();
    IGuild guild = message.getGuild();
    IUser user = message.getAuthor();
    String value = args[1];
    if (args[1].length() <= 32) {
        if (AnnouncementCreator.getCreator().hasAnnouncement(guild.getLongID())) {
            Announcement a = AnnouncementCreator.getCreator().getAnnouncement(guild.getLongID());
            IUser u = guild.getUserByID(UserUtils.getUser(value, message));
            IRole r = guild.getRoleByID(RoleUtils.getRole(value, message));
            if (value.equalsIgnoreCase("everyone") || value.equalsIgnoreCase("here")) {
                String men = value.toLowerCase();
                if (!a.getSubscriberRoleIds().contains(men)) {
                    a.getSubscriberRoleIds().add(men);
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Success", "%value%", men, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Success", "%value%", men, settings), event);
                    }
                } else {
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Already", "%value%", men, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Already", "%value%", men, settings), event);
                    }
                }
            } else if (u != null) {
                String username = u.getName();
                if (!a.getSubscriberUserIds().contains(u.getStringID())) {
                    a.getSubscriberUserIds().add(u.getStringID());
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Success", "%value%", username, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Success", "%value%", username, settings), event);
                    }
                } else {
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Already", "%value%", username, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Already", "%value%", username, settings), event);
                    }
                }
            } else if (r != null) {
                String username = r.getName();
                if (!a.getSubscriberRoleIds().contains(r.getStringID())) {
                    a.getSubscriberRoleIds().add(r.getStringID());
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Success", "%value%", username, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Success", "%value%", username, settings), event);
                    }
                } else {
                    if (a.getCreatorMessage() != null) {
                        Message.deleteMessage(a.getCreatorMessage());
                        Message.deleteMessage(event);
                        a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Already", "%value%", username, settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Other.Already", "%value%", username, settings), event);
                    }
                }
            }
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Self.Specify", settings), event);
        }
    } else {
        if (AnnouncementUtils.announcementExists(value, event)) {
            String senderId = user.getStringID();
            Announcement a = DatabaseManager.getManager().getAnnouncement(UUID.fromString(value), guild.getLongID());
            if (!a.getSubscriberUserIds().contains(senderId)) {
                a.getSubscriberUserIds().add(senderId);
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Self.Success", settings), event);
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Self.Already", settings), event);
            }
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Announcement.CannotFind.Announcement", settings), event);
        }
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Aggregations

Announcement (com.cloudcraftgaming.discal.api.object.announcement.Announcement)28 ArrayList (java.util.ArrayList)7 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)4 IMessage (sx.blah.discord.handle.obj.IMessage)3 WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)2 AnnouncementType (com.cloudcraftgaming.discal.api.enums.announcement.AnnouncementType)1 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)1 AnnouncementCreatorResponse (com.cloudcraftgaming.discal.api.object.announcement.AnnouncementCreatorResponse)1 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)1 PreCalendar (com.cloudcraftgaming.discal.api.object.calendar.PreCalendar)1 PreEvent (com.cloudcraftgaming.discal.api.object.event.PreEvent)1 Calendar (com.google.api.services.calendar.Calendar)1 Event (com.google.api.services.calendar.model.Event)1 IOException (java.io.IOException)1