Skip to content

Commit a3bbe89

Browse files
committed
add DM when post is closed by other user, fix preferences
1 parent b88c4b4 commit a3bbe89

File tree

7 files changed

+65
-19
lines changed

7 files changed

+65
-19
lines changed

src/main/java/net/javadiscord/javabot/listener/UserLeaveListener.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.javadiscord.javabot.systems.help.HelpManager;
1717
import net.javadiscord.javabot.systems.help.dao.HelpAccountRepository;
1818
import net.javadiscord.javabot.systems.help.dao.HelpTransactionRepository;
19+
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceService;
1920

2021
import java.util.function.Consumer;
2122

@@ -31,6 +32,7 @@ public class UserLeaveListener extends ListenerAdapter {
3132
private final BotConfig botConfig;
3233
private final HelpAccountRepository helpAccountRepository;
3334
private final HelpTransactionRepository helpTransactionRepository;
35+
private final UserPreferenceService userPreferenceService;
3436

3537
@Override
3638
public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
@@ -84,7 +86,7 @@ private void executeForAllOpenPostsOfUser(Guild guild, User user, ForumChannel f
8486
}
8587

8688
private void unreserveHelpChannel(ThreadChannel post) {
87-
HelpManager manager = new HelpManager(post, dbActions, botConfig, helpAccountRepository, helpTransactionRepository);
89+
HelpManager manager = new HelpManager(post, dbActions, botConfig, helpAccountRepository, helpTransactionRepository, userPreferenceService);
8890
manager.close(UserSnowflake.fromId(post.getGuild().getSelfMember().getIdLong()), "User left the server");
8991
}
9092
}

src/main/java/net/javadiscord/javabot/systems/help/HelpListener.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import net.javadiscord.javabot.data.h2db.DbActions;
2323
import net.javadiscord.javabot.systems.help.dao.HelpAccountRepository;
2424
import net.javadiscord.javabot.systems.help.dao.HelpTransactionRepository;
25+
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceService;
2526
import net.javadiscord.javabot.util.InteractionUtils;
2627
import net.javadiscord.javabot.util.Responses;
2728
import org.jetbrains.annotations.NotNull;
@@ -73,6 +74,7 @@ protected boolean removeEldestEntry(Map.Entry<Long, Long> eldest) {
7374
return System.currentTimeMillis() > eldest.getValue() || size() >= 32;
7475
}
7576
};
77+
private final UserPreferenceService preferenceService;
7678

7779
@Override
7880
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
@@ -176,7 +178,7 @@ public void handleButton(@NotNull ButtonInteractionEvent event, @NotNull Button
176178
return;
177179
}
178180
ThreadChannel post = event.getChannel().asThreadChannel();
179-
HelpManager manager = new HelpManager(post, dbActions, botConfig, helpAccountRepository, helpTransactionRepository);
181+
HelpManager manager = new HelpManager(post, dbActions, botConfig, helpAccountRepository, helpTransactionRepository, preferenceService);
180182
switch (id[0]) {
181183
case HelpManager.HELP_THANKS_IDENTIFIER -> handleHelpThanksInteraction(event, manager, id);
182184
case HelpManager.HELP_GUIDELINES_IDENTIFIER ->

src/main/java/net/javadiscord/javabot/systems/help/HelpManager.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@
99
import net.dv8tion.jda.api.entities.MessageEmbed;
1010
import net.dv8tion.jda.api.entities.User;
1111
import net.dv8tion.jda.api.entities.UserSnowflake;
12+
import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel;
1213
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
1314
import net.dv8tion.jda.api.entities.emoji.Emoji;
1415
import net.dv8tion.jda.api.interactions.Interaction;
1516
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
1617
import net.dv8tion.jda.api.interactions.components.ActionRow;
1718
import net.dv8tion.jda.api.interactions.components.ItemComponent;
1819
import net.dv8tion.jda.api.interactions.components.buttons.Button;
20+
import net.dv8tion.jda.api.requests.RestAction;
1921
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
2022
import net.javadiscord.javabot.data.config.BotConfig;
2123
import net.javadiscord.javabot.data.config.guild.HelpConfig;
2224
import net.javadiscord.javabot.data.h2db.DbActions;
2325
import net.javadiscord.javabot.systems.help.dao.HelpAccountRepository;
2426
import net.javadiscord.javabot.systems.help.dao.HelpTransactionRepository;
27+
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceService;
28+
import net.javadiscord.javabot.systems.user_preferences.model.Preference;
2529
import net.javadiscord.javabot.util.ExceptionLogger;
2630
import net.javadiscord.javabot.util.MessageActionUtils;
2731
import net.javadiscord.javabot.util.Responses;
@@ -64,6 +68,7 @@ public class HelpManager {
6468
private final HelpAccountRepository helpAccountRepository;
6569

6670
private final HelpTransactionRepository helpTransactionRepository;
71+
private final UserPreferenceService preferenceService;
6772

6873
/**
6974
* Builds and replies {@link ActionRow}s with all members which helped the
@@ -108,6 +113,31 @@ public void close(IReplyCallback callback, boolean withHelpers, @Nullable String
108113
Responses.info(callback, "Post Closed", "This post has been closed by %s%s", callback.getUser().getAsMention(), reason != null ? " for the following reason:\n> " + reason : ".")
109114
.setEphemeral(false)
110115
.queue(s -> postThread.getManager().setLocked(true).setArchived(true).queue());
116+
if (callback.getMember().getIdLong() != postThread.getOwnerIdLong() &&
117+
Boolean.parseBoolean(preferenceService.getOrCreate(postThread.getOwnerIdLong(), Preference.PRIVATE_CLOSE_NOTIFICATIONS).getState())) {
118+
postThread.getOwner().getUser().openPrivateChannel()
119+
.flatMap(c -> createDMCloseInfoEmbed(callback.getMember(), postThread, reason, c))
120+
.queue(success -> {}, failure -> {});
121+
}
122+
}
123+
124+
private RestAction<Message> createDMCloseInfoEmbed(Member closingMember, ThreadChannel postThread, String reason, PrivateChannel c) {
125+
EmbedBuilder eb = new EmbedBuilder()
126+
.setTitle("Post closed")
127+
.setDescription(
128+
"""
129+
Your post %s has been closed by %s
130+
131+
[Post link](%s)
132+
"""
133+
.formatted(postThread.getAsMention(), closingMember.getAsMention(), postThread.getJumpUrl()))
134+
.addField("Deactivate", "You can deactivate notifications like this using the `/preferences` command.", false)
135+
.setAuthor(closingMember.getEffectiveName(), null, closingMember.getEffectiveAvatarUrl());
136+
if (reason != null) {
137+
eb
138+
.addField("Reason", reason, false);
139+
}
140+
return c.sendMessageEmbeds(eb.build());
111141
}
112142

113143
/**

src/main/java/net/javadiscord/javabot/systems/help/commands/CloseCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.javadiscord.javabot.data.h2db.DbActions;
77
import net.javadiscord.javabot.systems.help.dao.HelpAccountRepository;
88
import net.javadiscord.javabot.systems.help.dao.HelpTransactionRepository;
9+
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceService;
910

1011
/**
1112
* A simple command that can be used inside reserved help channels to immediately unreserve them,
@@ -21,9 +22,10 @@ public class CloseCommand extends UnreserveCommand {
2122
* @param dbActions A utility object providing various operations on the main database
2223
* @param helpTransactionRepository Dao object that represents the HELP_TRANSACTION SQL Table.
2324
* @param helpAccountRepository Dao object that represents the HELP_ACCOUNT SQL Table.
25+
* @param preferenceService Service for user preferences
2426
*/
25-
public CloseCommand(BotConfig botConfig, DbActions dbActions, HelpTransactionRepository helpTransactionRepository, HelpAccountRepository helpAccountRepository) {
26-
super(botConfig, dbActions, helpTransactionRepository, helpAccountRepository);
27+
public CloseCommand(BotConfig botConfig, DbActions dbActions, HelpTransactionRepository helpTransactionRepository, HelpAccountRepository helpAccountRepository, UserPreferenceService preferenceService) {
28+
super(botConfig, dbActions, helpTransactionRepository, helpAccountRepository, preferenceService);
2729
setCommandData(
2830
Commands.slash("close", "Unreserves this post marking your question/issue as resolved.")
2931
.setGuildOnly(true).addOption(OptionType.STRING, "reason",

src/main/java/net/javadiscord/javabot/systems/help/commands/UnreserveCommand.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.javadiscord.javabot.data.h2db.DbActions;
1212
import net.javadiscord.javabot.systems.help.dao.HelpAccountRepository;
1313
import net.javadiscord.javabot.systems.help.dao.HelpTransactionRepository;
14+
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceService;
1415
import net.javadiscord.javabot.systems.help.HelpManager;
1516
import net.javadiscord.javabot.util.Responses;
1617
import org.jetbrains.annotations.NotNull;
@@ -25,19 +26,22 @@ public class UnreserveCommand extends SlashCommand {
2526
private final DbActions dbActions;
2627
private final HelpAccountRepository helpAccountRepository;
2728
private final HelpTransactionRepository helpTransactionRepository;
29+
private final UserPreferenceService preferenceService;
2830

2931
/**
3032
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
3133
* @param botConfig The main configuration of the bot
3234
* @param dbActions A utility object providing various operations on the main database
33-
* @param helpTransactionRepository Dao object that represents the HELP_TRANSACTION SQL Table.
34-
* @param helpAccountRepository Dao object that represents the HELP_ACCOUNT SQL Table.
35+
* @param helpTransactionRepository Dao object that represents the HELP_TRANSACTION SQL Table
36+
* @param helpAccountRepository Dao object that represents the HELP_ACCOUNT SQL Table
37+
* @param preferenceService Service for user preferences
3538
*/
36-
public UnreserveCommand(BotConfig botConfig, DbActions dbActions, HelpTransactionRepository helpTransactionRepository, HelpAccountRepository helpAccountRepository) {
39+
public UnreserveCommand(BotConfig botConfig, DbActions dbActions, HelpTransactionRepository helpTransactionRepository, HelpAccountRepository helpAccountRepository, UserPreferenceService preferenceService) {
3740
this.botConfig = botConfig;
3841
this.dbActions = dbActions;
3942
this.helpAccountRepository = helpAccountRepository;
4043
this.helpTransactionRepository = helpTransactionRepository;
44+
this.preferenceService = preferenceService;
4145
setCommandData(Commands.slash("unreserve", "Unreserves this post marking your question/issue as resolved.")
4246
.setGuildOnly(true)
4347
.addOption(OptionType.STRING, "reason", "The reason why you're unreserving this channel", false)
@@ -55,11 +59,11 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
5559
if (postThread.getParentChannel().getType() != ChannelType.FORUM) {
5660
replyInvalidChannel(event);
5761
}
58-
HelpManager manager = new HelpManager(postThread, dbActions, botConfig, helpAccountRepository, helpTransactionRepository);
62+
HelpManager manager = new HelpManager(postThread, dbActions, botConfig, helpAccountRepository, helpTransactionRepository, preferenceService);
5963
if (manager.isForumEligibleToBeUnreserved(event.getInteraction())) {
60-
manager.close(event, event.getUser().getIdLong() == postThread.getOwnerIdLong(),
61-
event.getOption("reason", null, OptionMapping::getAsString)
62-
);
64+
manager.close(event,
65+
event.getUser().getIdLong() == manager.getPostThread().getOwnerIdLong(),
66+
event.getOption("reason", null, OptionMapping::getAsString));
6367
} else {
6468
Responses.warning(event, "Could not close this post", "You're not allowed to close this post.").queue();
6569
}

src/main/java/net/javadiscord/javabot/systems/user_preferences/commands/PreferencesSetSubcommand.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.jetbrains.annotations.Contract;
1818
import org.jetbrains.annotations.NotNull;
1919

20+
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.List;
2223

@@ -58,24 +59,24 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
5859
return;
5960
}
6061
if (service.setOrCreate(event.getUser().getIdLong(), preference, state)) {
61-
Responses.info(event, "Preference Updated", "Successfully set %s to `%s`!", preference, state).queue();
62+
Responses.info(event, "Preference Updated", "Successfully set `%s` to `%s`!", preference, state).queue();
6263
} else {
6364
Responses.error(event, "Could not set %s to `%s`.", preference, state).queue();
6465
}
6566
}
6667

6768
@Contract("_ -> new")
6869
private Command.@NotNull Choice toChoice(@NotNull Preference preference) {
69-
return new Command.Choice(preference.toString(), String.valueOf(preference.ordinal()));
70+
return new Command.Choice(preference.toString(), preference.ordinal());
7071
}
7172

7273
@Override
7374
public void handleAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event, @NotNull AutoCompleteQuery target) {
74-
String preferenceString = event.getOption("preference", OptionMapping::getAsString);
75-
if (preferenceString != null && Arrays.stream(Preference.values()).map(Preference::name).anyMatch(c -> c.equals(preferenceString))) {
76-
Preference preference = Preference.valueOf(preferenceString);
75+
int preferenceInt = event.getOption("preference",-1, OptionMapping::getAsInt);
76+
if (preferenceInt > 0 && preferenceInt<Preference.values().length) {
77+
Preference preference = Preference.values()[preferenceInt];
7778
if (preference.getType().getDefaultChoices() != null && preference.getType().getDefaultChoices().length > 0) {
78-
event.replyChoices(AutoCompleteUtils.filterChoices(event, List.of(preference.getType().getDefaultChoices()))).queue();
79+
event.replyChoices(AutoCompleteUtils.filterChoices(event, new ArrayList<>(List.of(preference.getType().getDefaultChoices())))).queue();
7980
}
8081
}
8182
}

src/main/java/net/javadiscord/javabot/systems/user_preferences/model/Preference.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ public enum Preference {
99
*/
1010
QOTW_REMINDER("Question of the Week Reminder", "false", new BooleanPreference()),
1111
/**
12-
* Enables/Disables DM notifications for dormant help post.
12+
* Enables/Disables DM notifications for dormant help posts.
1313
*/
14-
PRIVATE_DORMANT_NOTIFICATIONS("Send notifications about dormant help post via DM", "true", new BooleanPreference());
14+
PRIVATE_DORMANT_NOTIFICATIONS("Send notifications about dormant help post via DM", "true", new BooleanPreference()),
15+
16+
/**
17+
* Enables/Disables DM notifications for closed help posts.
18+
*/
19+
PRIVATE_CLOSE_NOTIFICATIONS("Send notifications about help posts closed by other users via DM", "true", new BooleanPreference());
1520

1621
private final String name;
1722
private final String defaultState;

0 commit comments

Comments
 (0)