Search in sources :

Example 1 with GuildSettings

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

the class CommandListener method onMessageEvent.

/**
 * Checks for command validity and calls the command executor if valid.
 * @param event The event received to check for a command.
 */
@EventSubscriber
public void onMessageEvent(MessageReceivedEvent event) {
    try {
        if (event.getMessage() != null && event.getGuild() != null && event.getChannel() != null && !event.getChannel().isPrivate() && event.getMessage().getContent() != null && event.getMessage().getContent().length() > 0) {
            // Message is a valid guild message (not DM). Check if in correct channel.
            GuildSettings settings = DatabaseManager.getManager().getSettings(event.getGuild().getLongID());
            if (event.getMessage().getContent().startsWith(settings.getPrefix())) {
                if (PermissionChecker.isCorrectChannel(event)) {
                    // Prefixed with ! which should mean it is a command, convert and confirm.
                    String[] argsOr = event.getMessage().getContent().split("\\s+");
                    if (argsOr.length > 1) {
                        ArrayList<String> argsOr2 = new ArrayList<>(Arrays.asList(argsOr).subList(1, argsOr.length));
                        String[] args = argsOr2.toArray(new String[argsOr2.size()]);
                        String command = argsOr[0].replace(settings.getPrefix(), "");
                        cmd.issueCommand(command, args, event, settings);
                    } else if (argsOr.length == 1) {
                        // Only command... no args.
                        cmd.issueCommand(argsOr[0].replace(settings.getPrefix(), ""), new String[0], event, settings);
                    }
                }
            } else if (!event.getMessage().mentionsEveryone() && !event.getMessage().mentionsHere() && (event.getMessage().toString().startsWith("<@" + Main.getSelfUser().getStringID() + ">") || event.getMessage().toString().startsWith("<@!" + Main.getSelfUser().getStringID() + ">"))) {
                if (PermissionChecker.isCorrectChannel(event)) {
                    String[] argsOr = event.getMessage().getContent().split("\\s+");
                    if (argsOr.length > 2) {
                        ArrayList<String> argsOr2 = new ArrayList<>(Arrays.asList(argsOr).subList(2, argsOr.length));
                        String[] args = argsOr2.toArray(new String[argsOr2.size()]);
                        String command = argsOr[1];
                        cmd.issueCommand(command, args, event, settings);
                    } else if (argsOr.length == 2) {
                        // No args...
                        cmd.issueCommand(argsOr[1], new String[0], event, settings);
                    } else if (argsOr.length == 1) {
                        // Only disCal mentioned...
                        cmd.issueCommand("DisCal", new String[0], event, settings);
                    }
                }
            }
        }
    } catch (Exception e) {
        Logger.getLogger().exception(event.getAuthor(), "Command error; event message: " + event.getMessage().getContent(), e, this.getClass(), true);
    }
}
Also used : ArrayList(java.util.ArrayList) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings) EventSubscriber(sx.blah.discord.api.events.EventSubscriber)

Example 2 with GuildSettings

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

the class DevCommand method moduleMaxCalendars.

private void moduleMaxCalendars(String[] args, MessageReceivedEvent event) {
    if (args.length == 3) {
        long guildId = Long.valueOf(args[1]);
        try {
            Integer mc = Integer.valueOf(args[2]);
            mc = Math.abs(mc);
            if (Main.client.getGuildByID(guildId) != null) {
                GuildSettings settings = DatabaseManager.getManager().getSettings(guildId);
                settings.setMaxCalendars(mc);
                DatabaseManager.getManager().updateSettings(settings);
                Message.sendMessage("Guild with ID: `" + guildId + "` max calendar count set to: `" + mc + "`", event);
            } else {
                Message.sendMessage("Guild not found or is not connected to DisCal!", event);
            }
        } catch (NumberFormatException e) {
            Message.sendMessage("Max Calendar amount must be a valid Integer!", event);
        }
    } else {
        Message.sendMessage("Please specify the ID of the guild and calendar amount with `!dev maxcal <ID> <amount>`", event);
    }
}
Also used : GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 3 with GuildSettings

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

the class EventEndpoint method deleteEvent.

public static String deleteEvent(Request request, Response response) {
    JSONObject requestBody = new JSONObject(request.body());
    String eventId = requestBody.getString("id");
    GuildSettings settings;
    // Check if logged in, else get guild ID from body.
    if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
        settings = g.getSettings();
    } else {
        Long guildId = requestBody.getLong("guild_id");
        settings = DatabaseManager.getManager().getSettings(guildId);
    }
    // okay, time to properly delete the event
    if (EventUtils.deleteEvent(settings, eventId)) {
        // Deleted!
        response.body(ResponseUtils.getJsonResponseMessage("Successfully deleted event!"));
    } else {
        // Oh nos! we failed >.<
        response.status(500);
        response.body(ResponseUtils.getJsonResponseMessage("Failed to delete event!"));
    }
    return response.body();
}
Also used : JSONObject(org.json.JSONObject) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 4 with GuildSettings

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

the class EventEndpoint method getEventsForMonth.

public static String getEventsForMonth(Request request, Response response) {
    JSONObject requestBody = new JSONObject(request.body());
    Integer daysInMonth = Integer.valueOf(requestBody.getString("DaysInMonth"));
    Long startEpoch = Long.valueOf(requestBody.getString("StartEpoch"));
    Long endEpoch = startEpoch + (86400000L * daysInMonth);
    GuildSettings settings;
    if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
        settings = g.getSettings();
    } else {
        long guildId = requestBody.getLong("guild_id");
        settings = DatabaseManager.getManager().getSettings(guildId);
    }
    // okay, lets actually get the month's events.
    try {
        Calendar service;
        if (settings.useExternalCalendar()) {
            service = CalendarAuth.getCalendarService(settings);
        } else {
            service = CalendarAuth.getCalendarService();
        }
        CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
        Events events = service.events().list(calendarData.getCalendarAddress()).setTimeMin(new DateTime(startEpoch)).setTimeMax(new DateTime(endEpoch)).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
        List<Event> items = events.getItems();
        List<JSONObject> eventsJson = new ArrayList<>();
        for (Event e : items) {
            JSONObject jo = new JSONObject();
            jo.put("id", e.getId());
            jo.put("epochStart", e.getStart().getDateTime().getValue());
            jo.put("epochEnd", e.getEnd().getDateTime().getValue());
            eventsJson.add(jo);
        }
        JSONObject body = new JSONObject();
        body.put("events", eventsJson);
        body.put("count", eventsJson.size() + "");
        response.body(body.toString());
    } catch (Exception e) {
        response.body("Internal server error!");
        Logger.getLogger().exception(null, "[WEB] Failed to retrieve events for a month.", e, EventEndpoint.class, true);
        return response.body();
    }
    return response.body();
}
Also used : Calendar(com.google.api.services.calendar.Calendar) ArrayList(java.util.ArrayList) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) JSONObject(org.json.JSONObject) Events(com.google.api.services.calendar.model.Events) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Event(com.google.api.services.calendar.model.Event) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 5 with GuildSettings

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

the class GuildEndpoint method updateSettings.

public static String updateSettings(Request request, Response response) {
    try {
        JSONObject body = new JSONObject((request.body()));
        Long guildId = body.getLong("guild_id");
        GuildSettings settings = DatabaseManager.getManager().getSettings(guildId);
        if (body.has("control_role"))
            settings.setControlRole(body.getString("control_role"));
        if (body.has("discal_channel"))
            settings.setDiscalChannel(body.getString("discal_channel"));
        if (body.has("simple_announcement"))
            settings.setSimpleAnnouncements(body.getBoolean("simple_announcement"));
        if (body.has("lang"))
            settings.setLang(body.getString("lang"));
        if (body.has("prefix"))
            settings.setPrefix(body.getString("prefix"));
        if (DatabaseManager.getManager().updateSettings(settings)) {
            response.type("application/json");
            response.status(200);
            response.body(ResponseUtils.getJsonResponseMessage("Successfully updated guild settings!"));
        } else {
            response.type("application/json");
            response.status(500);
            response.body(ResponseUtils.getJsonResponseMessage("Failed to update settings!"));
        }
    } catch (JSONException e) {
        e.printStackTrace();
        halt(400, "Bad Request");
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB-API] Internal update guild settings error", e, GuildEndpoint.class, true);
        halt(500, "Internal Server Error");
    }
    return response.body();
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings) JSONException(org.json.JSONException)

Aggregations

GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)19 JSONObject (org.json.JSONObject)7 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)6 WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)6 Calendar (com.google.api.services.calendar.Calendar)6 Map (java.util.Map)6 Event (com.google.api.services.calendar.model.Event)5 DateTime (com.google.api.client.util.DateTime)4 EventDateTime (com.google.api.services.calendar.model.EventDateTime)4 ArrayList (java.util.ArrayList)4 EventData (com.cloudcraftgaming.discal.api.object.event.EventData)3 Recurrence (com.cloudcraftgaming.discal.api.object.event.Recurrence)3 JSONException (org.json.JSONException)3 Events (com.google.api.services.calendar.model.Events)2 IOException (java.io.IOException)2 AESEncryption (com.cloudcraftgaming.discal.api.crypto.AESEncryption)1 Announcement (com.cloudcraftgaming.discal.api.object.announcement.Announcement)1 AuthPollResponseError (com.cloudcraftgaming.discal.api.object.json.google.AuthPollResponseError)1 AuthPollResponseGrant (com.cloudcraftgaming.discal.api.object.json.google.AuthPollResponseGrant)1 WebCalendar (com.cloudcraftgaming.discal.api.object.web.WebCalendar)1