Skip to content

Commit f13bae7

Browse files
committed
Add a staff role check to all form commands
1 parent 6cc7b9e commit f13bae7

14 files changed

+116
-51
lines changed

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/AddFieldFormSubcommand.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Arrays;
44
import java.util.Optional;
55

6+
import net.discordjug.javabot.data.config.BotConfig;
67
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
78
import net.discordjug.javabot.systems.staff_commands.forms.model.FormData;
89
import net.discordjug.javabot.systems.staff_commands.forms.model.FormField;
@@ -15,7 +16,6 @@
1516
import net.dv8tion.jda.api.interactions.commands.OptionType;
1617
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1718
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
18-
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
1919

2020
/**
2121
* The `/form add-field` command. This command allows for modification of
@@ -27,16 +27,18 @@
2727
*
2828
* @see FormData
2929
*/
30-
public class AddFieldFormSubcommand extends Subcommand implements AutoCompletable {
30+
public class AddFieldFormSubcommand extends FormSubcommand implements AutoCompletable {
3131

3232
private final FormsRepository formsRepo;
3333

3434
/**
3535
* The main constructor of this subcommand.
3636
*
3737
* @param formsRepo the forms repository
38+
* @param botConfig bot configuration
3839
*/
39-
public AddFieldFormSubcommand(FormsRepository formsRepo) {
40+
public AddFieldFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
41+
super(botConfig);
4042
this.formsRepo = formsRepo;
4143
setCommandData(new SubcommandData("add-field", "Adds a field to an existing form")
4244
.addOption(OptionType.INTEGER, "form-id", "Form ID to add the field to", true, true)
@@ -52,7 +54,7 @@ public AddFieldFormSubcommand(FormsRepository formsRepo) {
5254

5355
@Override
5456
public void execute(SlashCommandInteractionEvent event) {
55-
57+
if (!checkForStaffRole(event)) return;
5658
event.deferReply(true).queue();
5759
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));
5860
if (formOpt.isEmpty()) {

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/AttachFormSubcommand.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Optional;
77
import java.util.Set;
88

9+
import net.discordjug.javabot.data.config.BotConfig;
910
import net.discordjug.javabot.systems.staff_commands.forms.FormInteractionManager;
1011
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
1112
import net.discordjug.javabot.systems.staff_commands.forms.model.FormData;
@@ -26,7 +27,6 @@
2627
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
2728
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
2829
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
29-
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
3030
import xyz.dynxsty.dih4jda.util.ComponentIdBuilder;
3131

3232
/**
@@ -39,16 +39,18 @@
3939
*
4040
* @see FormData
4141
*/
42-
public class AttachFormSubcommand extends Subcommand implements AutoCompletable {
42+
public class AttachFormSubcommand extends FormSubcommand implements AutoCompletable {
4343

4444
private final FormsRepository formsRepo;
4545

4646
/**
4747
* The main constructor of this subcommand.
4848
*
4949
* @param formsRepo the forms repository
50+
* @param botConfig bot configuration
5051
*/
51-
public AttachFormSubcommand(FormsRepository formsRepo) {
52+
public AttachFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
53+
super(botConfig);
5254
this.formsRepo = formsRepo;
5355
setCommandData(new SubcommandData("attach", "Attach a form to a message").addOptions(
5456
new OptionData(OptionType.INTEGER, "form-id", "ID of the form to attach", true, true),
@@ -62,7 +64,7 @@ public AttachFormSubcommand(FormsRepository formsRepo) {
6264

6365
@Override
6466
public void execute(SlashCommandInteractionEvent event) {
65-
67+
if (!checkForStaffRole(event)) return;
6668
event.deferReply().setEphemeral(true).queue();
6769

6870
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/CloseFormSubcommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
1616
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1717
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
18-
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
1918

2019
/**
2120
* The `/form close` command. This command closes a form. A closed form doesn't
@@ -24,7 +23,7 @@
2423
*
2524
* @see FormData
2625
*/
27-
public class CloseFormSubcommand extends Subcommand implements AutoCompletable {
26+
public class CloseFormSubcommand extends FormSubcommand implements AutoCompletable {
2827

2928
private final FormsRepository formsRepo;
3029
private final FormInteractionManager interactionManager;
@@ -38,6 +37,7 @@ public class CloseFormSubcommand extends Subcommand implements AutoCompletable {
3837
*/
3938
public CloseFormSubcommand(FormsRepository formsRepo, FormInteractionManager interactionManager,
4039
BotConfig botConfig) {
40+
super(botConfig);
4141
this.formsRepo = formsRepo;
4242
this.interactionManager = interactionManager;
4343
setCommandData(new SubcommandData("close", "Close an existing form")
@@ -46,6 +46,7 @@ public CloseFormSubcommand(FormsRepository formsRepo, FormInteractionManager int
4646

4747
@Override
4848
public void execute(SlashCommandInteractionEvent event) {
49+
if (!checkForStaffRole(event)) return;
4950
long id = event.getOption("form-id", OptionMapping::getAsLong);
5051
Optional<FormData> formOpt = formsRepo.getForm(id);
5152
if (formOpt.isEmpty()) {

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/CreateFormSubcommand.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.Optional;
66

7+
import net.discordjug.javabot.data.config.BotConfig;
78
import net.discordjug.javabot.systems.staff_commands.forms.FormInteractionManager;
89
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
910
import net.discordjug.javabot.systems.staff_commands.forms.model.FormData;
@@ -12,26 +13,27 @@
1213
import net.dv8tion.jda.api.interactions.commands.OptionType;
1314
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
1415
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
15-
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
1616

1717
/**
18-
* The `/form create` command.
19-
* This command creates a new, empty form.
20-
* Newly created forms have no fields, and thus can't be attached (See {@link AttachFormSubcommand}) to messages.
21-
* Use {@link AddFieldFormSubcommand} to add new fields to the form.
18+
* The `/form create` command. This command creates a new, empty form. Newly
19+
* created forms have no fields, and thus can't be attached (See
20+
* {@link AttachFormSubcommand}) to messages. Use {@link AddFieldFormSubcommand}
21+
* to add new fields to the form.
2222
*
2323
* @see FormData
2424
*/
25-
public class CreateFormSubcommand extends Subcommand {
25+
public class CreateFormSubcommand extends FormSubcommand {
2626

2727
private final FormsRepository formsRepo;
2828

2929
/**
3030
* The main constructor of this subcommand.
3131
*
3232
* @param formsRepo the forms repository
33+
* @param botConfig bot configuration
3334
*/
34-
public CreateFormSubcommand(FormsRepository formsRepo) {
35+
public CreateFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
36+
super(botConfig);
3537
this.formsRepo = formsRepo;
3638
setCommandData(new SubcommandData("create", "Create a new form").addOptions(
3739
new OptionData(OptionType.STRING, "title", "Form title (shown in modal)", true),
@@ -47,7 +49,7 @@ public CreateFormSubcommand(FormsRepository formsRepo) {
4749

4850
@Override
4951
public void execute(SlashCommandInteractionEvent event) {
50-
52+
if (!checkForStaffRole(event)) return;
5153
event.deferReply().setEphemeral(true).queue();
5254
Optional<Instant> expirationOpt;
5355
try {

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/DeleteFormSubcommand.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Optional;
44

5+
import net.discordjug.javabot.data.config.BotConfig;
56
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
67
import net.discordjug.javabot.systems.staff_commands.forms.model.FormData;
78
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
@@ -13,7 +14,6 @@
1314
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
1415
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1516
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
16-
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
1717

1818
/**
1919
* The `/form delete` command. Deletes an existing form, also detaching it from
@@ -23,24 +23,27 @@
2323
*
2424
* @see FormData
2525
*/
26-
public class DeleteFormSubcommand extends Subcommand implements AutoCompletable {
26+
public class DeleteFormSubcommand extends FormSubcommand implements AutoCompletable {
2727

2828
private final FormsRepository formsRepo;
2929

3030
/**
3131
* The main constructor of this subcommand.
3232
*
3333
* @param formsRepo the forms repository
34+
* @param botConfig
35+
* @param botConfig bot configuration
3436
*/
35-
public DeleteFormSubcommand(FormsRepository formsRepo) {
37+
public DeleteFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
38+
super(botConfig);
3639
this.formsRepo = formsRepo;
3740
setCommandData(new SubcommandData("delete", "Delete an existing form")
3841
.addOptions(new OptionData(OptionType.INTEGER, "form-id", "The ID of a form to delete", true, true)));
3942
}
4043

4144
@Override
4245
public void execute(SlashCommandInteractionEvent event) {
43-
46+
if (!checkForStaffRole(event)) return;
4447
long id = event.getOption("form-id", OptionMapping::getAsLong);
4548
Optional<FormData> formOpt = formsRepo.getForm(id);
4649
if (formOpt.isEmpty()) {

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/DetachFormSubcommand.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Objects;
55
import java.util.Optional;
66

7+
import net.discordjug.javabot.data.config.BotConfig;
78
import net.discordjug.javabot.systems.staff_commands.forms.FormInteractionManager;
89
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
910
import net.discordjug.javabot.systems.staff_commands.forms.model.FormData;
@@ -21,7 +22,6 @@
2122
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
2223
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
2324
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
24-
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
2525
import xyz.dynxsty.dih4jda.util.ComponentIdBuilder;
2626

2727
/**
@@ -32,24 +32,26 @@
3232
*
3333
* @see FormData
3434
*/
35-
public class DetachFormSubcommand extends Subcommand implements AutoCompletable {
35+
public class DetachFormSubcommand extends FormSubcommand implements AutoCompletable {
3636

3737
private final FormsRepository formsRepo;
3838

3939
/**
4040
* The main constructor of this subcommand.
4141
*
4242
* @param formsRepo the forms repository
43+
* @param botConfig bot configuration
4344
*/
44-
public DetachFormSubcommand(FormsRepository formsRepo) {
45+
public DetachFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
46+
super(botConfig);
4547
this.formsRepo = formsRepo;
4648
setCommandData(new SubcommandData("detach", "Detach a form from a message")
4749
.addOptions(new OptionData(OptionType.INTEGER, "form-id", "ID of the form to attach", true, true)));
4850
}
4951

5052
@Override
5153
public void execute(SlashCommandInteractionEvent event) {
52-
54+
if (!checkForStaffRole(event)) return;
5355
event.deferReply().setEphemeral(true).queue();
5456

5557
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/DetailsFormSubcommand.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.time.Instant;
44
import java.util.Optional;
55

6+
import net.discordjug.javabot.data.config.BotConfig;
67
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
78
import net.discordjug.javabot.systems.staff_commands.forms.model.FormData;
89
import net.dv8tion.jda.api.EmbedBuilder;
@@ -18,7 +19,6 @@
1819
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
1920
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
2021
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
21-
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
2222

2323
/**
2424
* The `/form details` command. Displays information about the specified form.
@@ -27,24 +27,26 @@
2727
*
2828
* @see FormData
2929
*/
30-
public class DetailsFormSubcommand extends Subcommand implements AutoCompletable {
30+
public class DetailsFormSubcommand extends FormSubcommand implements AutoCompletable {
3131

3232
private final FormsRepository formsRepo;
3333

3434
/**
3535
* The main constructor of this subcommand.
3636
*
3737
* @param formsRepo the forms repository
38+
* @param botConfig bot configuration
3839
*/
39-
public DetailsFormSubcommand(FormsRepository formsRepo) {
40+
public DetailsFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
41+
super(botConfig);
4042
this.formsRepo = formsRepo;
4143
setCommandData(new SubcommandData("details", "Get details about a form").addOptions(
4244
new OptionData(OptionType.INTEGER, "form-id", "The ID of a form to get details for", true, true)));
4345
}
4446

4547
@Override
4648
public void execute(SlashCommandInteractionEvent event) {
47-
49+
if (!checkForStaffRole(event)) return;
4850
event.deferReply().setEphemeral(false).queue();
4951
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));
5052
if (formOpt.isEmpty()) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.discordjug.javabot.systems.staff_commands.forms.commands;
2+
3+
import net.discordjug.javabot.data.config.BotConfig;
4+
import net.discordjug.javabot.util.Checks;
5+
import net.discordjug.javabot.util.Responses;
6+
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
7+
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
8+
9+
/**
10+
* Base abstract class containing common methods used in form subcommands.
11+
*/
12+
public abstract class FormSubcommand extends Subcommand {
13+
14+
private final BotConfig botConfig;
15+
16+
/**
17+
* The main constructor.
18+
*
19+
* @param botConfig main bot configuration
20+
*/
21+
public FormSubcommand(BotConfig botConfig) {
22+
this.botConfig = botConfig;
23+
}
24+
25+
/**
26+
* Check if the author of this event has the configured staff role. If not,
27+
* reply to the event with a message and return `false`
28+
*
29+
* @param event event to reply to
30+
* @return true if the user has the staff role
31+
*/
32+
protected boolean checkForStaffRole(IReplyCallback event) {
33+
if (!Checks.hasStaffRole(botConfig, event.getMember())) {
34+
Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue();
35+
return false;
36+
}
37+
return true;
38+
}
39+
}

0 commit comments

Comments
 (0)