Skip to content

Commit 78dce9d

Browse files
committed
(feat): add Guild bean, don't create commands if empty
1 parent 922c241 commit 78dce9d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/main/java/pink/zak/discord/utils/autoconfig/JdaAutoConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import lombok.RequiredArgsConstructor;
44
import net.dv8tion.jda.api.JDA;
55
import net.dv8tion.jda.api.JDABuilder;
6+
import net.dv8tion.jda.api.entities.Guild;
67
import net.dv8tion.jda.api.requests.GatewayIntent;
78
import net.dv8tion.jda.api.utils.cache.CacheFlag;
9+
import org.jetbrains.annotations.NotNull;
810
import org.springframework.boot.autoconfigure.AutoConfiguration;
911
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
12+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1013
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1114
import org.springframework.context.annotation.Bean;
1215
import pink.zak.discord.utils.configuration.JdaConfiguration;
@@ -41,4 +44,11 @@ public JDA jda() throws LoginException, InterruptedException {
4144
.awaitReady();
4245

4346
}
47+
48+
@Bean
49+
@ConditionalOnMissingBean
50+
@ConditionalOnProperty("spring.discord.guild-id")
51+
public Guild guild(@NotNull JDA jda) {
52+
return jda.getGuildById(jdaConfiguration.getGuildId());
53+
}
4454
}

src/main/java/pink/zak/discord/utils/discord/DiscordCommandBackend.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ private List<Command> createNewCommands(@Nullable Guild guild) {
100100
Set<CommandData> createdData = this.commandsByName.values().stream().map(commandData -> commandData.command().createCommandData()).collect(Collectors.toSet());
101101
LOGGER.info("Created data {}", createdData);
102102

103+
if (createdData.isEmpty()) {
104+
LOGGER.warn("No commands were created, this is likely due to no commands being registered");
105+
return List.of();
106+
}
107+
103108
List<Command> createdCommands;
104109
if (guild == null) createdCommands = this.jda.updateCommands().addCommands(createdData).complete();
105110
else createdCommands = guild.updateCommands().addCommands(createdData).complete();

0 commit comments

Comments
 (0)