Skip to content

Commit 2dd9e06

Browse files
committed
make sure all RestActions are queued and automatically locking QOTW view threads
1 parent 7426b94 commit 2dd9e06

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

src/main/java/net/javadiscord/javabot/systems/moderation/PurgeCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.javadiscord.javabot.util.TimeUtils;
1515
import org.jetbrains.annotations.NotNull;
1616

17+
import javax.annotation.CheckReturnValue;
1718
import javax.annotation.Nullable;
1819
import java.io.IOException;
1920
import java.io.PrintWriter;
@@ -43,6 +44,7 @@ public PurgeCommand() {
4344
}
4445

4546
@Override
47+
@CheckReturnValue
4648
protected ReplyCallbackAction handleModerationCommand(@NotNull SlashCommandInteractionEvent event, @NotNull Member moderator) {
4749
OptionMapping amountOption = event.getOption("amount");
4850
OptionMapping userOption = event.getOption("user");

src/main/java/net/javadiscord/javabot/systems/qotw/commands/view/QOTWListAnswersSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
5151
}
5252
OptionMapping questionNumOption = event.getOption("question");
5353
if (questionNumOption == null) {
54-
Responses.replyMissingArguments(event);
54+
Responses.replyMissingArguments(event).queue();
5555
return;
5656
}
5757
int questionNum = questionNumOption.getAsInt();

src/main/java/net/javadiscord/javabot/systems/qotw/commands/view/QOTWViewAnswerSubcommand.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
3838
}
3939
OptionMapping questionOption = event.getOption("question");
4040
if (questionOption == null) {
41-
Responses.replyMissingArguments(event);
41+
Responses.replyMissingArguments(event).queue();
4242
return;
4343
}
4444
OptionMapping answerOwnerOption = event.getOption("answerer");
@@ -64,7 +64,10 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
6464
buildQOTWInfoEmbed(submission, event.getMember() == null ? event.getUser().getName() : event.getMember().getEffectiveName()),
6565
"QOTW #" + submission.getQuestionNumber(),
6666
history.getRetrievedHistory(),
67-
() -> Responses.success(event.getHook(), "View Answer", "Answer copied successfully"))),
67+
thread -> {
68+
Responses.success(event.getHook(), "View Answer", "Answer copied successfully").queue();
69+
thread.getManager().setLocked(true);
70+
})),
6871
() -> Responses.error(event.getHook(), "The QOTW-Submission thread was not found.").queue()));
6972
});
7073
}

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/subcommands/MarkBestAnswerSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void sendBestAnswer(InteractionHook hook, List<Message> messages, Member
147147
this.buildBestAnswerEmbed(member),
148148
submissionThread.getName(),
149149
messages,
150-
() -> Responses.success(hook, "Best Answer", "Successfully marked %s as the best answer", submissionThread.getAsMention()).queue());
150+
thread -> Responses.success(hook, "Best Answer", "Successfully marked %s as the best answer", submissionThread.getAsMention()).queue());
151151
}
152152

153153
/**

src/main/java/net/javadiscord/javabot/systems/user_commands/RegexCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
3838
try {
3939
pattern = Pattern.compile(patternOption.getAsString());
4040
} catch (PatternSyntaxException e) {
41-
Responses.error(event, "Please provide a valid regex pattern!");
41+
Responses.error(event, "Please provide a valid regex pattern!").queue();
4242
return;
4343
}
4444
String string = stringOption.getAsString();

src/main/java/net/javadiscord/javabot/util/MessageActionUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.dv8tion.jda.api.entities.GuildMessageChannel;
44
import net.dv8tion.jda.api.entities.Message;
55
import net.dv8tion.jda.api.entities.MessageEmbed;
6+
import net.dv8tion.jda.api.entities.ThreadChannel;
67
import net.dv8tion.jda.api.interactions.components.ActionRow;
78
import net.dv8tion.jda.api.interactions.components.ItemComponent;
89
import net.dv8tion.jda.api.requests.restaction.MessageAction;
@@ -11,6 +12,7 @@
1112
import java.util.EnumSet;
1213
import java.util.List;
1314
import java.util.concurrent.CompletableFuture;
15+
import java.util.function.Consumer;
1416

1517
import org.jetbrains.annotations.NotNull;
1618

@@ -83,7 +85,7 @@ public static CompletableFuture<Message> addAttachmentsAndSend(Message message,
8385
* @param messages The messages to copy.
8486
* @param onFinish A callback to execute when copying is done.
8587
*/
86-
public static void copyMessagesToNewThread(GuildMessageChannel targetChannel, @NotNull MessageEmbed infoEmbed, String newThreadName, List<Message> messages, Runnable onFinish) {
88+
public static void copyMessagesToNewThread(GuildMessageChannel targetChannel, @NotNull MessageEmbed infoEmbed, String newThreadName, List<Message> messages, Consumer<ThreadChannel> onFinish) {
8789
targetChannel.sendMessageEmbeds(infoEmbed).queue(
8890
message -> message.createThreadChannel(newThreadName).queue(
8991
thread -> {
@@ -93,7 +95,7 @@ public static void copyMessagesToNewThread(GuildMessageChannel targetChannel, @N
9395
MessageActionUtils.addAttachmentsAndSend(m, thread.sendMessage(messageContent)
9496
.allowedMentions(EnumSet.of(Message.MentionType.EMOJI, Message.MentionType.CHANNEL)));
9597
});
96-
onFinish.run();
98+
onFinish.accept(thread);
9799
}
98100
));
99101
}

src/main/java/net/javadiscord/javabot/util/Responses.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.javadiscord.javabot.Bot;
1515
import org.jetbrains.annotations.NotNull;
1616

17+
import javax.annotation.CheckReturnValue;
1718
import javax.annotation.Nullable;
1819
import java.awt.*;
1920
import java.time.Instant;
@@ -29,84 +30,104 @@ public final class Responses {
2930
private Responses() {
3031
}
3132

33+
@CheckReturnValue
3234
public static @NotNull ReplyCallbackAction success(CommandInteraction event, String title, String message, Object... args) {
3335
return reply(event, title, String.format(message, args), Type.SUCCESS.getColor(), true);
3436
}
3537

38+
@CheckReturnValue
3639
public static @NotNull WebhookMessageAction<Message> success(InteractionHook hook, String title, String message, Object... args) {
3740
return reply(hook, title, String.format(message, args), Type.SUCCESS.getColor(), true);
3841
}
3942

43+
@CheckReturnValue
4044
public static @NotNull ReplyCallbackAction info(CommandInteraction event, String title, String message, Object... args) {
4145
return reply(event, title, String.format(message, args), Type.INFO.getColor(), true);
4246
}
4347

48+
@CheckReturnValue
4449
public static @NotNull WebhookMessageAction<Message> info(InteractionHook hook, String title, String message, Object... args) {
4550
return reply(hook, title, String.format(message, args), Type.INFO.getColor(), true);
4651
}
4752

53+
@CheckReturnValue
4854
public static @NotNull ReplyCallbackAction error(CommandInteraction event, String message, Object... args) {
4955
return reply(event, "An Error Occurred", String.format(message, args), Type.ERROR.getColor(), true);
5056
}
5157

58+
@CheckReturnValue
5259
public static @NotNull WebhookMessageAction<Message> error(InteractionHook hook, String message, Object... args) {
5360
return reply(hook, "An Error Occurred", String.format(message, args), Type.ERROR.getColor(), true);
5461
}
5562

63+
@CheckReturnValue
5664
public static @NotNull ReplyCallbackAction warning(CommandInteraction event, String message, Object... args) {
5765
return warning(event, null, String.format(message, args));
5866
}
5967

68+
@CheckReturnValue
6069
public static @NotNull WebhookMessageAction<Message> warning(InteractionHook hook, String message, Object... args) {
6170
return warning(hook, null, String.format(message, args));
6271
}
6372

73+
@CheckReturnValue
6474
public static @NotNull ReplyCallbackAction warning(CommandInteraction event, String title, String message, Object... args) {
6575
return reply(event, title, String.format(message, args), Type.WARN.getColor(), true);
6676
}
6777

78+
@CheckReturnValue
6879
public static @NotNull WebhookMessageAction<Message> warning(InteractionHook hook, String title, String message, Object... args) {
6980
return reply(hook, title, String.format(message, args), Type.WARN.getColor(), true);
7081
}
7182

83+
@CheckReturnValue
7284
public static @NotNull ReplyCallbackAction replyMissingArguments(CommandInteraction event) {
7385
return error(event, "Missing required arguments.");
7486
}
7587

88+
@CheckReturnValue
7689
public static @NotNull WebhookMessageAction<Message> replyMissingArguments(InteractionHook hook) {
7790
return error(hook, "Missing required arguments.");
7891
}
7992

93+
@CheckReturnValue
8094
public static @NotNull ReplyCallbackAction replyGuildOnly(CommandInteraction event) {
8195
return error(event, "This command may only be used inside servers.");
8296
}
8397

98+
@CheckReturnValue
8499
public static @NotNull WebhookMessageAction<Message> replyGuildOnly(InteractionHook hook) {
85100
return error(hook, "This command may only be used inside servers.");
86101
}
87102

103+
@CheckReturnValue
88104
public static @NotNull ReplyCallbackAction replyInsufficientPermissions(CommandInteraction event, Permission... permissions) {
89105
return error(event, "I am missing one or more permissions in order to execute this action. (%s)",
90106
Arrays.stream(permissions).map(p -> MarkdownUtil.monospace(p.getName())).collect(Collectors.joining(", ")));
91107
}
92108

109+
@CheckReturnValue
93110
public static @NotNull WebhookMessageAction<Message> replyInsufficientPermissions(InteractionHook hook, Permission... permissions) {
94111
return error(hook, "I am missing one or more permissions in order to execute this action. (%s)",
95112
Arrays.stream(permissions).map(p -> MarkdownUtil.monospace(p.getName())).collect(Collectors.joining(", ")));
96113
}
97114

115+
@CheckReturnValue
98116
public static @NotNull ReplyCallbackAction replyMissingMember(CommandInteraction event) {
99117
return error(event, "The provided user **must** be a member of this server. Please try again.");
100118
}
101119

120+
@CheckReturnValue
102121
public static @NotNull ReplyCallbackAction replyCannotInteract(CommandInteraction event, @NotNull IMentionable mentionable) {
103122
return error(event, "I am missing permissions in order to interact with that. (%s)", mentionable.getAsMention());
104123
}
105124

125+
@CheckReturnValue
106126
public static @NotNull ReplyCallbackAction replyStaffOnly(CommandInteraction event, Guild guild) {
107127
return error(event, "This command may only be used by staff members. (%s)", Bot.getConfig().get(guild).getModerationConfig().getStaffRole().getAsMention());
108128
}
109129

130+
@CheckReturnValue
110131
public static @NotNull ReplyCallbackAction replyAdminOnly(CommandInteraction event, Guild guild) {
111132
return error(event, "This command may only be used by admins. (%s)", Bot.getConfig().get(guild).getModerationConfig().getAdminRole().getAsMention());
112133
}
@@ -121,6 +142,7 @@ private Responses() {
121142
* @param ephemeral Whether the message should be ephemeral.
122143
* @return The reply action.
123144
*/
145+
@CheckReturnValue
124146
private static @NotNull ReplyCallbackAction reply(@NotNull CommandInteraction event, @Nullable String title, String message, Color color, boolean ephemeral) {
125147
return event.replyEmbeds(buildEmbed(title, message, color)).setEphemeral(ephemeral);
126148
}
@@ -135,10 +157,12 @@ private Responses() {
135157
* @param ephemeral Whether the message should be ephemeral.
136158
* @return The webhook message action.
137159
*/
160+
@CheckReturnValue
138161
private static @NotNull WebhookMessageAction<Message> reply(@NotNull InteractionHook hook, @Nullable String title, String message, Color color, boolean ephemeral) {
139162
return hook.sendMessageEmbeds(buildEmbed(title, message, color)).setEphemeral(ephemeral);
140163
}
141164

165+
@CheckReturnValue
142166
private static @NotNull MessageEmbed buildEmbed(@Nullable String title, String message, Color color) {
143167
EmbedBuilder embedBuilder = new EmbedBuilder()
144168
.setTimestamp(Instant.now())

0 commit comments

Comments
 (0)