mirror of
https://github.com/DreamExposure/DisCal-Discord-Bot.git
synced 2026-02-11 05:58:26 -06:00
Dannie - Parallelize Mono/Database Calls to Improve Response Times
This commit is contained in:
@@ -930,10 +930,10 @@ public class AnnouncementCommand implements ICommand {
|
||||
String value = args[1];
|
||||
if (!AnnouncementCreator.getCreator().hasAnnouncement(settings.getGuildID())) {
|
||||
if (value.equalsIgnoreCase("all")) {
|
||||
ArrayList<Announcement> announcements = DatabaseManager.getManager().getAnnouncements(settings.getGuildID());
|
||||
List<Announcement> announcements = DatabaseManager.getManager().getAnnouncements(settings.getGuildID());
|
||||
MessageManager.sendMessageAsync(MessageManager.getMessage("Creator.Announcement.List.All", "%amount%", announcements.size() + "", settings), event);
|
||||
//Loop and add embeds
|
||||
for (Announcement a: announcements) {
|
||||
for (Announcement a : announcements) {
|
||||
MessageManager.sendMessageAsync(AnnouncementMessageFormatter.getCondensedAnnouncementEmbed(a, settings), event);
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -1,275 +0,0 @@
|
||||
package org.dreamexposure.discal.client.network.discal.api.v1;
|
||||
|
||||
import org.dreamexposure.discal.client.message.MessageManager;
|
||||
import org.dreamexposure.discal.client.utils.GuildFinder;
|
||||
import org.dreamexposure.discal.core.database.DatabaseManager;
|
||||
import org.dreamexposure.discal.core.enums.network.DisCalRealm;
|
||||
import org.dreamexposure.discal.core.logger.Logger;
|
||||
import org.dreamexposure.discal.core.object.BotSettings;
|
||||
import org.dreamexposure.discal.core.object.GuildSettings;
|
||||
import org.dreamexposure.discal.core.object.web.WebGuild;
|
||||
import org.dreamexposure.discal.core.utils.GuildUtils;
|
||||
import org.dreamexposure.discal.core.utils.JsonUtils;
|
||||
import org.dreamexposure.discal.core.utils.PermissionChecker;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import discord4j.core.object.entity.Guild;
|
||||
import discord4j.core.object.entity.Member;
|
||||
import discord4j.core.object.util.Snowflake;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@SuppressWarnings({"Duplicates", "OptionalGetWithoutIsPresent"})
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/com")
|
||||
public class ComHandler {
|
||||
|
||||
@PostMapping(value = "/website/dashboard/guild", produces = "application/json")
|
||||
public String getWebsiteDashboardGuild(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody) {
|
||||
//Authorization
|
||||
if (request.getHeader("Authorization") == null || !request.getHeader("Authorization").equals(BotSettings.BOT_API_TOKEN.get())) {
|
||||
//Not authorized....
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(401);
|
||||
return JsonUtils.getJsonResponseMessage("Unauthorized. Only official DisCal network can use this!");
|
||||
}
|
||||
|
||||
//Requires us to grab data for guild and return a response containing the WebGuild with needed info
|
||||
JSONObject jsonRequest = new JSONObject(requestBody);
|
||||
JSONObject jsonResponse = new JSONObject();
|
||||
|
||||
Optional<Guild> guild = GuildFinder.findGuild(Snowflake.of(jsonRequest.getLong("guild_id")));
|
||||
|
||||
if (guild.isPresent()) {
|
||||
Member member = guild.get().getMemberById(Snowflake.of(jsonRequest.getLong("member_id"))).block();
|
||||
if (member != null) {
|
||||
WebGuild wg = new WebGuild().fromGuild(guild.get());
|
||||
wg.setDiscalRole(PermissionChecker.hasSufficientRole(member, wg.getSettings()).blockOptional().orElse(false));
|
||||
wg.setManageServer(PermissionChecker.hasManageServerRole(member).blockOptional().orElse(false));
|
||||
|
||||
jsonResponse.put("guild", new WebGuild().fromGuild(guild.get()).toJson(false));
|
||||
|
||||
response.setStatus(200);
|
||||
} else {
|
||||
jsonResponse.put("message", "Member not Found");
|
||||
response.setStatus(404);
|
||||
}
|
||||
} else {
|
||||
jsonResponse.put("message", "Guild not Found");
|
||||
response.setStatus(404);
|
||||
}
|
||||
response.setContentType("application/json");
|
||||
return jsonResponse.toString();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/website/dashboard/defaults", produces = "application/json")
|
||||
public String getWebsiteDashboardDefaults(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody) {
|
||||
//Authorization
|
||||
if (request.getHeader("Authorization") == null || !request.getHeader("Authorization").equals(BotSettings.BOT_API_TOKEN.get())) {
|
||||
//Not authorized....
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(401);
|
||||
return JsonUtils.getJsonResponseMessage("Unauthorized. Only official DisCal network can use this!");
|
||||
}
|
||||
|
||||
//Get guilds the user is in.
|
||||
JSONObject jsonRequest = new JSONObject(requestBody);
|
||||
JSONObject jsonResponse = new JSONObject();
|
||||
|
||||
Snowflake memId = Snowflake.of(jsonRequest.getLong("member_id"));
|
||||
JSONArray guildIds = jsonRequest.getJSONArray("guilds");
|
||||
List<Optional<Guild>> guilds = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < guildIds.length(); i++) {
|
||||
guilds.add(GuildFinder.findGuild(Snowflake.of(guildIds.getLong(i))));
|
||||
}
|
||||
|
||||
JSONArray webGuilds = new JSONArray();
|
||||
|
||||
for (Optional<Guild> g : guilds) {
|
||||
if (g.isPresent()) {
|
||||
WebGuild wg = new WebGuild().fromGuild(g.get());
|
||||
Member mem = g.get().getMemberById(memId).block();
|
||||
if (mem != null) {
|
||||
wg.setManageServer(PermissionChecker.hasManageServerRole(mem).blockOptional().orElse(false));
|
||||
wg.setDiscalRole(PermissionChecker.hasSufficientRole(mem, wg.getSettings()).blockOptional().orElse(false));
|
||||
}
|
||||
webGuilds.put(wg.toJson(false));
|
||||
}
|
||||
}
|
||||
|
||||
if (webGuilds.length() > 0) {
|
||||
jsonResponse.put("guilds", webGuilds);
|
||||
jsonResponse.put("count", webGuilds.length());
|
||||
response.setStatus(200);
|
||||
} else {
|
||||
jsonResponse.put("message", "Success, however, no listed guilds are connected.");
|
||||
response.setStatus(204);
|
||||
}
|
||||
response.setContentType("application/json");
|
||||
return jsonResponse.toString();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/website/embed/calendar", produces = "application/json")
|
||||
public String getWebsiteEmbedCalendar(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody) {
|
||||
//Authorization
|
||||
if (request.getHeader("Authorization") == null || !request.getHeader("Authorization").equals(BotSettings.BOT_API_TOKEN.get())) {
|
||||
//Not authorized....
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(401);
|
||||
return JsonUtils.getJsonResponseMessage("Unauthorized. Only official DisCal network can use this!");
|
||||
}
|
||||
|
||||
//Get guild for calendar embed on the website.
|
||||
JSONObject jsonRequest = new JSONObject(requestBody);
|
||||
JSONObject jsonResponse = new JSONObject();
|
||||
|
||||
Optional<Guild> guild = GuildFinder.findGuild(Snowflake.of(jsonRequest.getLong("guild_id")));
|
||||
|
||||
if (guild.isPresent()) {
|
||||
jsonResponse.put("guild", new WebGuild().fromGuild(guild.get()).toJson(false));
|
||||
|
||||
response.setStatus(200);
|
||||
} else {
|
||||
jsonResponse.put("message", "Guild not Found");
|
||||
response.setStatus(404);
|
||||
}
|
||||
response.setContentType("application/json");
|
||||
return jsonResponse.toString();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/bot/action/update", produces = "application/json")
|
||||
public String handleActionUpdate(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody) {
|
||||
//Authorization
|
||||
if (request.getHeader("Authorization") == null || !request.getHeader("Authorization").equals(BotSettings.BOT_API_TOKEN.get())) {
|
||||
//Not authorized....
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(401);
|
||||
return JsonUtils.getJsonResponseMessage("Unauthorized. Only official DisCal network can use this!");
|
||||
}
|
||||
|
||||
//Handle action request....
|
||||
try {
|
||||
JSONObject body = new JSONObject(requestBody);
|
||||
|
||||
//Guild ID must be present for this endpoint...
|
||||
Snowflake guildId = Snowflake.of(body.getLong("guild_id"));
|
||||
|
||||
//Check if guild exists on shard, if not, just ignore this as we can't process it
|
||||
if (GuildUtils.findShard(guildId) == Integer.parseInt(BotSettings.SHARD_INDEX.get())) {
|
||||
if (body.has("bot_nick")) {
|
||||
GuildFinder.getGuild(guildId)
|
||||
.get()
|
||||
.changeSelfNickname(body.getString("bot_nick"))
|
||||
.onErrorResume(e -> Mono.empty())
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
//Send success response to close the connection
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(200);
|
||||
return JsonUtils.getJsonResponseMessage("Success");
|
||||
} else {
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(404);
|
||||
return JsonUtils.getJsonResponseMessage("Guild not found on this shard!");
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(400);
|
||||
return JsonUtils.getJsonResponseMessage("Bad Request");
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger().exception(null, "[COM-API-v1] Failed to handle bot action update", e, true, this.getClass());
|
||||
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(500);
|
||||
return JsonUtils.getJsonResponseMessage("Internal Server Exception");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = "/bot/action/handle", produces = "application/json")
|
||||
public String handleActionHandle(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody) {
|
||||
//Authorization
|
||||
if (request.getHeader("Authorization") == null || !request.getHeader("Authorization").equals(BotSettings.BOT_API_TOKEN.get())) {
|
||||
//Not authorized....
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(401);
|
||||
return JsonUtils.getJsonResponseMessage("Unauthorized. Only official DisCal network can use this!");
|
||||
}
|
||||
|
||||
//Handle action request....
|
||||
try {
|
||||
JSONObject body = new JSONObject(requestBody);
|
||||
|
||||
//First, we handle anything that doesn't require a specific guild to be present
|
||||
DisCalRealm realm = DisCalRealm.valueOf(body.getString("realm"));
|
||||
|
||||
if (realm.equals(DisCalRealm.BOT_LANGS)) {
|
||||
MessageManager.reloadLangs();
|
||||
} else if (realm.equals(DisCalRealm.BOT_INVALIDATE_CACHES)) {
|
||||
DatabaseManager.getManager().clearCache();
|
||||
} else if (body.has("guild_id")) {
|
||||
//Okay, handle everything with guild required...
|
||||
Snowflake guildId = Snowflake.of(body.getLong("guild_id"));
|
||||
if (GuildUtils.findShard(guildId) == Integer.parseInt(BotSettings.SHARD_INDEX.get())) {
|
||||
if (realm.equals(DisCalRealm.GUILD_LEAVE)) {
|
||||
GuildFinder.getGuild(guildId)
|
||||
.get()
|
||||
.leave()
|
||||
.onErrorResume(e -> Mono.empty())
|
||||
.subscribe();
|
||||
} else if (realm.equals(DisCalRealm.GUILD_MAX_CALENDARS)) {
|
||||
GuildSettings settings = DatabaseManager.getManager().getSettings(guildId);
|
||||
settings.setMaxCalendars(body.getInt("max_calendars"));
|
||||
DatabaseManager.getManager().updateSettings(settings);
|
||||
} else if (realm.equals(DisCalRealm.GUILD_IS_DEV)) {
|
||||
GuildSettings settings = DatabaseManager.getManager().getSettings(guildId);
|
||||
settings.setDevGuild(!settings.isDevGuild());
|
||||
DatabaseManager.getManager().updateSettings(settings);
|
||||
} else if (realm.equals(DisCalRealm.GUILD_IS_PATRON)) {
|
||||
GuildSettings settings = DatabaseManager.getManager().getSettings(guildId);
|
||||
settings.setPatronGuild(!settings.isPatronGuild());
|
||||
DatabaseManager.getManager().updateSettings(settings);
|
||||
} else {
|
||||
//Guild not on this shard...
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(400);
|
||||
return JsonUtils.getJsonResponseMessage("Realm not supported on this endpoint!");
|
||||
}
|
||||
} else {
|
||||
//Guild not on this shard...
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(404);
|
||||
return JsonUtils.getJsonResponseMessage("Guild not found on this shard!");
|
||||
}
|
||||
}
|
||||
//Send success response to close the connection
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(200);
|
||||
return JsonUtils.getJsonResponseMessage("Success");
|
||||
} catch (JSONException e) {
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(400);
|
||||
return JsonUtils.getJsonResponseMessage("Bad Request");
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger().exception(null, "[COM-API-v1] Failed to handle bot action update", e, true, this.getClass());
|
||||
|
||||
response.setContentType("application/json");
|
||||
response.setStatus(500);
|
||||
return JsonUtils.getJsonResponseMessage("Internal Server Exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -805,7 +806,7 @@ public class DatabaseManager {
|
||||
* @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(Snowflake guildId) {
|
||||
public List<Announcement> getAnnouncements(Snowflake guildId) {
|
||||
ArrayList<Announcement> announcements = new ArrayList<>();
|
||||
try (final Connection connection = slaveInfo.getSource().getConnection()) {
|
||||
String announcementTableName = String.format("%sannouncements", slaveInfo.getSettings().getPrefix());
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package org.dreamexposure.discal.core.object.web;
|
||||
|
||||
import org.dreamexposure.discal.core.database.DatabaseManager;
|
||||
import org.dreamexposure.discal.core.logger.Logger;
|
||||
import org.dreamexposure.discal.core.object.BotSettings;
|
||||
import org.dreamexposure.discal.core.object.GuildSettings;
|
||||
import org.dreamexposure.discal.core.object.announcement.Announcement;
|
||||
import org.dreamexposure.discal.core.object.calendar.CalendarData;
|
||||
import org.dreamexposure.discal.core.utils.GuildUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import discord4j.core.object.entity.Guild;
|
||||
@@ -18,34 +17,140 @@ import discord4j.core.object.entity.Member;
|
||||
import discord4j.core.object.entity.TextChannel;
|
||||
import discord4j.core.object.util.Image;
|
||||
import discord4j.core.object.util.Snowflake;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
import reactor.function.TupleUtils;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 12/19/17.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal-Discord-Bot
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public class WebGuild {
|
||||
private long id;
|
||||
private String name;
|
||||
private String iconUrl;
|
||||
|
||||
//Functions
|
||||
public static WebGuild fromGuild(Guild g) {
|
||||
long id = g.getId().asLong();
|
||||
String name = g.getName();
|
||||
String iconUrl = g.getIconUrl(Image.Format.PNG).orElse(null);
|
||||
Mono<String> botNick = g.getMemberById(Snowflake.of(BotSettings.ID.get()))
|
||||
.map(Member::getNickname)
|
||||
.flatMap(Mono::justOrEmpty)
|
||||
.defaultIfEmpty("DisCal");
|
||||
|
||||
Mono<GuildSettings> settings = Mono.fromCallable(() ->
|
||||
DatabaseManager.getManager().getSettings(g.getId()))
|
||||
.subscribeOn(Schedulers.elastic())
|
||||
.cache();
|
||||
|
||||
Mono<List<WebRole>> roles = settings.flatMapMany(s ->
|
||||
g.getRoles().map(role -> new WebRole().fromRole(role, s)))
|
||||
.collectList();
|
||||
|
||||
Mono<Boolean> discalChannel = settings.map(GuildSettings::getDiscalChannel)
|
||||
.map(c -> c.equalsIgnoreCase("all"));
|
||||
|
||||
Mono<List<WebChannel>> webChannels = settings.flatMapMany(s ->
|
||||
g.getChannels()
|
||||
.ofType(TextChannel.class)
|
||||
.map(channel -> new WebChannel().fromChannel(channel, s)))
|
||||
.collectList();
|
||||
|
||||
Mono<List<Announcement>> announcements =
|
||||
Mono.fromCallable(() -> DatabaseManager.getManager().getAnnouncements(g.getId()))
|
||||
.subscribeOn(Schedulers.elastic());
|
||||
|
||||
Mono<WebCalendar> calendar = settings.flatMap(s -> Mono.fromCallable(() -> {
|
||||
CalendarData data = DatabaseManager.getManager().getMainCalendar(Snowflake.of(id));
|
||||
return new WebCalendar().fromCalendar(data, s);
|
||||
}).subscribeOn(Schedulers.elastic()));
|
||||
|
||||
return Mono.zip(botNick, settings, roles, discalChannel, webChannels, announcements, calendar)
|
||||
.map(TupleUtils.function((bn, s, r, dc, wc, a, c) -> {
|
||||
WebGuild wg = new WebGuild(id, name, iconUrl, s, bn, false, false, c);
|
||||
|
||||
WebChannel all = new WebChannel();
|
||||
all.setId(0);
|
||||
all.setName("All Channels");
|
||||
all.setDiscalChannel(dc);
|
||||
wg.getChannels().add(all);
|
||||
|
||||
wg.getRoles().addAll(r);
|
||||
wg.getChannels().addAll(wc);
|
||||
wg.getAnnouncements().addAll(a);
|
||||
return wg;
|
||||
})).block();
|
||||
}
|
||||
|
||||
public static WebGuild fromJson(JSONObject data) {
|
||||
long id = Long.parseLong(data.getString("id"));
|
||||
GuildSettings settings = new GuildSettings(
|
||||
Snowflake.of(id)).fromJson(data.getJSONObject("settings"));
|
||||
|
||||
WebGuild webGuild = new WebGuild(
|
||||
id,
|
||||
data.getString("name"),
|
||||
data.optString("icon_url"),
|
||||
settings,
|
||||
data.optString("bot_nick"),
|
||||
data.getBoolean("manage_server"),
|
||||
data.getBoolean("discal_role"),
|
||||
new WebCalendar().fromJson(data.getJSONObject("calendar")));
|
||||
|
||||
JSONArray jRoles = data.getJSONArray("roles");
|
||||
for (int i = 0; i < jRoles.length(); i++) {
|
||||
webGuild.getRoles().add(new WebRole().fromJson(jRoles.getJSONObject(i)));
|
||||
}
|
||||
|
||||
JSONArray jChannels = data.getJSONArray("channels");
|
||||
for (int i = 0; i < jChannels.length(); i++) {
|
||||
webGuild.getChannels().add(new WebChannel().fromJson(jChannels.getJSONObject(i)));
|
||||
}
|
||||
|
||||
JSONArray jAnnouncements = data.getJSONArray("announcements");
|
||||
for (int i = 0; i < jAnnouncements.length(); i++) {
|
||||
webGuild.getAnnouncements().add(new Announcement(Snowflake.of(id)).fromJson(jAnnouncements.getJSONObject(i)));
|
||||
}
|
||||
|
||||
return webGuild;
|
||||
}
|
||||
|
||||
public static WebGuild fromPartialGuild(long id, String name, String icon) {
|
||||
return new WebGuild(id, name, icon, null, null, false, false, null);
|
||||
}
|
||||
|
||||
private final long id;
|
||||
private final String name;
|
||||
private final String iconUrl;
|
||||
|
||||
//Bot settings
|
||||
private GuildSettings settings;
|
||||
private String botNick;
|
||||
private final GuildSettings settings;
|
||||
private final String botNick;
|
||||
|
||||
//User info
|
||||
private boolean manageServer;
|
||||
private boolean discalRole;
|
||||
|
||||
//Lists and stuffs
|
||||
private List<WebRole> roles = new ArrayList<>();
|
||||
private List<WebChannel> channels = new ArrayList<>();
|
||||
private List<Announcement> announcements = new ArrayList<>();
|
||||
private final List<WebRole> roles = new ArrayList<>();
|
||||
private final List<WebChannel> channels = new ArrayList<>();
|
||||
private final List<Announcement> announcements = new ArrayList<>();
|
||||
|
||||
private List<String> availableLangs = new ArrayList<>();
|
||||
private final List<String> availableLangs = new ArrayList<>();
|
||||
|
||||
private WebCalendar calendar;
|
||||
private final WebCalendar calendar;
|
||||
|
||||
private WebGuild(long id, String name, String iconUrl, GuildSettings settings, String botNick,
|
||||
boolean manageServer, boolean discalRole, WebCalendar calendar) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.iconUrl = iconUrl;
|
||||
this.settings = settings;
|
||||
this.botNick = botNick;
|
||||
this.manageServer = manageServer;
|
||||
this.discalRole = discalRole;
|
||||
this.calendar = calendar;
|
||||
}
|
||||
|
||||
//Getters
|
||||
public long getId() {
|
||||
@@ -112,88 +217,13 @@ public class WebGuild {
|
||||
return discalRole;
|
||||
}
|
||||
|
||||
//Setters
|
||||
public void setId(long _id) {
|
||||
id = _id;
|
||||
//Setties
|
||||
public void setManageServer(boolean ms) {
|
||||
manageServer = ms;
|
||||
}
|
||||
|
||||
public void setName(String _name) {
|
||||
name = _name;
|
||||
}
|
||||
|
||||
public void setIcon(String _url) {
|
||||
iconUrl = _url;
|
||||
}
|
||||
|
||||
public void setSettings(GuildSettings _settings) {
|
||||
settings = _settings;
|
||||
}
|
||||
|
||||
public void setBotNick(String _nick) {
|
||||
botNick = _nick;
|
||||
}
|
||||
|
||||
public void setCalendar(WebCalendar _cal) {
|
||||
calendar = _cal;
|
||||
}
|
||||
|
||||
public void setManageServer(boolean _ms) {
|
||||
manageServer = _ms;
|
||||
}
|
||||
|
||||
public void setDiscalRole(boolean _dr) {
|
||||
discalRole = _dr;
|
||||
}
|
||||
|
||||
|
||||
//Functions
|
||||
public WebGuild fromGuild(Guild g) {
|
||||
id = g.getId().asLong();
|
||||
name = g.getName();
|
||||
Logger.getLogger().debug("WebGuild Convert: get icon", true);
|
||||
if (g.getIconUrl(Image.Format.PNG).isPresent())
|
||||
iconUrl = g.getIconUrl(Image.Format.PNG).get();
|
||||
Logger.getLogger().debug("WebGuild Convert: get nick", true);
|
||||
botNick = g.getMemberById(Snowflake.of(BotSettings.ID.get()))
|
||||
.map(Member::getNickname)
|
||||
.block()
|
||||
.orElse("DisCal");
|
||||
|
||||
Logger.getLogger().debug("WebGuild Convert: get settings", true);
|
||||
settings = DatabaseManager.getManager().getSettings(g.getId());
|
||||
|
||||
//Handle web role conversion
|
||||
Logger.getLogger().debug("WebGuild Convert: get roles", true);
|
||||
Collection<WebRole> webRoles = g.getRoles()
|
||||
.map(role -> new WebRole().fromRole(role, settings))
|
||||
.collectList()
|
||||
.block();
|
||||
roles.addAll(webRoles);
|
||||
|
||||
//Handle web channel conversion
|
||||
Logger.getLogger().debug("WebGuild Convert: get channels", true);
|
||||
WebChannel all = new WebChannel();
|
||||
all.setId(0);
|
||||
all.setName("All Channels");
|
||||
all.setDiscalChannel(settings.getDiscalChannel().equalsIgnoreCase("all"));
|
||||
channels.add(all);
|
||||
|
||||
Collection<WebChannel> webChannels = g.getChannels()
|
||||
.ofType(TextChannel.class)
|
||||
.map(channel -> new WebChannel().fromChannel(channel, settings))
|
||||
.collectList()
|
||||
.block();
|
||||
channels.addAll(webChannels);
|
||||
|
||||
//Grab all announcements and calendars from our database
|
||||
Logger.getLogger().debug("WebGuild Convert: get announcements", true);
|
||||
announcements.addAll(DatabaseManager.getManager().getAnnouncements(g.getId()));
|
||||
|
||||
Logger.getLogger().debug("WebGuild Convert: get calendar", true);
|
||||
calendar = new WebCalendar()
|
||||
.fromCalendar(DatabaseManager.getManager().getMainCalendar(Snowflake.of(id)), settings);
|
||||
|
||||
return this;
|
||||
public void setDiscalRole(boolean dr) {
|
||||
discalRole = dr;
|
||||
}
|
||||
|
||||
public JSONObject toJson(boolean secure) {
|
||||
@@ -241,37 +271,4 @@ public class WebGuild {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public WebGuild fromJson(JSONObject data) {
|
||||
id = Long.parseLong(data.getString("id"));
|
||||
name = data.getString("name");
|
||||
if (data.has("icon_url"))
|
||||
iconUrl = data.getString("icon_url");
|
||||
settings = new GuildSettings(Snowflake.of(id)).fromJson(data.getJSONObject("settings"));
|
||||
if (data.has("bot_nick"))
|
||||
botNick = data.getString("bot_nick");
|
||||
else
|
||||
botNick = "";
|
||||
manageServer = data.getBoolean("manage_server");
|
||||
discalRole = data.getBoolean("discal_role");
|
||||
|
||||
JSONArray jRoles = data.getJSONArray("roles");
|
||||
for (int i = 0; i < jRoles.length(); i++) {
|
||||
roles.add(new WebRole().fromJson(jRoles.getJSONObject(i)));
|
||||
}
|
||||
|
||||
JSONArray jChannels = data.getJSONArray("channels");
|
||||
for (int i = 0; i < jChannels.length(); i++) {
|
||||
channels.add(new WebChannel().fromJson(jChannels.getJSONObject(i)));
|
||||
}
|
||||
|
||||
JSONArray jAnnouncements = data.getJSONArray("announcements");
|
||||
for (int i = 0; i < jAnnouncements.length(); i++) {
|
||||
announcements.add(new Announcement(Snowflake.of(id)).fromJson(jAnnouncements.getJSONObject(i)));
|
||||
}
|
||||
|
||||
calendar = new WebCalendar().fromJson(data.getJSONObject("calendar"));
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,8 @@
|
||||
package org.dreamexposure.discal.core.utils;
|
||||
|
||||
import org.dreamexposure.discal.core.object.BotSettings;
|
||||
import org.dreamexposure.discal.core.object.web.WebGuild;
|
||||
import org.json.JSONArray;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import discord4j.core.DiscordClient;
|
||||
import discord4j.core.object.entity.Guild;
|
||||
import discord4j.core.object.entity.Member;
|
||||
import discord4j.core.object.util.Snowflake;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 11/6/17.
|
||||
@@ -27,21 +18,4 @@ public class GuildUtils {
|
||||
//TODO: Determine an accurate way to detect if a guild is still connected to DisCal
|
||||
return true;
|
||||
}
|
||||
|
||||
public static List<WebGuild> getGuilds(JSONArray ids, String userId, DiscordClient client) {
|
||||
List<WebGuild> guilds = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < ids.length(); i++) {
|
||||
Guild g = client.getGuildById(Snowflake.of(ids.getLong(i))).onErrorResume(e -> Mono.empty()).block();
|
||||
if (g != null) {
|
||||
Member m = g.getMemberById(Snowflake.of(userId)).block();
|
||||
|
||||
WebGuild wg = new WebGuild().fromGuild(g);
|
||||
wg.setManageServer(PermissionChecker.hasManageServerRole(m).blockOptional().orElse(false));
|
||||
wg.setDiscalRole(PermissionChecker.hasSufficientRole(m, wg.getSettings()).blockOptional().orElse(false));
|
||||
guilds.add(wg);
|
||||
}
|
||||
}
|
||||
return guilds;
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public class GetWebGuildEndpoint {
|
||||
if (g != null) {
|
||||
//Start slowdown
|
||||
Logger.getLogger().debug("Guild get endpoint convert guild", true);
|
||||
WebGuild wg = new WebGuild().fromGuild(g);
|
||||
WebGuild wg = WebGuild.fromGuild(g);
|
||||
Logger.getLogger().debug("Guild get endpoint get member", true);
|
||||
|
||||
Member m = g.getMemberById(userId).onErrorResume(e -> Mono.empty()).block();
|
||||
|
||||
@@ -119,21 +119,20 @@ public class DiscordLoginHandler {
|
||||
for (int i = 0; i < jGuilds.length(); i++) {
|
||||
JSONObject jGuild = jGuilds.getJSONObject(i);
|
||||
|
||||
WebGuild wg = new WebGuild();
|
||||
wg.setId(jGuild.getLong("id"));
|
||||
wg.setName(jGuild.getString("name"));
|
||||
|
||||
long id = jGuild.getLong("id");
|
||||
String name = jGuild.getString("name");
|
||||
String icon;
|
||||
if (jGuild.has("icon") && !jGuild.isNull("icon")) {
|
||||
wg.setIcon("https://cdn.discordapp.com/icons/"
|
||||
+ wg.getId()
|
||||
icon = "https://cdn.discordapp.com/icons/"
|
||||
+ id
|
||||
+ "/"
|
||||
+ jGuild.getString("icon")
|
||||
+ ".png");
|
||||
+ ".png";
|
||||
} else {
|
||||
wg.setIcon("/assets/img/default/guild-icon.png");
|
||||
icon = "/assets/img/default/guild-icon.png";
|
||||
}
|
||||
|
||||
guilds.add(wg);
|
||||
guilds.add(WebGuild.fromPartialGuild(id, name, icon));
|
||||
}
|
||||
m.put("guilds", guilds);
|
||||
|
||||
|
||||
@@ -290,6 +290,7 @@
|
||||
<div class="container-fluid">
|
||||
<div class="loader"></div>
|
||||
|
||||
<!--Start not connected display-->
|
||||
<div id="not-connected" hidden="hidden">
|
||||
<h2 class="text-discal-blue">It seems this guild doesn't have DisCal in it.</h2>
|
||||
|
||||
@@ -300,7 +301,30 @@
|
||||
Add To Discord!
|
||||
</a>
|
||||
</div>
|
||||
<!--End not connected display-->
|
||||
|
||||
<!--TODO: Handle the display and editing of the following if allowed:
|
||||
- calendar name/summary
|
||||
- calendar description
|
||||
- calendar timezone
|
||||
- calendar delete
|
||||
-->
|
||||
<!--Start calendar settings display-->
|
||||
<div id="calendar-settings" hidden="hidden">
|
||||
|
||||
</div>
|
||||
<!--End calendar settings display-->
|
||||
|
||||
<!--TODO: Handle creating of a new calendar if one does not exist and allowed:
|
||||
- calendar name/summary
|
||||
- calendar description
|
||||
- calendar timezone
|
||||
-->
|
||||
<!--Start calendar create display-->
|
||||
<div id="calendar-create" hidden="hidden">
|
||||
|
||||
</div>
|
||||
<!--End calendar create display-->
|
||||
|
||||
</div>
|
||||
<!--End container fluid-->
|
||||
|
||||
@@ -290,6 +290,7 @@
|
||||
<div class="container-fluid">
|
||||
<div class="loader"></div>
|
||||
|
||||
<!--Start not connected display-->
|
||||
<div id="not-connected" hidden="hidden">
|
||||
<h2 class="text-discal-blue">It seems this guild doesn't have DisCal in it.</h2>
|
||||
|
||||
@@ -300,6 +301,7 @@
|
||||
Add To Discord!
|
||||
</a>
|
||||
</div>
|
||||
<!--End not connected display-->
|
||||
|
||||
<!--Start guild settings display-->
|
||||
<div id="guild-settings" hidden="hidden">
|
||||
|
||||
Reference in New Issue
Block a user