Skip to content

Commit 9852dc6

Browse files
authored
Merge pull request #19 from ice-games/feat/guildcommnads
Guild Commands
2 parents 76f916b + e90fd95 commit 9852dc6

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/main/java/com/seailz/jdaframework/DiscordBot.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import lombok.Setter;
1414
import net.dv8tion.jda.api.JDA;
1515
import net.dv8tion.jda.api.JDABuilder;
16+
import net.dv8tion.jda.api.entities.Guild;
1617
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
1718
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
1819
import net.dv8tion.jda.api.hooks.EventListener;
@@ -121,4 +122,17 @@ public DiscordBot registerCommands(Command... commands) {
121122
}
122123
return this;
123124
}
125+
126+
/**
127+
* Registers commands. These commands will <b>ONLY</b> show up in the guild you register them in, and not in DMs or any other server.
128+
* @param guild The guild to register them for
129+
* @param commands The commands to register
130+
* @return The bot instance
131+
*/
132+
public DiscordBot registerGuildCommand(Guild guild, Command... commands) {
133+
for (Command command : commands) {
134+
registry.registerGuildCommand(command, guild);
135+
}
136+
return this;
137+
}
124138
}

src/main/java/com/seailz/jdaframework/command/registry/CommandRegistry.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.Getter;
55
import net.dv8tion.jda.api.JDA;
66
import net.dv8tion.jda.api.JDABuilder;
7+
import net.dv8tion.jda.api.entities.Guild;
78
import net.dv8tion.jda.api.requests.restaction.CommandCreateAction;
89

910
import java.util.HashMap;
@@ -34,4 +35,18 @@ public void registerCommand(JDA jda, Command command) {
3435
commands.put(command.getName(), command);
3536
}
3637

38+
public void registerGuildCommand(Command command, Guild guild) {
39+
if (commands.containsValue(command)) return;
40+
if (command.getOptions().isEmpty()) {
41+
guild.upsertCommand(command.getName(), command.getDescription()).queue();
42+
} else {
43+
CommandCreateAction action = guild.upsertCommand(command.getName(), command.getDescription());
44+
command.getOptions().forEach(option -> {
45+
action.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired());
46+
});
47+
action.queue();
48+
}
49+
commands.put(command.getName(), command);
50+
}
51+
3752
}

0 commit comments

Comments
 (0)