use of com.cloudcraftgaming.discal.api.object.GuildSettings in project DisCal-Discord-Bot by NovaFox161.
the class GuildEndpoint method getSettings.
public static String getSettings(Request request, Response response) {
try {
JSONObject jsonMain = new JSONObject(request.body());
Long guildId = jsonMain.getLong("guild_id");
GuildSettings settings = DatabaseManager.getManager().getSettings(guildId);
response.type("application/json");
response.status(200);
JSONObject body = new JSONObject();
body.put("external_calendar", settings.useExternalCalendar());
body.put("control_role", settings.getControlRole());
body.put("discal_channel", settings.getDiscalChannel());
body.put("simple_announcement", settings.usingSimpleAnnouncements());
body.put("lang", settings.getLang());
body.put("prefix", settings.getPrefix());
body.put("patron_guild", settings.isPatronGuild());
body.put("dev_guild", settings.isDevGuild());
body.put("max_calendars", settings.getMaxCalendars());
response.body(body.toString());
} catch (JSONException e) {
e.printStackTrace();
halt(400, "Bad Request");
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB-API] Internal get guild settings error", e, GuildEndpoint.class, true);
halt(500, "Internal Server Error");
}
return response.body();
}
use of com.cloudcraftgaming.discal.api.object.GuildSettings in project DisCal-Discord-Bot by NovaFox161.
the class PermissionChecker method hasSufficientRole.
/**
* Checks if the user who sent the received message has the proper role to use a command.
*
* @param event The Event received to check for the user and guild.
* @return <code>true</code> if the user has the proper role, otherwise <code>false</code>.
*/
public static boolean hasSufficientRole(MessageReceivedEvent event) {
// TODO: Figure out exactly what is causing a NPE here...
try {
GuildSettings settings = DatabaseManager.getManager().getSettings(event.getGuild().getLongID());
if (!settings.getControlRole().equalsIgnoreCase("everyone")) {
IUser sender = event.getMessage().getAuthor();
String roleId = settings.getControlRole();
IRole role = null;
for (IRole r : event.getMessage().getGuild().getRoles()) {
if (r.getStringID().equals(roleId)) {
role = r;
break;
}
}
if (role != null) {
for (IRole r : sender.getRolesForGuild(event.getMessage().getGuild())) {
if (r.getStringID().equals(role.getStringID()) || r.getPosition() > role.getPosition()) {
return true;
}
}
return false;
} else {
// Role not found... reset Db...
settings.setControlRole("everyone");
DatabaseManager.getManager().updateSettings(settings);
return true;
}
}
} catch (Exception e) {
// Something broke so we will harmlessly allow access and email the dev.
Logger.getLogger().exception(event.getMessage().getAuthor(), "Failed to check for sufficient control role.", e, PermissionChecker.class, true);
return true;
}
return true;
}
use of com.cloudcraftgaming.discal.api.object.GuildSettings 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);
}
}
use of com.cloudcraftgaming.discal.api.object.GuildSettings in project DisCal-Discord-Bot by NovaFox161.
the class DevCommand method modulePatron.
private void modulePatron(String[] args, MessageReceivedEvent event) {
if (args.length == 2) {
long guildId = Long.valueOf(args[1]);
if (Main.client.getGuildByID(guildId) != null) {
GuildSettings settings = DatabaseManager.getManager().getSettings(guildId);
settings.setPatronGuild(!settings.isPatronGuild());
Boolean isPatron = settings.isPatronGuild();
DatabaseManager.getManager().updateSettings(settings);
Message.sendMessage("Guild with ID: `" + guildId + "` is patron set to: `" + isPatron + "`", event);
} else {
Message.sendMessage("Guild not found or is not connected to DisCal!", event);
}
} else {
Message.sendMessage("Please specify the ID of the guild to set as a patron guild with `!dev patron <ID>`", event);
}
}
use of com.cloudcraftgaming.discal.api.object.GuildSettings in project DisCal-Discord-Bot by NovaFox161.
the class DevCommand method moduleDevGuild.
private void moduleDevGuild(String[] args, MessageReceivedEvent event) {
if (args.length == 2) {
long guildId = Long.valueOf(args[1]);
if (Main.client.getGuildByID(guildId) != null) {
GuildSettings settings = DatabaseManager.getManager().getSettings(guildId);
settings.setDevGuild(!settings.isDevGuild());
Boolean isPatron = settings.isDevGuild();
DatabaseManager.getManager().updateSettings(settings);
Message.sendMessage("Guild with ID: `" + guildId + "` is dev guild set to: `" + isPatron + "`", event);
} else {
Message.sendMessage("Guild not found or is not connected to DisCal!", event);
}
} else {
Message.sendMessage("Please specify the ID of the guild to set as a dev guild with `!dev dev <ID>`", event);
}
}
Aggregations