Search in sources :

Example 11 with Announcement

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

the class AnnouncementCommand method moduleUnsubscribeRewriteArgsThree.

private void moduleUnsubscribeRewriteArgsThree(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    IMessage message = event.getMessage();
    IGuild guild = message.getGuild();
    List<String> subscribedUsers = new ArrayList<>();
    List<String> subscribedRoles = new ArrayList<>();
    String announcementID;
    boolean updateDb;
    if (args[1].length() > 32) {
        AnnouncementCreator.getCreator().getAnnouncement(guild.getLongID());
        updateDb = true;
        announcementID = args[1];
    } else {
        updateDb = false;
        if (AnnouncementCreator.getCreator().hasAnnouncement(guild.getLongID())) {
            announcementID = AnnouncementCreator.getCreator().getAnnouncement(guild.getLongID()).getAnnouncementId().toString();
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Specify", settings), event);
            return;
        }
    }
    if (AnnouncementUtils.announcementExists(announcementID, event) || !updateDb) {
        Announcement a = updateDb ? DatabaseManager.getManager().getAnnouncement(UUID.fromString(announcementID), guild.getLongID()) : AnnouncementCreator.getCreator().getAnnouncement(guild.getLongID());
        for (int i = 1; i < args.length; i++) {
            IUser u = guild.getUserByID(UserUtils.getUser(args[i].matches("<@?!?#?&?[0-9]+>") ? args[i].replaceAll("<@?!?#?&?[0-9]+>", "") : args[i], event.getGuild()));
            IRole r = guild.getRoleByID(RoleUtils.getRole(args[i].matches("<@?!?#?&?[0-9]+>") ? args[i].replaceAll("<@?!?#?&?[0-9]+>", "") : args[i], event.getGuild()));
            if (args[i].toLowerCase().contains("everyone") || args[i].toLowerCase().contains("here")) {
                // Here or everyone is to be subscribed...
                String men = args[i].toLowerCase();
                if (a.getSubscriberRoleIds().contains(men)) {
                    a.getSubscriberRoleIds().remove(men);
                    Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Success", "%value%", men, settings), event);
                    subscribedUsers.add(men);
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Unsubscribe.Other.Not", "%value%", men, settings), event);
                }
            }
            if (u != null) {
                if (a.getSubscriberUserIds().contains(u.getStringID())) {
                    subscribedUsers.add(u.getName());
                    a.getSubscriberUserIds().remove(u.getStringID());
                }
            } else if (r != null) {
                if (a.getSubscriberRoleIds().contains(r.getStringID())) {
                    subscribedRoles.add(r.getName());
                    a.getSubscriberRoleIds().remove(r.getStringID());
                }
            }
        }
        EmbedBuilder em = new EmbedBuilder();
        em.withColor(EventColor.TURQUOISE.getR(), EventColor.TURQUOISE.getG(), EventColor.TURQUOISE.getB());
        em.withAuthorIcon(event.getClient().getApplicationIconURL());
        em.withAuthorName(MessageManager.getMessage("Embed.Announcement.Unsubscribe.Title", settings));
        em.withDesc(MessageManager.getMessage("Embed.Announcement.Unsubscribe.Users", "%users%", subscribedUsers.toString(), settings) + MessageUtils.lineBreak + MessageManager.getMessage("Embed.Announcement.Unsubscribe.Roles", "%roles%", subscribedRoles.toString(), settings));
        em.withFooterText(MessageManager.getMessage("Embed.Announcement.Unsubscribe.Footer", "%id%", a.getAnnouncementId().toString(), settings));
        Message.sendMessage(em.build(), event);
        if (updateDb) {
            DatabaseManager.getManager().updateAnnouncement(a);
        }
    } else {
        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.CannotFind.Announcement", settings), event);
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement) ArrayList(java.util.ArrayList)

Example 12 with Announcement

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

the class AnnouncementCommand method moduleCopy.

private void moduleCopy(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    long guildId = event.getGuild().getLongID();
    if (args.length == 2) {
        String value = args[1];
        if (!AnnouncementCreator.getCreator().hasAnnouncement(guildId)) {
            if (AnnouncementUtils.announcementExists(value, event)) {
                Announcement a = AnnouncementCreator.getCreator().init(event, value, settings);
                if (a.getCreatorMessage() == null) {
                    Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Copy.Success", settings), event);
                }
            } else {
                Message.sendMessage(MessageManager.getMessage("Creator.Announcement.CannotFind.Announcement", 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);
            }
        }
    } else {
        Message.sendMessage(MessageManager.getMessage("Creator.Announcement.Copy.Specify", settings), event);
    }
}
Also used : Announcement(com.cloudcraftgaming.discal.api.object.announcement.Announcement)

Example 13 with Announcement

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

the class AnnouncementEndpoint method updateAnnouncement.

public static String updateAnnouncement(Request request, Response response) {
    try {
        JSONObject jsonMain = new JSONObject(request.body());
        Long guildId = jsonMain.getLong("guild_id");
        String announcementId = jsonMain.getString("id");
        Announcement a = DatabaseManager.getManager().getAnnouncement(UUID.fromString(announcementId), guildId);
        if (a != null) {
            JSONObject body = new JSONObject(request.body());
            if (body.has("channel"))
                a.setAnnouncementChannelId(body.getString("channel"));
            if (body.has("event_id"))
                a.setEventId(body.getString("event_id"));
            if (body.has("event_color"))
                a.setEventColor(EventColor.fromNameOrHexOrID(body.getString("event_color")));
            if (body.has("type"))
                a.setAnnouncementType(AnnouncementType.fromValue(body.getString("type")));
            if (body.has("hours"))
                a.setHoursBefore(body.getInt("hours"));
            if (body.has("minutes"))
                a.setMinutesBefore(body.getInt("minutes"));
            if (body.has("info"))
                a.setInfo(body.getString("info"));
            if (body.has("enabled"))
                a.setEnabled(body.getBoolean("enabled"));
            if (body.has("info_only")) {
                a.setInfoOnly(body.getBoolean("info_only"));
            }
            if (DatabaseManager.getManager().updateAnnouncement(a)) {
                response.type("application/json");
                response.status(200);
                response.body(ResponseUtils.getJsonResponseMessage("Successfully updated announcement"));
            } else {
                response.type("application/json");
                response.status(500);
                response.body(ResponseUtils.getJsonResponseMessage("Internal Server Error"));
            }
        } else {
            response.type("application/json");
            response.status(404);
            response.body(ResponseUtils.getJsonResponseMessage("Announcement not found"));
        }
    } catch (JSONException e) {
        e.printStackTrace();
        halt(400, "Bad Request");
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB-API] Internal update announcement 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) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

Example 14 with Announcement

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

the class AnnouncementEndpoint method createAnnouncement.

public static String createAnnouncement(Request request, Response response) {
    try {
        JSONObject jsonMain = new JSONObject(request.body());
        Long guildId = jsonMain.getLong("guild_id");
        Announcement a = new Announcement(guildId);
        JSONObject body = new JSONObject(request.body());
        a.setAnnouncementChannelId(body.getString("channel"));
        a.setAnnouncementType(AnnouncementType.fromValue(body.getString("type")));
        if (a.getAnnouncementType().equals(AnnouncementType.COLOR)) {
            a.setEventColor(EventColor.fromNameOrHexOrID(body.getString("event_color")));
        }
        if (a.getAnnouncementType().equals(AnnouncementType.RECUR) || a.getAnnouncementType().equals(AnnouncementType.SPECIFIC)) {
            a.setEventId(body.getString("event_id"));
        }
        a.setHoursBefore(body.getInt("hours"));
        a.setMinutesBefore(body.getInt("minutes"));
        a.setInfo(body.getString("info"));
        if (DatabaseManager.getManager().updateAnnouncement(a)) {
            response.type("application/json");
            response.status(200);
            JSONObject responseBody = new JSONObject();
            responseBody.put("Message", "Successfully created announcement");
            responseBody.put("id", a.getAnnouncementId().toString());
            response.body(responseBody.toString());
        } else {
            response.type("application/json");
            response.status(500);
            response.body(ResponseUtils.getJsonResponseMessage("Internal Server Error"));
        }
    } catch (JSONException e) {
        e.printStackTrace();
        halt(400, "Bad Request");
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB-API] Internal create announcement 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) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

Example 15 with Announcement

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

the class AnnouncementCreator method edit.

public Announcement edit(MessageReceivedEvent e, String announcementId, GuildSettings settings) {
    if (!hasAnnouncement(e.getGuild().getLongID()) && AnnouncementUtils.announcementExists(announcementId, e)) {
        Announcement edit = DatabaseManager.getManager().getAnnouncement(UUID.fromString(announcementId), e.getGuild().getLongID());
        // Copy
        Announcement a = new Announcement(edit, true);
        a.setEditing(true);
        if (PermissionChecker.botHasMessageManagePerms(e)) {
            IMessage msg = Message.sendMessage(AnnouncementMessageFormatter.getFormatAnnouncementEmbed(a, settings), MessageManager.getMessage("Creator.Announcement.Edit.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