| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3a53956 commit a92dbcd
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,7 @@ | |||
| 5 | 5 | import com.javadiscord.javabot.Bot; | |
| 6 | 6 | import com.javadiscord.javabot.Constants; | |
| 7 | 7 | import com.javadiscord.javabot.data.properties.command.CommandConfig; | |
| 8 | - import com.javadiscord.javabot.data.properties.command.CommandDataConfig; | ||
| 8 | + import com.javadiscord.javabot.data.properties.command.CommandDataLoader; | ||
| 9 | 9 | import com.javadiscord.javabot.utils.Misc; | |
| 10 | 10 | import com.mongodb.BasicDBObject; | |
| 11 | 11 | import com.mongodb.client.MongoCollection; | |
@@ -101,7 +101,7 @@ private void handleResponseException(ResponseException e, SlashCommandEvent even | |||
| 101 | 101 | * @param guild The guild to update commands for. | |
| 102 | 102 | */ | |
| 103 | 103 | public void registerSlashCommands(Guild guild) { | |
| 104 | - CommandConfig[] commandConfigs = CommandDataConfig.load(); | ||
| 104 | + CommandConfig[] commandConfigs = CommandDataLoader.load("commands.yaml"); | ||
| 105 | 105 | var commandUpdateAction = this.updateCommands(commandConfigs, guild); | |
| 106 | 106 | var customCommandNames=this.updateCustomCommands(commandUpdateAction, guild); | |
| 107 | 107 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | import net.dv8tion.jda.api.interactions.commands.build.CommandData; | |
| 5 | 5 | ||
| 6 | 6 | import java.util.Arrays; | |
| 7 | + import java.util.Objects; | ||
| 7 | 8 | ||
| 8 | 9 | /** | |
| 9 | 10 | * Simple DTO representing a top-level Discord slash command. | |
@@ -40,6 +41,18 @@ public CommandData toData() { | |||
| 40 | 41 | return data; | |
| 41 | 42 | } | |
| 42 | 43 | ||
| 44 | + @Override | ||
| 45 | + public boolean equals(Object o) { | ||
| 46 | + if (this == o) return true; | ||
| 47 | + if (!(o instanceof CommandConfig that)) return false; | ||
| 48 | + return getName().equals(that.getName()); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @Override | ||
| 52 | + public int hashCode() { | ||
| 53 | + return Objects.hash(getName()); | ||
| 54 | + } | ||
| 55 | + | ||
| 43 | 56 | @Override | |
| 44 | 57 | public String toString() { | |
| 45 | 58 | return "CommandConfig{" + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,45 @@ | |||
| 1 | + package com.javadiscord.javabot.data.properties.command; | ||
| 2 | + | ||
| 3 | + import lombok.extern.slf4j.Slf4j; | ||
| 4 | + import org.yaml.snakeyaml.Yaml; | ||
| 5 | + | ||
| 6 | + import java.io.InputStream; | ||
| 7 | + import java.util.HashSet; | ||
| 8 | + import java.util.Set; | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * Simple helper class that loads an array of {@link CommandConfig} instances | ||
| 12 | + * from the YAML files. | ||
| 13 | + */ | ||
| 14 | + @Slf4j | ||
| 15 | + public final class CommandDataLoader { | ||
| 16 | + private CommandDataLoader() {} | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * Loads an array of {@link CommandConfig} from the given set of classpath | ||
| 20 | + * resources. | ||
| 21 | + * @param resources The list of resources to read from. | ||
| 22 | + * @return An array of slash command data objects. | ||
| 23 | + */ | ||
| 24 | + public static CommandConfig[] load(String... resources) { | ||
| 25 | + Yaml yaml = new Yaml(); | ||
| 26 | + Set<CommandConfig> commands = new HashSet<>(); | ||
| 27 | + for (var resource : resources) { | ||
| 28 | + InputStream is = CommandDataLoader.class.getClassLoader().getResourceAsStream(resource); | ||
| 29 | + if (is == null) { | ||
| 30 | + System.err.println("Could not load commands from resource: " + resource); | ||
| 31 | + continue; | ||
| 32 | + } | ||
| 33 | + CommandConfig[] cs = yaml.loadAs(is, CommandConfig[].class); | ||
| 34 | + if (cs != null) { | ||
| 35 | + for (var newCommand : cs) { | ||
| 36 | + if (commands.contains(newCommand)) { | ||
| 37 | + log.warn("Found duplicate command {} in file {}. Already loaded a command with this name; this one will be ignored.", newCommand.getName(), resource); | ||
| 38 | + } | ||
| 39 | + commands.add(newCommand); | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + return commands.toArray(new CommandConfig[0]); | ||
| 44 | + } | ||
| 45 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments