Search in sources :

Example 1 with Announcement

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

the class DatabaseManager method getAnnouncement.

/**
 * Gets the {@link Announcement} Object with the corresponding ID for the specified Guild.
 *
 * @param announcementId The ID of the announcement.
 * @param guildId        The ID of the guild the Announcement belongs to.
 * @return The {@link Announcement} with the specified ID if it exists, otherwise <c>null</c>.
 */
public Announcement getAnnouncement(UUID announcementId, long guildId) {
    try {
        if (databaseInfo.getMySQL().checkConnection()) {
            String announcementTableName = String.format("%sannouncements", databaseInfo.getPrefix());
            String query = "SELECT * FROM " + announcementTableName + " WHERE ANNOUNCEMENT_ID = '" + announcementId.toString() + "';";
            PreparedStatement statement = databaseInfo.getConnection().prepareStatement(query);
            ResultSet res = statement.executeQuery();
            Boolean hasStuff = res.next();
            if (hasStuff && res.getString("ANNOUNCEMENT_ID") != null) {
                Announcement announcement = new Announcement(announcementId, guildId);
                announcement.setSubscriberRoleIdsFromString(res.getString("SUBSCRIBERS_ROLE"));
                announcement.setSubscriberUserIdsFromString(res.getString("SUBSCRIBERS_USER"));
                announcement.setAnnouncementChannelId(res.getString("CHANNEL_ID"));
                announcement.setAnnouncementType(AnnouncementType.valueOf(res.getString("ANNOUNCEMENT_TYPE")));
                announcement.setEventId(res.getString("EVENT_ID"));
                announcement.setEventColor(EventColor.fromNameOrHexOrID(res.getString("EVENT_COLOR")));
                announcement.setHoursBefore(res.getInt("HOURS_BEFORE"));
                announcement.setMinutesBefore(res.getInt("MINUTES_BEFORE"));
                announcement.setInfo(res.getString("INFO"));
                announcement.setEnabled(res.getBoolean("ENABLED"));
                announcement.setInfoOnly(res.getBoolean("INFO_ONLY"));
                statement.close();
                return announcement;
            }
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get announcement data.", e, this.getClass(), true);
        e.printStackTrace();
    }
    return null;
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Example 2 with Announcement

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

the class DatabaseManager method getAnnouncements.

/**
 * Gets an {@link ArrayList} of {@link Announcement}s belonging to the specific Guild.
 *
 * @param guildId The ID of the guild whose data is to be retrieved.
 * @return An ArrayList of Announcements that belong to the specified Guild.
 */
public ArrayList<Announcement> getAnnouncements(long guildId) {
    ArrayList<Announcement> announcements = new ArrayList<>();
    try {
        if (databaseInfo.getMySQL().checkConnection()) {
            String announcementTableName = String.format("%sannouncements", databaseInfo.getPrefix());
            String query = "SELECT * FROM " + announcementTableName + " WHERE GUILD_ID = '" + String.valueOf(guildId) + "';";
            PreparedStatement statement = databaseInfo.getConnection().prepareStatement(query);
            ResultSet res = statement.executeQuery();
            while (res.next()) {
                if (res.getString("ANNOUNCEMENT_ID") != null) {
                    Announcement announcement = new Announcement(UUID.fromString(res.getString("ANNOUNCEMENT_ID")), guildId);
                    announcement.setSubscriberRoleIdsFromString(res.getString("SUBSCRIBERS_ROLE"));
                    announcement.setSubscriberUserIdsFromString(res.getString("SUBSCRIBERS_USER"));
                    announcement.setAnnouncementChannelId(res.getString("CHANNEL_ID"));
                    announcement.setAnnouncementType(AnnouncementType.valueOf(res.getString("ANNOUNCEMENT_TYPE")));
                    announcement.setEventId(res.getString("EVENT_ID"));
                    announcement.setEventColor(EventColor.fromNameOrHexOrID(res.getString("EVENT_COLOR")));
                    announcement.setHoursBefore(res.getInt("HOURS_BEFORE"));
                    announcement.setMinutesBefore(res.getInt("MINUTES_BEFORE"));
                    announcement.setInfo(res.getString("INFO"));
                    announcement.setEnabled(res.getBoolean("ENABLED"));
                    announcement.setInfoOnly(res.getBoolean("INFO_ONLY"));
                    announcements.add(announcement);
                }
            }
            statement.close();
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get all guild announcements.", e, this.getClass(), true);
        e.printStackTrace();
    }
    return announcements;
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) ArrayList(java.util.ArrayList)

Example 3 with Announcement

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

the class DatabaseManager method getAnnouncements.

public ArrayList<Announcement> getAnnouncements() {
    ArrayList<Announcement> announcements = new ArrayList<>();
    try {
        if (databaseInfo.getMySQL().checkConnection()) {
            String announcementTableName = String.format("%sannouncements", databaseInfo.getPrefix());
            PreparedStatement stmt = databaseInfo.getConnection().prepareStatement("SELECT * FROM " + announcementTableName);
            ResultSet res = stmt.executeQuery();
            while (res.next()) {
                if (res.getString("ANNOUNCEMENT_ID") != null) {
                    Announcement announcement = new Announcement(UUID.fromString(res.getString("ANNOUNCEMENT_ID")), Long.valueOf(res.getString("GUILD_ID")));
                    announcement.setSubscriberRoleIdsFromString(res.getString("SUBSCRIBERS_ROLE"));
                    announcement.setSubscriberUserIdsFromString(res.getString("SUBSCRIBERS_USER"));
                    announcement.setAnnouncementChannelId(res.getString("CHANNEL_ID"));
                    announcement.setAnnouncementType(AnnouncementType.valueOf(res.getString("ANNOUNCEMENT_TYPE")));
                    announcement.setEventId(res.getString("EVENT_ID"));
                    announcement.setEventColor(EventColor.fromNameOrHexOrID(res.getString("EVENT_COLOR")));
                    announcement.setHoursBefore(res.getInt("HOURS_BEFORE"));
                    announcement.setMinutesBefore(res.getInt("MINUTES_BEFORE"));
                    announcement.setInfo(res.getString("INFO"));
                    announcement.setEnabled(res.getBoolean("ENABLED"));
                    announcement.setInfoOnly(res.getBoolean("INFO_ONLY"));
                    announcements.add(announcement);
                }
            }
            stmt.close();
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get all announcements.", e, this.getClass(), true);
    }
    return announcements;
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) ArrayList(java.util.ArrayList)

Example 4 with Announcement

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

the class CreatorCleaner method run.

@Override
public void run() {
    try {
        // 60 minutes
        long target = 60 * 1000 * 60;
        ArrayList<PreCalendar> cals = new ArrayList<>();
        ArrayList<PreEvent> events = new ArrayList<>();
        ArrayList<Announcement> ans = new ArrayList<>();
        // Run through calendar creator
        for (PreCalendar cal : CalendarCreator.getCreator().getAllPreCalendars()) {
            long difference = System.currentTimeMillis() - cal.getLastEdit();
            if (difference <= target) {
                // Last edited 60+ minutes ago, delete from creator and free up RAM.
                cals.add(cal);
            }
        }
        // Run through event creator
        for (PreEvent event : EventCreator.getCreator().getAllPreEvents()) {
            long difference = System.currentTimeMillis() - event.getLastEdit();
            if (difference <= target) {
                // Last edited 60+ minutes ago, delete from creator and free up RAM.
                events.add(event);
            }
        }
        // Run through announcement creator
        for (Announcement an : AnnouncementCreator.getCreator().getAllAnnouncements()) {
            long difference = System.currentTimeMillis() - an.getLastEdit();
            if (difference <= target) {
                // Last edited 60+ minutes ago, delete from creator and free up RAM.
                ans.add(an);
            }
        }
        // Okay, actually go through it all and delete
        for (PreCalendar c : cals) {
            CalendarCreator.getCreator().terminate(c.getGuildId());
        }
        for (PreEvent e : events) {
            EventCreator.getCreator().terminate(e.getGuildId());
        }
        for (Announcement a : ans) {
            AnnouncementCreator.getCreator().terminate(a.getGuildId());
        }
    } catch (Exception e) {
        Logger.getLogger().exception(null, "Error in CreatorCleaner", e, this.getClass(), true);
    }
}
Also used : PreCalendar(com.cloudcraftgaming.discal.api.object.calendar.PreCalendar) Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent) ArrayList(java.util.ArrayList)

Example 5 with Announcement

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

the class AnnouncementCreator method init.

// Functionals
/**
 * Initiates the creator for the guild involved.
 * @param e The event received upon init.
 * @return A new Announcement.
 */
public Announcement init(MessageReceivedEvent e, GuildSettings settings) {
    if (!hasAnnouncement(e.getGuild().getLongID())) {
        Announcement a = new Announcement(e.getGuild().getLongID());
        a.setAnnouncementChannelId(e.getChannel().getStringID());
        if (PermissionChecker.botHasMessageManagePerms(e)) {
            IMessage msg = Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Create.Init", settings), e);
            a.setCreatorMessage(msg);
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Notif.MANAGE_MESSAGES", settings), e);
        }
        announcements.add(a);
        return a;
    }
    return getAnnouncement(e.getGuild().getLongID());
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) IMessage(sx.blah.discord.handle.obj.IMessage)

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