Skip to content

Commit d76e51c

Browse files
Create more effective system for setting channel types for commands
1 parent 4a72819 commit d76e51c

3 files changed

Lines changed: 25 additions & 34 deletions

File tree

src/main/java/technobot/commands/levels/LevelingCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mongodb.client.model.Filters;
44
import com.mongodb.client.model.Updates;
55
import net.dv8tion.jda.api.Permission;
6+
import net.dv8tion.jda.api.entities.ChannelType;
67
import net.dv8tion.jda.api.entities.Role;
78
import net.dv8tion.jda.api.entities.User;
89
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@@ -32,7 +33,8 @@ public LevelingCommand(TechnoBot bot) {
3233
this.category = Category.LEVELS;
3334
this.permission = Permission.MANAGE_SERVER;
3435
this.subCommands.add(new SubcommandData("channel", "Sets a channel to send level-up messages to.")
35-
.addOption(OptionType.CHANNEL, "channel", "The channel to send level-up messages to"));
36+
.addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel to send level-up messages to")
37+
.setChannelTypes(ChannelType.TEXT, ChannelType.NEWS)));
3638
this.subCommands.add(new SubcommandData("message", "Sets a custom level-up message.")
3739
.addOption(OptionType.STRING, "text", "Custom level-up message. Use {user} for their name and {lvl} for the level"));
3840
this.subCommands.add(new SubcommandData("dm", "Makes the bot dm the member their level-up message."));
@@ -63,7 +65,7 @@ public void execute(SlashCommandInteractionEvent event) {
6365
case "channel" -> {
6466
OptionMapping channelOption = event.getOption("channel");
6567
if (channelOption != null) {
66-
long channel = channelOption.getAsTextChannel().getIdLong();
68+
long channel = channelOption.getAsGuildChannel().getIdLong();
6769
config.setLevelingChannel(channel);
6870
update = Updates.set("leveling_channel", channel);
6971
text = EmbedUtils.BLUE_TICK + " Leveling messages will now only display in <#" + channel + ">.";

src/main/java/technobot/commands/starboard/StarboardCommand.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package technobot.commands.starboard;
22

33
import net.dv8tion.jda.api.Permission;
4+
import net.dv8tion.jda.api.entities.ChannelType;
45
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
56
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
67
import net.dv8tion.jda.api.interactions.commands.OptionType;
@@ -27,13 +28,16 @@ public StarboardCommand(TechnoBot bot) {
2728
this.category = Category.STARBOARD;
2829
this.permission = Permission.MANAGE_SERVER;
2930
this.subCommands.add(new SubcommandData("create", "Sets a channel to become the starboard.")
30-
.addOption(OptionType.CHANNEL, "channel", "The channel to set as the starboard", true));
31+
.addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel to set as the starboard", true)
32+
.setChannelTypes(ChannelType.TEXT, ChannelType.NEWS)));
3133
this.subCommands.add(new SubcommandData("limit", "Sets the star requirement for messages to post to the starboard.")
3234
.addOptions(new OptionData(OptionType.INTEGER, "stars", "The star limit").setMinValue(1).setMaxValue(25)));
3335
this.subCommands.add(new SubcommandData("blacklist", "Blocks a channel from having their messages starred.")
34-
.addOption(OptionType.CHANNEL, "channel", "The channel to blacklist"));
36+
.addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel to blacklist")
37+
.setChannelTypes(ChannelType.TEXT, ChannelType.NEWS)));
3538
this.subCommands.add(new SubcommandData("unblacklist", "Removes a channel from the starboard blacklist.")
36-
.addOption(OptionType.CHANNEL, "channel", "The channel to unblacklist"));
39+
.addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel to unblacklist")
40+
.setChannelTypes(ChannelType.TEXT, ChannelType.NEWS)));
3741
this.subCommands.add(new SubcommandData("lock", "Toggles a temporary lock on the entire starboard."));
3842
this.subCommands.add(new SubcommandData("jump", "Toggles a link to the source message for each starboard entry."));
3943
this.subCommands.add(new SubcommandData("nsfw", "Toggles the ability to star messages in NSFW channels."));
@@ -51,15 +55,9 @@ public void execute(SlashCommandInteractionEvent event) {
5155
switch(event.getSubcommandName()) {
5256
case "create" -> {
5357
// Set starboard channel
54-
try {
55-
long channel = channelOption.getAsTextChannel().getIdLong();
56-
starboardHandler.setChannel(channel);
57-
text = EmbedUtils.BLUE_TICK + " Set the starboard channel to <#" + channel + ">";
58-
} catch (NullPointerException e) {
59-
text = "You can only set a text channel as the starboard!";
60-
event.getHook().sendMessageEmbeds(EmbedUtils.createError(text)).queue();
61-
return;
62-
}
58+
long channel = channelOption.getAsGuildChannel().getIdLong();
59+
starboardHandler.setChannel(channel);
60+
text = EmbedUtils.BLUE_TICK + " Set the starboard channel to <#" + channel + ">";
6361
}
6462
case "limit" -> {
6563
OptionMapping limitOption = event.getOption("stars");
@@ -74,31 +72,19 @@ public void execute(SlashCommandInteractionEvent event) {
7472
}
7573
case "blacklist" -> {
7674
if (channelOption != null) {
77-
try {
78-
long channel = channelOption.getAsTextChannel().getIdLong();
79-
starboardHandler.blacklistChannel(channel);
80-
text = EmbedUtils.BLUE_TICK + " Starboard will now ignore reactions from <#" + channel + ">";
81-
} catch (NullPointerException e) {
82-
text = "You can only blacklist a text channel!";
83-
event.getHook().sendMessageEmbeds(EmbedUtils.createError(text)).queue();
84-
return;
85-
}
75+
long channel = channelOption.getAsGuildChannel().getIdLong();
76+
starboardHandler.blacklistChannel(channel);
77+
text = EmbedUtils.BLUE_TICK + " Starboard will now ignore reactions from <#" + channel + ">";
8678
} else {
8779
starboardHandler.clearBlacklist();
8880
text = EmbedUtils.BLUE_TICK + " Reset the starboard blacklist!";
8981
}
9082
}
9183
case "unblacklist" -> {
9284
if (channelOption != null) {
93-
try {
94-
long channel = channelOption.getAsTextChannel().getIdLong();
95-
starboardHandler.unBlacklistChannel(channel);
96-
text = EmbedUtils.BLUE_X + " Removed <#" + channel + "> from the Starboard blacklist!";
97-
} catch (NullPointerException e) {
98-
text = "You can only unblacklist a text channel!";
99-
event.getHook().sendMessageEmbeds(EmbedUtils.createError(text)).queue();
100-
return;
101-
}
85+
long channel = channelOption.getAsGuildChannel().getIdLong();
86+
starboardHandler.unBlacklistChannel(channel);
87+
text = EmbedUtils.BLUE_X + " Removed <#" + channel + "> from the Starboard blacklist!";
10288
} else {
10389
starboardHandler.clearBlacklist();
10490
text = EmbedUtils.BLUE_TICK + " Reset the starboard blacklist!";

src/main/java/technobot/commands/suggestions/SuggestionsCommand.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package technobot.commands.suggestions;
22

33
import net.dv8tion.jda.api.Permission;
4+
import net.dv8tion.jda.api.entities.ChannelType;
45
import net.dv8tion.jda.api.entities.Emoji;
56
import net.dv8tion.jda.api.entities.Guild;
67
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
78
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
89
import net.dv8tion.jda.api.interactions.commands.OptionType;
10+
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
911
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1012
import net.dv8tion.jda.api.interactions.components.buttons.Button;
1113
import technobot.TechnoBot;
@@ -31,7 +33,8 @@ public SuggestionsCommand(TechnoBot bot) {
3133
this.category = Category.SUGGESTIONS;
3234
this.permission = Permission.MANAGE_SERVER;
3335
this.subCommands.add(new SubcommandData("create", "Sets a channel to become the suggestion board.")
34-
.addOption(OptionType.CHANNEL, "channel", "The channel to set as the suggestion board"));
36+
.addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel to set as the suggestion board")
37+
.setChannelTypes(ChannelType.TEXT, ChannelType.NEWS)));
3538
this.subCommands.add(new SubcommandData("dm", "Toggle private messages on suggestion response."));
3639
this.subCommands.add(new SubcommandData("anonymous", "Toggle anonymous mode for suggestions."));
3740
this.subCommands.add(new SubcommandData("config", "Display the current suggestions config."));
@@ -67,7 +70,7 @@ public void execute(SlashCommandInteractionEvent event) {
6770
} else {
6871
// Set suggestion board to mentioned channel
6972
try {
70-
long channel = channelOption.getAsTextChannel().getIdLong();
73+
long channel = channelOption.getAsGuildChannel().getIdLong();
7174
String channelMention = "<#" + channel + ">";
7275
suggestionHandler.setChannel(channel);
7376
text = EmbedUtils.BLUE_TICK + " Set the suggestion channel to " + channelMention;

0 commit comments

Comments
 (0)