Search in sources :

Example 21 with Announcement

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

the class AnnouncementCommand method moduleSubscribe.

@Deprecated
@SuppressWarnings("unused")
private void moduleSubscribe(String[] args, MessageReceivedEvent event) {
    long guildId = event.getGuild().getLongID();
    if (args.length == 1) {
        Message.sendMessage("Please specify the ID of the announcement you wish to subscribe to!", event);
    } else if (args.length == 2) {
        String value = args[1];
        if (AnnouncementUtils.announcementExists(value, event)) {
            Announcement a = DatabaseManager.getManager().getAnnouncement(UUID.fromString(value), guildId);
            String senderId = event.getMessage().getAuthor().getStringID();
            if (!a.getSubscriberUserIds().contains(senderId)) {
                a.getSubscriberUserIds().add(senderId);
                DatabaseManager.getManager().updateAnnouncement(a);
                Message.sendMessage("You have subscribed to the announcement with the ID: `" + value + "`" + MessageUtils.lineBreak + "To unsubscribe use `!announcement unsubscribe <id>`", event);
            } else {
                Message.sendMessage("You are already subscribed to that event!", event);
            }
        } else {
            Message.sendMessage("Hmm.. it seems the specified announcement does not exist, are you sure you wrote the ID correctly?", event);
        }
    } else if (args.length == 3) {
        String value1 = args[1];
        String value2 = args[2];
        if (AnnouncementUtils.announcementExists(value1, event)) {
            Announcement a = DatabaseManager.getManager().getAnnouncement(UUID.fromString(value1), guildId);
            IUser user = event.getMessage().getGuild().getUserByID(UserUtils.getUser(value2, event.getMessage()));
            if (user != null) {
                // Valid user, let's add that user to the announcement.
                if (!a.getSubscriberUserIds().contains(user.getStringID())) {
                    String username = user.getDisplayName(event.getMessage().getGuild());
                    a.getSubscriberUserIds().add(user.getStringID());
                    DatabaseManager.getManager().updateAnnouncement(a);
                    Message.sendMessage("`" + username + "` has been subscribed to the announcement with the ID `" + a.getAnnouncementId() + "`" + MessageUtils.lineBreak + "To unsubscribe them use `!announcement unsubscribe <announcement ID> <mention>", event);
                } else {
                    Message.sendMessage("That user is already subscribed to the specified announcement! To unsubscribe them use `!announcement unsubscribe <announcement ID> <mention>`", event);
                }
            } else if (value2.equalsIgnoreCase("everyone") || value2.equalsIgnoreCase("here")) {
                // Here or everyone is to be subscribed...
                String men = value2.toLowerCase();
                if (!a.getSubscriberRoleIds().contains(men)) {
                    a.getSubscriberRoleIds().add(men);
                    DatabaseManager.getManager().updateAnnouncement(a);
                    Message.sendMessage("`" + men + "` has been subscribed to the announcement with the ID `" + a.getAnnouncementId() + "`" + MessageUtils.lineBreak + "To unsubscribe them use `!announcement unsubscribe <announcement ID> <value>", event);
                } else {
                    Message.sendMessage(men + " is already subscribed to the specified announcement! To unsubscribe them use `!announcement unsubscribe <announcement ID> <value>`", event);
                }
            } else {
                // User does not exist, see if a role.
                IRole role = RoleUtils.getRoleFromMention(value2, event);
                if (role != null) {
                    // Role valid, let's add that role to the announcement.
                    if (!a.getSubscriberRoleIds().contains(role.getStringID())) {
                        String roleName = role.getName();
                        a.getSubscriberRoleIds().add(role.getStringID());
                        DatabaseManager.getManager().updateAnnouncement(a);
                        Message.sendMessage("`" + roleName + "` has been subscribed to the announcement with the ID `" + a.getAnnouncementId() + "`" + MessageUtils.lineBreak + "To unsubscribe them use `!announcement unsubscribe <announcement ID> <mention>", event);
                    } else {
                        Message.sendMessage("That role is already subscribed to the specified announcement! To unsubscribe them use `!announcement unsubscribe <announcement ID> <mention>`", event);
                    }
                } else {
                    // Role does not exist...
                    Message.sendMessage("Role or user not found! Are you sure you typed them correctly?", event);
                }
            }
        } else {
            Message.sendMessage("Hmm.. it seems the specified announcement does not exist, are you sure you wrote the ID correctly?", event);
        }
    } else {
        Message.sendMessage("Please use `!announcement subscribe <ID>` or `!announcement subscribe <ID> <user mention/role mention/here/everyone>`", event);
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Example 22 with Announcement

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

the class AnnouncementCommand method moduleSubscribeRewriteArgsOne.

private void moduleSubscribeRewriteArgsOne(MessageReceivedEvent event, GuildSettings settings) {
    IMessage message = event.getMessage();
    IGuild guild = message.getGuild();
    IUser user = message.getAuthor();
    if (AnnouncementCreator.getCreator().hasAnnouncement(guild.getLongID())) {
        Announcement a = AnnouncementCreator.getCreator().getAnnouncement(guild.getLongID());
        String senderId = user.getStringID();
        if (!a.getSubscriberUserIds().contains(senderId)) {
            a.getSubscriberUserIds().add(senderId);
            if (a.getCreatorMessage() != null) {
                Message.deleteMessage(a.getCreatorMessage());
                Message.deleteMessage(event);
                a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Subscribe.Self.Success", settings), event));
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Self.Success", settings), event);
            }
        } else {
            // Announcement contains user ID
            if (a.getCreatorMessage() != null) {
                Message.deleteMessage(a.getCreatorMessage());
                Message.deleteMessage(event);
                a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Subscribe.Self.Already", settings), event));
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Self.Already", settings), event);
            }
        }
    } else {
        // User not creating an announcement
        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Subscribe.Self.Specify", settings), event);
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Example 23 with Announcement

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

the class AnnouncementCommand method moduleUnsubscribeRewriteArgsOne.

private void moduleUnsubscribeRewriteArgsOne(MessageReceivedEvent event, GuildSettings settings) {
    IMessage message = event.getMessage();
    IGuild guild = message.getGuild();
    IUser user = message.getAuthor();
    if (AnnouncementCreator.getCreator().hasAnnouncement(guild.getLongID())) {
        Announcement a = AnnouncementCreator.getCreator().getAnnouncement(guild.getLongID());
        String senderId = user.getStringID();
        if (a.getSubscriberUserIds().contains(senderId)) {
            a.getSubscriberUserIds().remove(senderId);
            if (a.getCreatorMessage() != null) {
                Message.deleteMessage(a.getCreatorMessage());
                Message.deleteMessage(event);
                a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Unsubscribe.Self.Success", settings), event));
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Self.Success", settings), event);
            }
        } else {
            // Announcement does not contain user ID
            if (a.getCreatorMessage() != null) {
                Message.deleteMessage(a.getCreatorMessage());
                Message.deleteMessage(event);
                a.setCreatorMessage(Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Unsubscribe.Self.Not", settings), event));
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Self.Not", settings), event);
            }
        }
    } else {
        // User not creating an announcement
        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Self.Specify", settings), event);
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Example 24 with Announcement

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

the class DatabaseManager method getEnabledAnnouncements.

public ArrayList<Announcement> getEnabledAnnouncements() {
    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 + " WHERE ENABLED = 1");
            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.setInfoOnly(res.getBoolean("INFO_ONLY"));
                    // The announcement is obviously enabled if we have gotten here lol
                    announcement.setEnabled(true);
                    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 25 with Announcement

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

the class AnnouncementEndpoint method listAnnouncements.

public static String listAnnouncements(Request request, Response response) {
    try {
        JSONObject jsonMain = new JSONObject(request.body());
        Long guildId = jsonMain.getLong("guild_id");
        Integer amount = jsonMain.getInt("amount");
        ArrayList<JSONObject> announcements = new ArrayList<>();
        if (amount == -1) {
            for (Announcement a : DatabaseManager.getManager().getAnnouncements(guildId)) {
                JSONObject obj = new JSONObject();
                obj.put("id", a.getAnnouncementId().toString());
                obj.put("channel", a.getAnnouncementChannelId());
                obj.put("event_id", a.getEventId());
                obj.put("event_color", a.getEventColor().name());
                obj.put("type", a.getAnnouncementType().name());
                obj.put("hours", a.getHoursBefore());
                obj.put("minutes", a.getMinutesBefore());
                obj.put("info", a.getInfo());
                obj.put("enabled", a.isEnabled());
                obj.put("info_only", a.isInfoOnly());
                obj.put("subscribers_role", a.getSubscriberRoleIds());
                obj.put("subscribers_user", a.getSubscriberUserIds());
                announcements.add(obj);
            }
        } else {
            int i = 0;
            for (Announcement a : DatabaseManager.getManager().getAnnouncements(guildId)) {
                if (i < amount) {
                    JSONObject obj = new JSONObject();
                    obj.put("id", a.getAnnouncementId().toString());
                    obj.put("channel", a.getAnnouncementChannelId());
                    obj.put("event_id", a.getEventId());
                    obj.put("event_color", a.getEventColor().name());
                    obj.put("type", a.getAnnouncementType().name());
                    obj.put("hours", a.getHoursBefore());
                    obj.put("minutes", a.getMinutesBefore());
                    obj.put("info", a.getInfo());
                    obj.put("enabled", a.isEnabled());
                    obj.put("info_only", a.isInfoOnly());
                    obj.put("subscribers_role", a.getSubscriberRoleIds());
                    obj.put("subscribers_user", a.getSubscriberUserIds());
                    announcements.add(obj);
                    i++;
                } else {
                    break;
                }
            }
        }
        JSONObject body = new JSONObject();
        body.put("amount", announcements.size());
        body.put("announcements", announcements);
        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 announcements error", e, AnnouncementEndpoint.class, true);
        halt(500, "Internal Server Error");
    }
    return response.body();
}
Also used : JSONObject(org.json.JSONObject) Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

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