Skip to content

Commit

Permalink
Merge pull request #234 from KittyBot-Org/development
Browse files Browse the repository at this point in the history
some more info route updates
  • Loading branch information
topi314 authored Feb 1, 2021
2 parents 418e840 + e1aea76 commit 6884f56
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 49 deletions.
24 changes: 12 additions & 12 deletions src/main/java/de/kittybot/kittybot/modules/CommandsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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"));
Expand All @@ -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<String, Command> getCommands(){
return this.commands;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/kittybot/kittybot/modules/MusicModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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){
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/de/kittybot/kittybot/modules/WebModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public static Environment getCurrentEnv(){
return valueOf(env);
}
}
catch(IllegalArgumentException ignored){}
catch(IllegalArgumentException ignored){
}
return DEVELOPMENT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
24 changes: 8 additions & 16 deletions src/main/java/de/kittybot/kittybot/web/info/GetInfoRoute.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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;
import io.javalin.http.Context;
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;
Expand All @@ -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())
);
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/de/kittybot/kittybot/web/shards/GetShardsRoute.java
Original file line number Diff line number Diff line change
@@ -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())))
);
}

}

0 comments on commit 6884f56

Please sign in to comment.