Skip to content

Commit

Permalink
Merge pull request #254 from KittyBot-Org/development
Browse files Browse the repository at this point in the history
some command description fixes & internal changes
  • Loading branch information
topi314 authored Feb 17, 2021
2 parents 807dc9a + 4aa8494 commit 5d5efa2
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 11 deletions.
2 changes: 2 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"discord_boats_token": "",
"botlist_space": "",
"bots_for_discord_token": "",
"discordbotlist_token": "",
"discord_services_token": "",

"db_host": "",
"db_port": "",
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/de/kittybot/kittybot/commands/dev/DevCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.kittybot.kittybot.commands.dev;

import de.kittybot.kittybot.modules.CommandsModule;
import de.kittybot.kittybot.objects.enums.Environment;
import de.kittybot.kittybot.slashcommands.application.Category;
import de.kittybot.kittybot.slashcommands.application.Command;
import de.kittybot.kittybot.slashcommands.application.CommandOptionChoice;
Expand Down Expand Up @@ -84,12 +85,12 @@ private static class RemoveCommand extends SubCommand{
public RemoveCommand(){
super("remove", "Removes slash commands from a specified environment");
addOptions(
new CommandOptionInteger("environment", "In which environment should the commands get omitted").required()
new CommandOptionInteger("environment", "In which environment should the commands get removed").required()
.addChoices(
new CommandOptionChoice<>("global", 0),
new CommandOptionChoice<>("guild", 1)
),
new CommandOptionLong("guild", "In which guild commands should get omitted")
new CommandOptionLong("guild", "In which guild commands should get removed")
);
devOnly();
}
Expand All @@ -98,11 +99,15 @@ public RemoveCommand(){
public void run(Options options, CommandContext ctx){
var environment = options.getInt("environment");
if(environment == 0){
if(Environment.getCurrentEnv() == Environment.PRODUCTION){
ctx.reply(new InteractionResponse.Builder().ephemeral().setContent("Removing commands globally in production is not allowed sorry :3").build());
return;
}
ctx.reply(new InteractionResponse.Builder().ephemeral().setContent("processing...").build());
ctx.getModules().schedule(() -> {
var commandsModule = ctx.get(CommandsModule.class);
commandsModule.deleteAllCommands(-1L);
ctx.followup(new FollowupMessage.Builder().setEmbeds(new EmbedBuilder().setColor(Colors.KITTYBOT_BLUE).setDescription("Omitted slash commands globally").build()).build());
ctx.followup(new FollowupMessage.Builder().setEmbeds(new EmbedBuilder().setColor(Colors.KITTYBOT_BLUE).setDescription("Removed slash commands globally").build()).build());
}, 0, TimeUnit.SECONDS);
return;
}
Expand All @@ -115,7 +120,7 @@ public void run(Options options, CommandContext ctx){
ctx.getModules().schedule(() -> {
var commandsModule = ctx.get(CommandsModule.class);
commandsModule.deleteAllCommands(guildId);
ctx.followup(new FollowupMessage.Builder().setEmbeds(new EmbedBuilder().setColor(Colors.KITTYBOT_BLUE).setDescription("Omitted slash commands for guild `" + guildId + "`").build()).build());
ctx.followup(new FollowupMessage.Builder().setEmbeds(new EmbedBuilder().setColor(Colors.KITTYBOT_BLUE).setDescription("Removed slash commands for guild `" + guildId + "`").build()).build());
}, 0, TimeUnit.SECONDS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void run(Options options, CommandContext ctx){
.setAuthor("Help", Config.ORIGIN_URL, ctx.getSelfUser().getEffectiveAvatarUrl())
.setDescription(
"Hello " + ctx.getMember().getAsMention() + "\n" +
"KittyBot uses the new " + Emoji.SLASH.get() + "Slash Commands by Discord!" +
"KittyBot uses the new " + Emoji.SLASH.get() + " Slash Commands by Discord!\n" +
"To see all available commands just type `/` or use `/commands`"
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class StealEmoteCommand extends Command{
private static final int MAX_EMOTE_SIZE = 256000;

public StealEmoteCommand(){
super("stealemote", "Gets the avatar of a user", Category.UTILITIES);
super("stealemote", "Steal an emote to your server", Category.UTILITIES);
addOptions(
new EmoteCommand(),
new EmoteIdCommand(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ private void updateStats(){
if(!Config.BOTS_FOR_DISCORD_TOKEN.isBlank()){
requestModule.updateStats(API.BOTS_FOR_DISCORD, guildCount, Config.BOTS_FOR_DISCORD_TOKEN);
}
if(!Config.DISCORDBOTLIST_TOKEN.isBlank()){
requestModule.updateStats(API.DISCORDBOTLIST_COM, guildCount, Config.DISCORDBOTLIST_TOKEN);
}
if(!Config.DISCORD_SERVICES_TOKEN.isBlank()){
requestModule.updateStats(API.DISCORD_SERVICES, guildCount, Config.DISCORD_SERVICES_TOKEN);
}
}

private int getTotalGuilds(){
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/de/kittybot/kittybot/modules/CommandsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -31,8 +32,15 @@ public class CommandsModule extends Module{

private static final Logger LOG = LoggerFactory.getLogger(CommandsModule.class);
private static final String COMMANDS_PACKAGE = "de.kittybot.kittybot.commands";
private static final Set<Class<? extends Module>> DEPENDENCIES = Set.of(RequestModule.class);

private Map<String, Command> commands;

@Override
public Set<Class<? extends Module>> getDependencies(){
return DEPENDENCIES;
}

@Override
public void onEnable(){
scanCommands();
Expand Down Expand Up @@ -86,6 +94,9 @@ public void deployAllCommands(long guildId){
catch(IOException e){
LOG.error("Error while processing registerCommands", e);
}
if(guildId == -1L && !Config.DISCORD_SERVICES_TOKEN.isBlank()){
this.modules.get(RequestModule.class).uploadCommands(this.commands);
}
LOG.info("Registered " + this.commands.size() + " commands...");
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/de/kittybot/kittybot/modules/JoinModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class JoinModule extends Module{
private static final Logger LOG = LoggerFactory.getLogger(MessageUtils.class);
private static final String INVITE_CODE_PREFIX = "https://discord.gg/";
private static final Set<Class<? extends Module>> DEPENDENCIES = Set.of(InviteModule.class);
private static final List<String> oldCommands = List.of(".play", ".guildbanner", ".kiss", ".translate", ".blush", ".lick", ".hastebin", ".commands", ".dashboard", ".info", ".settings", ".pat", ".test", ".history", ".poke", ".pause", ".unassign", ".volume", ".feed", ".guildicon", ".eval", ".stop", ".restrictemote", ".tickle", ".fluff", ".downloademotes", ".senko", ".editsnipe", ".steal", ".ping", ".roles", ".privacy", ".skip", ".seek", ".bite", ".cuddle", ".forward", ".neko", ".avatar", ".snipe", ".uptime", ".kitsune", ".help", ".rewind", ".hug", ".shuffle", ".slap", ".queue", ".assign");

private List<String> randomJoinMessages;
private List<String> randomLeaveMessages;
Expand All @@ -46,19 +45,19 @@ protected void onEnable(){

@Override
public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event){
var msg = event.getMessage().getContentRaw();
var msg = event.getMessage().getContentRaw().toLowerCase();
if(event.getAuthor().isBot()){
return;
}
if(!event.getChannel().canTalk()){
return;
}
if(oldCommands.stream().anyMatch(msg::startsWith) || msg.contains("<@" + Config.BOT_ID + ">") || msg.contains("<@!" + Config.BOT_ID + ">")){
if((msg.contains("<@" + Config.BOT_ID + ">") || msg.contains("<@!" + Config.BOT_ID + ">")) && msg.contains("help")){
event.getChannel().sendMessage(new EmbedBuilder()
.setColor(Colors.KITTYBOT_BLUE)
.setAuthor("KittyBot Slash Commands Update", event.getJDA().getSelfUser().getEffectiveAvatarUrl(), Config.ORIGIN_URL)
.setDescription("KittyBot now uses the new slash commands.\n" +
"To use them invite me again **(you don't need to kick me)** over this specific " + MessageUtils.maskLink("link", Config.BOT_INVITE_URL) + " and type `/` in the chat box.\n"
"To use them invite me again **(you don't need to kick me)** over this specific " + MessageUtils.maskLink("link", Config.BOT_INVITE_URL) + " and type `/` in the chat box."
)
.setFooter(event.getMember().getEffectiveName(), event.getAuthor().getEffectiveAvatarUrl())
.setTimestamp(Instant.now())
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/de/kittybot/kittybot/modules/RequestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.kittybot.kittybot.objects.enums.API;
import de.kittybot.kittybot.objects.enums.Language;
import de.kittybot.kittybot.objects.module.Module;
import de.kittybot.kittybot.slashcommands.application.Command;
import de.kittybot.kittybot.utils.Config;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
Expand All @@ -14,8 +15,10 @@
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class RequestModule extends Module{

Expand Down Expand Up @@ -160,4 +163,21 @@ public void retrieveUrlContent(String url, BiConsumer<Call, Response> success, B
executeAsync(this.requestBuilder.build(), success, error);
}

public void uploadCommands(Map<String, Command> commands){
var json = DataArray.fromCollection(commands.values().stream().map(Command::toDiscordServicesJSON).collect(Collectors.toList()));

this.requestBuilder.url("https://api.discordservices.net/bot/" + Config.BOT_ID + "/commands");
this.requestBuilder.post(RequestBody.create(json.toJson(), MediaType.parse("application/json")));
this.requestBuilder.header("Authorization", Config.DISCORD_SERVICES_TOKEN);
executeAsync(this.requestBuilder.build(), (call, response) -> {}, (call, response) -> {
var body = response.body();
try{
LOG.error("Uploading commands to discordservices failed. Body: {}", body == null ? "null" : body.string());
}
catch(IOException e){
LOG.error("Uploading commands to discordservices failed", e);
}
});
}

}
4 changes: 3 additions & 1 deletion src/main/java/de/kittybot/kittybot/objects/enums/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public enum API{
DISCORD_BOATS("discord.boats", "https://discord.boats/api/bot/%s", "server_count"),
BOTS_FOR_DISCORD("botsfordiscord.com", "https://botsfordiscord.com/api/bot/%s", "server_count"),
BOTLIST_SPACE("botlist.space", "https://botsfordiscord.com/api/bot/%s", "server_count"),
DISCORDBOTLIST_COM("discordbotlist.com", "https://discordbotlist.com/api/v1/bots/%s/stats", "guilds"),
DISCORD_SERVICES("discordservices.net", "https://api.discordservices.net/bot/%s/stats", "servers"),

// other
PURR_BOT("purr bot", "https://purrbot.site/api/img/%s/%s/%s"),
Expand Down Expand Up @@ -44,4 +46,4 @@ public String getUrl(){
public String getStatsParameter(){
return this.statsParameter;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ private DataArray getPermissionArray(){
);
}

public DataObject toDiscordServicesJSON(){
return DataObject.empty()
.put("command", "/" + this.name)
.put("desc", this.description)
.put("category", this.category.getName());
}

}
4 changes: 4 additions & 0 deletions src/main/java/de/kittybot/kittybot/utils/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class Config{
public static String DISCORD_BOTS_TOKEN;
public static String BOTLIST_SPACE_TOKEN;
public static String BOTS_FOR_DISCORD_TOKEN;
public static String DISCORDBOTLIST_TOKEN;
public static String DISCORD_SERVICES_TOKEN;

public static String DB_HOST;
public static String DB_PORT;
Expand Down Expand Up @@ -112,6 +114,8 @@ public static void init(String path) throws IOException, MissingConfigValuesExce
DISCORD_BOATS_TOKEN = json.getString("discord_boats_token", "");
BOTLIST_SPACE_TOKEN = json.getString("botlist_space_token", "");
BOTS_FOR_DISCORD_TOKEN = json.getString("bots_for_discord_token", "");
DISCORDBOTLIST_TOKEN = json.getString("discordbotlist_token", "");
DISCORD_SERVICES_TOKEN = json.getString("discord_services_token", "");

DB_HOST = json.getString("db_host", "");
DB_PORT = json.getString("db_port", "");
Expand Down

0 comments on commit 5d5efa2

Please sign in to comment.