diff --git a/src/main/java/de/kittybot/kittybot/modules/CommandsModule.java b/src/main/java/de/kittybot/kittybot/modules/CommandsModule.java index 6124bd84..e42c9336 100644 --- a/src/main/java/de/kittybot/kittybot/modules/CommandsModule.java +++ b/src/main/java/de/kittybot/kittybot/modules/CommandsModule.java @@ -8,7 +8,6 @@ import io.github.classgraph.ClassGraph; import io.github.classgraph.ClassInfo; import net.dv8tion.jda.api.utils.data.DataArray; -import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.requests.Method; import net.dv8tion.jda.internal.requests.Requester; import net.dv8tion.jda.internal.requests.Route; @@ -20,7 +19,8 @@ import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.*; +import java.util.Map; +import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; @@ -89,6 +89,16 @@ public void deployAllCommands(long guildId){ LOG.info("Registered " + this.commands.size() + " commands..."); } + private Call put(Route.CompiledRoute route, RequestBody body){ + return this.modules.getHttpClient().newCall(newBuilder(route).put(body).build()); + } + + private Request.Builder newBuilder(Route.CompiledRoute route){ + return new Request.Builder() + .url(Requester.DISCORD_API_PREFIX + route.getCompiledRoute()) + .addHeader("Authorization", "Bot " + Config.BOT_TOKEN); + } + public void deleteAllCommands(long guildId){ LOG.info("Deleting commands {}...", guildId == -1 ? "global" : "for guild " + guildId); var rqBody = RequestBody.create(DataArray.empty().toString(), MediaType.parse("application/json")); @@ -107,16 +117,6 @@ public void deleteAllCommands(long guildId){ LOG.info("Deleted all commands..."); } - private Call put(Route.CompiledRoute route, RequestBody body){ - return this.modules.getHttpClient().newCall(newBuilder(route).put(body).build()); - } - - private Request.Builder newBuilder(Route.CompiledRoute route){ - return new Request.Builder() - .url(Requester.DISCORD_API_PREFIX + route.getCompiledRoute()) - .addHeader("Authorization", "Bot " + Config.BOT_TOKEN); - } - public Map getCommands(){ return this.commands; } diff --git a/src/main/java/de/kittybot/kittybot/modules/MusicModule.java b/src/main/java/de/kittybot/kittybot/modules/MusicModule.java index bcacd4c9..65cc337b 100644 --- a/src/main/java/de/kittybot/kittybot/modules/MusicModule.java +++ b/src/main/java/de/kittybot/kittybot/modules/MusicModule.java @@ -136,10 +136,6 @@ else if(event instanceof GuildVoiceJoinEvent){ } } - public int getActivePlayers(){ - return this.musicPlayers.size(); - } - public MusicPlayer get(long guildId){ return this.musicPlayers.get(guildId); } @@ -161,6 +157,10 @@ public void destroy(long guildId, long userId){ } } + public int getActivePlayers(){ + return this.musicPlayers.size(); + } + public MusicPlayer create(CommandContext ctx){ var guildId = ctx.getGuildId(); var link = this.modules.get(LavalinkModule.class).getLink(guildId); diff --git a/src/main/java/de/kittybot/kittybot/modules/StreamAnnouncementModule.java b/src/main/java/de/kittybot/kittybot/modules/StreamAnnouncementModule.java index 77b23883..2e88bcb0 100644 --- a/src/main/java/de/kittybot/kittybot/modules/StreamAnnouncementModule.java +++ b/src/main/java/de/kittybot/kittybot/modules/StreamAnnouncementModule.java @@ -91,6 +91,13 @@ private void checkYouTube(){ } + private void setLiveStatus(StreamUsersRecord record, boolean status){ + record.setIsLive(status); + record.attach(this.modules.get(DatabaseModule.class).getConfiguration()); + record.store(); + record.detach(); + } + private void sendAnnouncementMessage(StreamUsersRecord streamAnnouncement, Stream stream, AnnouncementType announcementType){ var guildId = streamAnnouncement.getGuildId(); var guild = this.modules.getGuildById(guildId); @@ -134,13 +141,6 @@ private void sendAnnouncementMessage(StreamUsersRecord streamAnnouncement, Strea .queue(); } - private void setLiveStatus(StreamUsersRecord record, boolean status){ - record.setIsLive(status); - record.attach(this.modules.get(DatabaseModule.class).getConfiguration()); - record.store(); - record.detach(); - } - public TwitchUser add(String name, long guildId, StreamType type){ var user = this.twitchWrapper.getUserByUsername(name); if(user == null){ diff --git a/src/main/java/de/kittybot/kittybot/modules/WebModule.java b/src/main/java/de/kittybot/kittybot/modules/WebModule.java index dfec8802..c33bb6f5 100644 --- a/src/main/java/de/kittybot/kittybot/modules/WebModule.java +++ b/src/main/java/de/kittybot/kittybot/modules/WebModule.java @@ -4,9 +4,9 @@ import de.kittybot.kittybot.utils.Config; import de.kittybot.kittybot.utils.Utils; import de.kittybot.kittybot.web.bot.invite.GetBotInviteRoute; +import de.kittybot.kittybot.web.commands.GetCommandsRoute; import de.kittybot.kittybot.web.discord.invite.GetDiscordInviteRoute; import de.kittybot.kittybot.web.discord.login.GetDiscordLoginRoute; -import de.kittybot.kittybot.web.commands.GetCommandsRoute; import de.kittybot.kittybot.web.guilds.GetAllGuildsRoute; import de.kittybot.kittybot.web.guilds.guild.channels.GetChannelsRoute; import de.kittybot.kittybot.web.guilds.guild.emotes.GetEmotesRoute; @@ -20,6 +20,7 @@ import de.kittybot.kittybot.web.info.GetInfoRoute; import de.kittybot.kittybot.web.login.DeleteLoginRoute; import de.kittybot.kittybot.web.login.PostLoginRoute; +import de.kittybot.kittybot.web.shards.GetShardsRoute; import de.kittybot.kittybot.web.user.GetUserInfoRoute; import io.javalin.Javalin; import io.javalin.http.*; @@ -66,6 +67,9 @@ public void onEnable(){ path("/info", () -> get(new GetInfoRoute(this.modules)) ); + path("/shards", () -> + get(new GetShardsRoute(this.modules)) + ); path("/discord_login", () -> get(new GetDiscordLoginRoute(this.modules)) ); diff --git a/src/main/java/de/kittybot/kittybot/objects/enums/Environment.java b/src/main/java/de/kittybot/kittybot/objects/enums/Environment.java index 03bf6f64..9e831194 100644 --- a/src/main/java/de/kittybot/kittybot/objects/enums/Environment.java +++ b/src/main/java/de/kittybot/kittybot/objects/enums/Environment.java @@ -12,7 +12,8 @@ public static Environment getCurrentEnv(){ return valueOf(env); } } - catch(IllegalArgumentException ignored){} + catch(IllegalArgumentException ignored){ + } return DEVELOPMENT; } } diff --git a/src/main/java/de/kittybot/kittybot/slashcommands/application/Category.java b/src/main/java/de/kittybot/kittybot/slashcommands/application/Category.java index 90b746a5..ef30acdd 100644 --- a/src/main/java/de/kittybot/kittybot/slashcommands/application/Category.java +++ b/src/main/java/de/kittybot/kittybot/slashcommands/application/Category.java @@ -29,22 +29,22 @@ public String getEmote(){ return this.emote.get(); } - public String getEmoteUrl(){ - return "https://cdn.discordapp.com/emojis/" + this.emote.getId() + ".png"; - } - public String getName(){ return this.name; } - public String getUrl(){ - return "http://" + Config.ORIGIN_URL + "/commands#" + this.name.toLowerCase(); - } - public DataObject toJSON(){ return DataObject.empty() .put("name", this.name) .put("url", getUrl()) .put("emote", getEmoteUrl()); } + + public String getUrl(){ + return "http://" + Config.ORIGIN_URL + "/commands#" + this.name.toLowerCase(); + } + + public String getEmoteUrl(){ + return "https://cdn.discordapp.com/emojis/" + this.emote.getId() + ".png"; + } } diff --git a/src/main/java/de/kittybot/kittybot/web/info/GetInfoRoute.java b/src/main/java/de/kittybot/kittybot/web/info/GetInfoRoute.java index 2a46e88b..a3fe48c7 100644 --- a/src/main/java/de/kittybot/kittybot/web/info/GetInfoRoute.java +++ b/src/main/java/de/kittybot/kittybot/web/info/GetInfoRoute.java @@ -1,5 +1,6 @@ package de.kittybot.kittybot.web.info; +import de.kittybot.kittybot.modules.CommandsModule; import de.kittybot.kittybot.modules.MusicModule; import de.kittybot.kittybot.modules.WebModule; import de.kittybot.kittybot.objects.module.Modules; @@ -7,12 +8,9 @@ import io.javalin.http.Handler; import net.dv8tion.jda.api.JDAInfo; import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.utils.data.DataArray; import net.dv8tion.jda.api.utils.data.DataObject; import org.jetbrains.annotations.NotNull; -import java.util.stream.Collectors; - public class GetInfoRoute implements Handler{ private final Modules modules; @@ -24,19 +22,13 @@ public GetInfoRoute(Modules modules){ @Override public void handle(@NotNull Context ctx){ var shardManager = modules.getShardManager(); - WebModule.ok(ctx, - DataObject.empty() - .put("shards", DataArray.fromCollection(shardManager.getShardCache().stream().map(shard -> DataObject.empty() - .put("id", shard.getShardInfo().getShardId()) - .put("guilds", shard.getGuildCache().size()) - .put("status", shard.getStatus().name()) - .put("ping", shard.getGatewayPing()) - ).collect(Collectors.toList())) - ) - .put("guilds", shardManager.getShardCache().size()) - .put("users", shardManager.getGuildCache().applyStream(guildStream -> guildStream.mapToInt(Guild::getMemberCount).sum())) - .put("jda_version", JDAInfo.VERSION) - .put("players", modules.get(MusicModule.class).getActivePlayers()) + WebModule.ok(ctx, DataObject.empty() + .put("shards", shardManager.getShardCache().size()) + .put("guilds", shardManager.getGuildCache().size()) + .put("users", shardManager.getGuildCache().applyStream(guildStream -> guildStream.mapToInt(Guild::getMemberCount).sum())) + .put("jda_version", JDAInfo.VERSION) + .put("players", modules.get(MusicModule.class).getActivePlayers()) + .put("commands", modules.get(CommandsModule.class).getCommands().size()) ); } diff --git a/src/main/java/de/kittybot/kittybot/web/shards/GetShardsRoute.java b/src/main/java/de/kittybot/kittybot/web/shards/GetShardsRoute.java new file mode 100644 index 00000000..068cb28d --- /dev/null +++ b/src/main/java/de/kittybot/kittybot/web/shards/GetShardsRoute.java @@ -0,0 +1,32 @@ +package de.kittybot.kittybot.web.shards; + +import de.kittybot.kittybot.modules.WebModule; +import de.kittybot.kittybot.objects.module.Modules; +import io.javalin.http.Context; +import io.javalin.http.Handler; +import net.dv8tion.jda.api.utils.data.DataArray; +import net.dv8tion.jda.api.utils.data.DataObject; +import org.jetbrains.annotations.NotNull; + +import java.util.stream.Collectors; + +public class GetShardsRoute implements Handler{ + + private final Modules modules; + + public GetShardsRoute(Modules modules){ + this.modules = modules; + } + + @Override + public void handle(@NotNull Context ctx){ + WebModule.ok(ctx, DataObject.empty().put("shards", DataArray.fromCollection(modules.getShardManager().getShardCache().stream().map(shard -> DataObject.empty() + .put("id", shard.getShardInfo().getShardId()) + .put("guilds", shard.getGuildCache().size()) + .put("status", shard.getStatus().name()) + .put("ping", shard.getGatewayPing()) + ).collect(Collectors.toList()))) + ); + } + +}