Skip to content
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

Logging the /tag-manage command #296

Merged
merged 77 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
eb273b9
`minor fix`
Dec 2, 2021
b0ce95d
fixed auto-checked problems
Dec 3, 2021
e06af67
`minor fix`
Dec 3, 2021
5545a72
added `content.md` files to the logs and optimized logging methods to…
Dec 3, 2021
3c8c59c
removed duplicate `getTextChannels()`
Dec 3, 2021
202e117
made `log()` `synchronized` to prevent interruptions
Dec 3, 2021
a4447cf
made `enum Filename`
Dec 3, 2021
ccbaecb
files are now in the same message as the embed
Dec 4, 2021
b9f98fa
changed `oldContent` to `previousContent`
Dec 4, 2021
a78c039
changed `class VirtualFile` to `record Attachment`
Dec 4, 2021
40ce619
made `class ModAuditLogWriter` an `enum`
Dec 4, 2021
4bfb896
fixed minor code-style problems
Dec 4, 2021
d7e3ccf
improved the structure of `ModAuditLogWriter#log()`
Dec 5, 2021
60e328b
merged the 3 logging methods in `TagManageCommand`
Dec 5, 2021
123aa43
now using `Optional#orElseThrow()` instead of `Optional#get()`
Dec 5, 2021
d5438e6
changed error message `"Shouldn't log subcommand"`
Dec 5, 2021
c4e752b
made arrays `@NotNull`
Dec 5, 2021
e94a720
replaced `Filename` enum with constants
Dec 7, 2021
3c9658e
now checking for `null` instead of abusing Optionals
Dec 7, 2021
81b43b3
`ModAuditLogWriter#log` now takes the attachments as a List instead o…
Dec 7, 2021
e9e4a25
updated a minor javadoc
Dec 7, 2021
35411af
renamed `ModAuditLogWriter#log()` to `writeModAuditLog()`
Dec 7, 2021
b462051
changed the embed color from `#4FC3F7` to `#3788AC`
Dec 7, 2021
d0f3340
removed duplicate `getModAuditLogChannel()`
Dec 7, 2021
3e3a7b6
now ignores `NoSuchElementException`
Dec 7, 2021
70c680c
`actionVerb` is now a field in `Subcommand` enum
Dec 7, 2021
64aaab4
removed unused field and var
Dec 7, 2021
91a968d
moved duplicate debug message
Dec 7, 2021
6f6a342
removed static import
interacsion Dec 24, 2021
ae123c5
Improved JavaDoc.
interacsion Jan 20, 2022
b23e7f5
Added missing imports
interacsion Jan 21, 2022
54c9daf
removed `static final` from `record`
interacsion Jan 21, 2022
071fc9b
Slightly updated previous content retrieval code
interacsion Jan 21, 2022
b494b75
now using `EnumSet` instead of `List`
interacsion Jan 21, 2022
5bc61ff
made `logAction()` fail-fast
interacsion Jan 21, 2022
358057c
removed unnecessary constructor
interacsion Jan 21, 2022
4377d89
made `Subcommand` enum methods `@NotNull`
interacsion Jan 21, 2022
6eb5898
moved predicates directly into the variable declaration
interacsion Jan 21, 2022
e8a7f0b
updated `Attachment` `record`
interacsion Jan 21, 2022
7c6823d
moved the code that gets previous content into a helper method, getTa…
interacsion Jan 21, 2022
a221436
now using `actionVerb` as the verb in the message
interacsion Jan 21, 2022
24def34
renamed `writeModAuditLog()` to `write`
interacsion Jan 21, 2022
7c49c73
removed `write()` overloads and now getting attachments as a vararg
interacsion Jan 21, 2022
a9a4339
renamed `getModAuditLogChannel()` to `getAndHandleModAuditLogChannel()`
interacsion Jan 21, 2022
81482cd
fixed code-style.
interacsion Jan 21, 2022
1a8b799
fixed a merge issue
interacsion Jan 26, 2022
429d9b7
using constant `CONTENT_FILE_NAME` instead of `"content.md"`
interacsion Jan 27, 2022
d011b34
Now using a constant for the log embed description
interacsion Jan 27, 2022
3b9b8b9
code-style quick fix
interacsion Jan 27, 2022
749db7b
moved comment to be above the constant, instead of in front of it.
interacsion Jan 27, 2022
7d25c29
Put audit log channel name pattern in a field
interacsion Jan 27, 2022
97c4f32
renamed field.
interacsion Jan 27, 2022
1cf7627
spotless applied.
interacsion Jan 28, 2022
ccac112
Removed useless imports.
interacsion Feb 9, 2022
f00590a
Updated `getTagContent()`
interacsion Mar 2, 2022
e848ac0
Some code changes
interacsion Mar 11, 2022
4d1f09a
Updated `ModAuditLogWriter`'s javadoc
interacsion Mar 11, 2022
4d55f1f
changed variable name
interacsion Mar 11, 2022
f8f6608
updated `getTagContent()`
interacsion Mar 11, 2022
86b93e5
Moved enum set into a variable.
interacsion Mar 11, 2022
2521bcb
code reformat
interacsion Mar 11, 2022
1a67ae9
code reformat
interacsion Mar 11, 2022
f036077
moved field and var into `Subcommand`
interacsion Mar 11, 2022
7b101c6
made fields `private`
interacsion Mar 11, 2022
4b20e3c
removed unnecessary `this.`
interacsion Mar 11, 2022
da0b177
Rebased and made `ModAuditLogWriter` singleton
interacsion Mar 11, 2022
ddc1b88
Using a `BiConsumer` to minimize code duplication.
interacsion Mar 11, 2022
2661399
`getTagContent()` is now returning `null`
interacsion Mar 11, 2022
f6ad120
Made test compile again
interacsion Mar 11, 2022
1f493f3
now verifying wanted results in tests
interacsion Mar 11, 2022
a3692ba
quick patch
interacsion Mar 12, 2022
7a812b7
Updated `Unable to retrieve content` handling
interacsion Mar 12, 2022
4a19004
made `auditLogChannelNamePattern` compile in constructor
interacsion Mar 12, 2022
279d3aa
changed `logAction()` logic
interacsion Mar 12, 2022
7639f71
Updated `getTagContent()` javadoc
interacsion Mar 12, 2022
2111dc8
replaced `ArrayList` with `List`
interacsion Mar 12, 2022
cf98c4a
put predicate in the field instead of the pattern
interacsion Mar 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.togetherjava.tjbot.commands.tophelper.TopHelpersPurgeMessagesRoutine;
import org.togetherjava.tjbot.config.Config;
import org.togetherjava.tjbot.db.Database;
import org.togetherjava.tjbot.moderation.ModAuditLogWriter;
import org.togetherjava.tjbot.routines.ModAuditLogRoutine;

import java.util.ArrayList;
Expand Down Expand Up @@ -50,14 +51,15 @@ public enum Features {
@NotNull Database database, @NotNull Config config) {
TagSystem tagSystem = new TagSystem(database);
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config);

// NOTE The system can add special system relevant commands also by itself,
// hence this list may not necessarily represent the full list of all commands actually
// available.
Collection<Feature> features = new ArrayList<>();

// Routines
features.add(new ModAuditLogRoutine(database, config));
features.add(new ModAuditLogRoutine(database, config, modAuditLogWriter));
features.add(new TemporaryModerationRoutine(jda, actionsStore, config));
features.add(new TopHelpersPurgeMessagesRoutine(database));
features.add(new RemindRoutine(database));
Expand All @@ -73,7 +75,7 @@ public enum Features {
features.add(new PingCommand());
features.add(new TeXCommand());
features.add(new TagCommand(tagSystem));
features.add(new TagManageCommand(tagSystem, config));
features.add(new TagManageCommand(tagSystem, config, modAuditLogWriter));
features.add(new TagsCommand(tagSystem));
features.add(new VcActivityCommand());
features.add(new WarnCommand(actionsStore, config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,24 @@ public final class TagManageCommand extends SlashCommandAdapter {
private final TagSystem tagSystem;
private final Predicate<String> hasRequiredRole;

private final ModAuditLogWriter modAuditLogWriter;

/**
* Creates a new instance, using the given tag system as base.
*
* @param tagSystem the system providing the actual tag data
* @param config the config to use for this
* @param modAuditLogWriter the mod audit log writer to use for this
Zabuzard marked this conversation as resolved.
Show resolved Hide resolved
*/
public TagManageCommand(TagSystem tagSystem, @NotNull Config config) {
public TagManageCommand(TagSystem tagSystem, @NotNull Config config,
Zabuzard marked this conversation as resolved.
Show resolved Hide resolved
@NotNull ModAuditLogWriter modAuditLogWriter) {
super("tag-manage", "Provides commands to manage all tags", SlashCommandVisibility.GUILD);

this.tagSystem = tagSystem;
hasRequiredRole = Pattern.compile(config.getTagManageRolePattern()).asMatchPredicate();

this.modAuditLogWriter = modAuditLogWriter;

// TODO Think about adding a "Are you sure"-dialog to 'edit', 'edit-with-message' and
// 'delete'
getData().addSubcommands(new SubcommandData(Subcommand.RAW.name,
Expand Down Expand Up @@ -359,33 +365,33 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild,
}

switch (subcommand) {
case CREATE -> ModAuditLogWriter.write("Tag-Manage Create",
case CREATE -> modAuditLogWriter.write("Tag-Manage Create",
String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author,
triggeredAt, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME,
Objects.requireNonNull(newContent)));

case CREATE_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Create with message",
case CREATE_WITH_MESSAGE -> modAuditLogWriter.write("Tag-Manage Create with message",
String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author,
triggeredAt, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME,
Objects.requireNonNull(newContent)));

case EDIT -> ModAuditLogWriter.write("Tag-Manage Edit",
case EDIT -> modAuditLogWriter.write("Tag-Manage Edit",
String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author,
triggeredAt, guild,
new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME,
Objects.requireNonNull(newContent)),
new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME,
Objects.requireNonNull(previousContent)));

case EDIT_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Edit with message",
case EDIT_WITH_MESSAGE -> modAuditLogWriter.write("Tag-Manage Edit with message",
String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author,
triggeredAt, guild,
new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME,
Objects.requireNonNull(newContent)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already checked whenever it's null?

You do this for all attachments, and all it does is making your code unreadable, for fun?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe SonarLint picks on that if I don't do it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does IntelliJ pick on it?
If yes, I understand
If no, fuck sonarlint it just needs a brain.

I'd personally just ignore it since I know it's not true, you literally check or it's null above.
and even if I wasn't sure whenever it was null, Objects#requireNonNull throws an empty NPE in this case, I'd just let the method it runs throw the NPE with more info

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I could do, is to make these parameters @NotNull, and to use a junk string like this "" for cases where the parameter is not used. that would also get rid of the null checks. whats your opinion, @Tais993, @Zabuzard?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, having an empty string sound like odd behaviour.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats indeed a bad idea. the null-detection tool is there to help u. if u bypass it on purpose, ur just fighting against urself, thats the worst thing u could do.

i doubt sonar picks up on this, its intellijs null-detection feature. so u can safely push it without the objects.requirenonnull. if the warning annoys u, lower its level to "suggestion".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both IntelliJ and Sonar doesn't like that, actually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please show the sonar error message, cause I really doubt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate with #296 (comment)

new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME,
Objects.requireNonNull(previousContent)));

case DELETE -> ModAuditLogWriter.write("Tag-Manage Delete",
case DELETE -> modAuditLogWriter.write("Tag-Manage Delete",
String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author,
triggeredAt, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME,
Objects.requireNonNull(previousContent)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@
* Use {@link ModAuditLogWriter#write(String, String, User, TemporalAccessor, Guild, Attachment...)}
* to log an entry.
*/
public enum ModAuditLogWriter {
;
public final class ModAuditLogWriter {
private static final Color EMBED_COLOR = Color.decode("#3788AC");

private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class);
private final Config config;

private static Pattern auditLogChannelNamePattern = null;
private final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class);
Zabuzard marked this conversation as resolved.
Show resolved Hide resolved

private Pattern auditLogChannelNamePattern = null;

/**
* Creates a new instance.
*
* @param config the config to use for this
*/
public ModAuditLogWriter(@NotNull Config config) {
this.config = config;
}

/**
* Sends a log on the mod audit log channel.
Expand All @@ -43,8 +53,8 @@ public enum ModAuditLogWriter {
* @param guild the guild to write this log to
* @param attachments attachments that will be added to the message. none or many.
*/
public static void write(@NotNull String title, @NotNull String description,
@NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild,
public void write(@NotNull String title, @NotNull String description, @NotNull User author,
@NotNull TemporalAccessor timestamp, @NotNull Guild guild,
@NotNull Attachment... attachments) {
Optional<TextChannel> auditLogChannel = getAndHandleModAuditLogChannel(guild);
if (auditLogChannel.isEmpty()) {
Expand All @@ -71,10 +81,9 @@ public static void write(@NotNull String title, @NotNull String description,
*
* @param guild the guild to look for the channel in
*/
public static Optional<TextChannel> getAndHandleModAuditLogChannel(@NotNull Guild guild) {
public Optional<TextChannel> getAndHandleModAuditLogChannel(@NotNull Guild guild) {
if (auditLogChannelNamePattern == null) {
auditLogChannelNamePattern =
Pattern.compile(Config.getInstance().getModAuditLogChannelPattern());
auditLogChannelNamePattern = Pattern.compile(config.getModAuditLogChannelPattern());
Zabuzard marked this conversation as resolved.
Show resolved Hide resolved
}

Optional<TextChannel> auditLogChannel = guild.getTextChannelCache()
Expand All @@ -86,7 +95,7 @@ public static Optional<TextChannel> getAndHandleModAuditLogChannel(@NotNull Guil
if (auditLogChannel.isEmpty()) {
logger.warn(
"Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'",
Config.getInstance().getModAuditLogChannelPattern(), guild.getName());
config.getModAuditLogChannelPattern(), guild.getName());
}
return auditLogChannel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ public final class ModAuditLogRoutine implements Routine {

private final Database database;
private final Config config;
private final ModAuditLogWriter modAuditLogWriter;

/**
* Creates a new instance.
*
* @param database the database for memorizing audit log dates
* @param config the config to use for this
* @param modAuditLogWriter the mod audit log writer to use for this
Zabuzard marked this conversation as resolved.
Show resolved Hide resolved
*/
public ModAuditLogRoutine(@NotNull Database database, @NotNull Config config) {
public ModAuditLogRoutine(@NotNull Database database, @NotNull Config config,
@NotNull ModAuditLogWriter modAuditLogWriter) {
this.config = config;
this.database = database;
this.modAuditLogWriter = modAuditLogWriter;
}

private static @NotNull RestAction<AuditLogMessage> handleAction(@NotNull Action action,
Expand Down Expand Up @@ -215,7 +219,7 @@ private void checkAuditLogsRoutine(@NotNull JDA jda) {
}

Optional<TextChannel> auditLogChannel =
ModAuditLogWriter.getAndHandleModAuditLogChannel(guild);
modAuditLogWriter.getAndHandleModAuditLogChannel(guild);
if (auditLogChannel.isEmpty()) {
return;
}
Expand Down