FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Cleaned up CommandDataLoader so that it now accepts multiple resources. · Java-Discord/JavaBot@a92dbcd · GitHub

Commit a92dbcd

Browse files
committed
Cleaned up CommandDataLoader so that it now accepts multiple resources.
1 parent 3a53956 commit a92dbcd

4 files changed

Lines changed: 60 additions & 19 deletions

File tree

‎src/main/java/com/javadiscord/javabot/commands/SlashCommands.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.javadiscord.javabot.Bot;
66
import com.javadiscord.javabot.Constants;
77
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;
99
import com.javadiscord.javabot.utils.Misc;
1010
import com.mongodb.BasicDBObject;
1111
import com.mongodb.client.MongoCollection;
@@ -101,7 +101,7 @@ private void handleResponseException(ResponseException e, SlashCommandEvent even
101101
* @param guild The guild to update commands for.
102102
*/
103103
public void registerSlashCommands(Guild guild) {
104-
CommandConfig[] commandConfigs = CommandDataConfig.load();
104+
CommandConfig[] commandConfigs = CommandDataLoader.load("commands.yaml");
105105
var commandUpdateAction = this.updateCommands(commandConfigs, guild);
106106
var customCommandNames=this.updateCustomCommands(commandUpdateAction, guild);
107107

‎src/main/java/com/javadiscord/javabot/data/properties/command/CommandConfig.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
55

66
import java.util.Arrays;
7+
import java.util.Objects;
78

89
/**
910
* Simple DTO representing a top-level Discord slash command.
@@ -40,6 +41,18 @@ public CommandData toData() {
4041
return data;
4142
}
4243

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+
4356
@Override
4457
public String toString() {
4558
return "CommandConfig{" +

‎src/main/java/com/javadiscord/javabot/data/properties/command/CommandDataConfig.java‎

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL