-
-
Notifications
You must be signed in to change notification settings - Fork 92
Added /note command #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added /note command #384
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package org.togetherjava.tjbot.commands.moderation; | ||
|
||
import net.dv8tion.jda.api.entities.*; | ||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; | ||
import net.dv8tion.jda.api.interactions.Interaction; | ||
import net.dv8tion.jda.api.interactions.commands.OptionMapping; | ||
import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.togetherjava.tjbot.commands.SlashCommandAdapter; | ||
import org.togetherjava.tjbot.commands.SlashCommandVisibility; | ||
import org.togetherjava.tjbot.config.Config; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
/** | ||
* This command allows users to write notes about others. Notes are persisted and can be retrieved | ||
* using {@link AuditCommand}, like other moderative actions. | ||
* <p> | ||
* The command fails if the user triggering it is lacking permissions to either write a note about | ||
* other users or to write a note about the specific given user (for example a moderator attempting | ||
* to write a note about an admin). | ||
*/ | ||
public final class NoteCommand extends SlashCommandAdapter { | ||
private static final Logger logger = LoggerFactory.getLogger(NoteCommand.class); | ||
private static final String USER_OPTION = "user"; | ||
private static final String CONTENT_OPTION = "content"; | ||
private static final String ACTION_VERB = "write a note about"; | ||
private final ModerationActionsStore actionsStore; | ||
private final Predicate<String> hasRequiredRole; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param actionsStore used to store actions issued by this command | ||
* @param config the config to use for this | ||
*/ | ||
public NoteCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config config) { | ||
super("note", "Writes a note about the given user", SlashCommandVisibility.GUILD); | ||
|
||
getData() | ||
.addOption(OptionType.USER, USER_OPTION, "The user who you want to write a note about", | ||
true) | ||
.addOption(OptionType.STRING, CONTENT_OPTION, | ||
"The content of the note you want to write", true); | ||
|
||
hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); | ||
this.actionsStore = Objects.requireNonNull(actionsStore); | ||
} | ||
|
||
@Override | ||
public void onSlashCommand(@NotNull SlashCommandEvent event) { | ||
OptionMapping targetOption = event.getOption(USER_OPTION); | ||
Member author = event.getMember(); | ||
Guild guild = event.getGuild(); | ||
String content = event.getOption(CONTENT_OPTION).getAsString(); | ||
|
||
if (!handleChecks(guild.getSelfMember(), author, targetOption.getAsMember(), content, | ||
event)) { | ||
return; | ||
} | ||
|
||
sendNote(targetOption.getAsUser(), author, content, guild, event); | ||
} | ||
|
||
@SuppressWarnings("BooleanMethodNameMustStartWithQuestion") | ||
private boolean handleChecks(@NotNull Member bot, @NotNull Member author, | ||
@Nullable Member target, CharSequence content, @NotNull Interaction event) { | ||
if (target != null && !ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, | ||
target, event)) { | ||
return false; | ||
} | ||
|
||
if (!ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event)) { | ||
return false; | ||
} | ||
|
||
return ModerationUtils.handleReason(content, event); | ||
} | ||
|
||
private void sendNote(@NotNull User target, @NotNull Member author, @NotNull String content, | ||
@NotNull ISnowflake guild, @NotNull Interaction event) { | ||
storeNote(target, author, content, guild); | ||
sendFeedback(target, author, content, event); | ||
} | ||
|
||
private void storeNote(@NotNull User target, @NotNull Member author, @NotNull String content, | ||
@NotNull ISnowflake guild) { | ||
logger.info("'{}' ({}) wrote a note about the user '{}' ({}) with content '{}'.", | ||
author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(), | ||
content); | ||
|
||
actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(), | ||
ModerationAction.NOTE, null, content); | ||
} | ||
|
||
private static void sendFeedback(@NotNull User target, @NotNull Member author, | ||
@NotNull String noteContent, @NotNull Interaction event) { | ||
MessageEmbed feedback = ModerationUtils.createActionResponse(author.getUser(), | ||
ModerationAction.NOTE, target, null, noteContent); | ||
|
||
event.replyEmbeds(feedback).queue(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.