Skip to content

Commit d9bdcc3

Browse files
authored
Merge pull request #438 from danthe1st/auto-quiet
automatically determine quiet-ness based on channel
2 parents 7d556df + dd758d2 commit d9bdcc3

File tree

10 files changed

+59
-8
lines changed

10 files changed

+59
-8
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import net.dv8tion.jda.api.entities.Message;
66
import net.dv8tion.jda.api.entities.User;
77
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
8-
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
98
import net.dv8tion.jda.api.interactions.commands.OptionType;
109
import net.dv8tion.jda.api.interactions.commands.build.Commands;
1110
import net.javadiscord.javabot.data.config.BotConfig;
@@ -52,7 +51,7 @@ protected WebhookMessageCreateAction<Message> handleModerationUserCommand(@Nonnu
5251
if (!Checks.hasPermission(event.getGuild(), Permission.BAN_MEMBERS)) {
5352
return Responses.replyInsufficientPermissions(event.getHook(), Permission.BAN_MEMBERS);
5453
}
55-
boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean);
54+
boolean quiet = isQuiet(event);
5655
ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool);
5756
service.ban(target, reason, commandUser, event.getChannel(), quiet);
5857
return Responses.success(event.getHook(), "User Banned", "%s has been banned.", target.getAsMention());

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import net.dv8tion.jda.api.entities.Message;
66
import net.dv8tion.jda.api.entities.User;
77
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
8-
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
98
import net.dv8tion.jda.api.interactions.commands.OptionType;
109
import net.dv8tion.jda.api.interactions.commands.build.Commands;
1110
import net.javadiscord.javabot.data.config.BotConfig;
@@ -52,7 +51,7 @@ protected WebhookMessageCreateAction<Message> handleModerationUserCommand(@Nonnu
5251
if (!Checks.hasPermission(event.getGuild(), Permission.KICK_MEMBERS)) {
5352
return Responses.replyInsufficientPermissions(event.getHook(), Permission.KICK_MEMBERS);
5453
}
55-
boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean);
54+
boolean quiet = isQuiet(event);
5655
ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool);
5756
service.kick(target, reason, event.getMember(), event.getChannel(), quiet);
5857
return Responses.success(event.getHook(), "User Kicked", "%s has been kicked.", target.getAsMention());

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,33 @@ protected ReplyCallbackAction handleModerationCommand(@NotNull SlashCommandInter
4949
}
5050

5151
protected abstract WebhookMessageCreateAction<Message> handleModerationUserCommand(@Nonnull SlashCommandInteractionEvent event, @Nonnull Member commandUser, @Nonnull User target, @Nullable String reason);
52+
53+
/**
54+
* Determines whether this command is executed quitely.
55+
*
56+
* If it is, no (public) response should be sent in the current channel. This does not effect logging.
57+
*
58+
* By default, moderative actions in the log channel are quiet.
59+
* @param event The {@link SlashCommandInteractionEvent} corresponding to the executed command
60+
* @return {@code true} iff the command is quiet, else {@code false}
61+
*/
62+
protected boolean isQuiet(SlashCommandInteractionEvent event) {
63+
return isQuiet(botConfig, event);
64+
}
65+
66+
/**
67+
* Determines whether this command is executed quitely.
68+
*
69+
* If it is, no (public) response should be sent in the current channel. This does not effect logging.
70+
*
71+
* By default, moderative actions in the log channel are quiet.
72+
* @param botConfig the main configuration of the bot
73+
* @param event The {@link SlashCommandInteractionEvent} corresponding to the executed command
74+
* @return {@code true} iff the command is quiet, else {@code false}
75+
*/
76+
public static boolean isQuiet(BotConfig botConfig, SlashCommandInteractionEvent event) {
77+
return event.getOption("quiet",
78+
() -> event.getChannel().getIdLong() == botConfig.get(event.getGuild()).getModerationConfig().getLogChannelId(),
79+
OptionMapping::getAsBoolean);
80+
}
5281
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public UnbanCommand(NotificationService notificationService, BotConfig botConfig
4040
setModerationSlashCommandData(Commands.slash("unban", "Unbans a member")
4141
.addOption(OptionType.STRING, "id", "The id of the user you want to unban", true)
4242
.addOption(OptionType.STRING, "reason", "The reason for unbanning this user", true)
43+
.addOption(OptionType.BOOLEAN, "quiet", "If true, don't send a message in the server channel where the unban is issued.", false)
4344
);
4445
}
4546

@@ -51,7 +52,7 @@ protected ReplyCallbackAction handleModerationCommand(@NotNull SlashCommandInter
5152
return Responses.replyMissingArguments(event);
5253
}
5354
long id = idOption.getAsLong();
54-
boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean);
55+
boolean quiet = ModerateUserCommand.isQuiet(botConfig, event);
5556
ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool);
5657
if (service.unban(id, reasonOption.getAsString(), event.getMember(), event.getChannel(), quiet)) {
5758
return Responses.success(event, "User Unbanned", "User with id `%s` has been unbanned.", id);

src/main/java/net/javadiscord/javabot/systems/moderation/timeout/AddTimeoutSubcommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1111
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
1212
import net.javadiscord.javabot.data.config.BotConfig;
13+
import net.javadiscord.javabot.systems.moderation.ModerateUserCommand;
1314
import net.javadiscord.javabot.systems.moderation.ModerationService;
1415
import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository;
1516
import net.javadiscord.javabot.systems.notification.NotificationService;
@@ -75,7 +76,7 @@ protected ReplyCallbackAction handleTimeoutCommand(@NotNull SlashCommandInteract
7576
return Responses.error(event, "This command can only be performed in a server message channel.");
7677
}
7778
MessageChannel channel = event.getMessageChannel();
78-
boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean);
79+
boolean quiet = ModerateUserCommand.isQuiet(botConfig, event);
7980
if (member.isTimedOut()) {
8081
return Responses.error(event, "Could not timeout %s; they're already timed out.", member.getAsMention());
8182
}

src/main/java/net/javadiscord/javabot/systems/moderation/timeout/RemoveTimeoutSubcommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1010
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
1111
import net.javadiscord.javabot.data.config.BotConfig;
12+
import net.javadiscord.javabot.systems.moderation.ModerateUserCommand;
1213
import net.javadiscord.javabot.systems.moderation.ModerationService;
1314
import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository;
1415
import net.javadiscord.javabot.systems.notification.NotificationService;
@@ -59,7 +60,7 @@ protected ReplyCallbackAction handleTimeoutCommand(@NotNull SlashCommandInteract
5960
if (!channel.getType().isMessage()) {
6061
return Responses.error(event, "This command can only be performed in a server message channel.");
6162
}
62-
boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean);
63+
boolean quiet = ModerateUserCommand.isQuiet(botConfig, event);
6364
if (!member.isTimedOut()) {
6465
return Responses.error(event, "Could not remove timeout from member %s; they're not timed out.", member.getAsMention());
6566
}

src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardAllWarnsSubcommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.javadiscord.javabot.systems.moderation.ModerationService;
1212
import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository;
1313
import net.javadiscord.javabot.systems.notification.NotificationService;
14+
import net.javadiscord.javabot.util.Checks;
1415
import net.javadiscord.javabot.util.Responses;
1516

1617
import java.util.concurrent.ExecutorService;
@@ -54,6 +55,10 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
5455
Responses.replyGuildOnly(event).queue();
5556
return;
5657
}
58+
if(!Checks.hasStaffRole(botConfig, event.getMember())) {
59+
Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue();
60+
return;
61+
}
5762
User target = userMapping.getAsUser();
5863
new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool).discardAllWarns(target, event.getMember());
5964
Responses.success(event, "Warns Discarded", "Successfully discarded all warns from **%s**.", UserUtils.getUserTag(target)).queue();

src/main/java/net/javadiscord/javabot/systems/moderation/warn/DiscardWarnByIdSubCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.javadiscord.javabot.systems.moderation.ModerationService;
1010
import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository;
1111
import net.javadiscord.javabot.systems.notification.NotificationService;
12+
import net.javadiscord.javabot.util.Checks;
1213
import net.javadiscord.javabot.util.Responses;
1314

1415
import java.util.concurrent.ExecutorService;
@@ -52,6 +53,10 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
5253
Responses.replyGuildOnly(event).queue();
5354
return;
5455
}
56+
if(!Checks.hasStaffRole(botConfig, event.getMember())) {
57+
Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue();
58+
return;
59+
}
5560
int id = idMapping.getAsInt();
5661
ModerationService service = new ModerationService(notificationService, botConfig, event, warnRepository, asyncPool);
5762
if (service.discardWarnById(id, event.getUser())) {

src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnAddSubcommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
99
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1010
import net.javadiscord.javabot.data.config.BotConfig;
11+
import net.javadiscord.javabot.systems.moderation.ModerateUserCommand;
1112
import net.javadiscord.javabot.systems.moderation.ModerationService;
1213
import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository;
1314
import net.javadiscord.javabot.systems.moderation.warn.model.WarnSeverity;
1415
import net.javadiscord.javabot.systems.notification.NotificationService;
16+
import net.javadiscord.javabot.util.Checks;
1517
import net.javadiscord.javabot.util.Responses;
1618

1719
import java.util.concurrent.ExecutorService;
@@ -66,13 +68,17 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
6668
Responses.replyGuildOnly(event).queue();
6769
return;
6870
}
71+
if(!Checks.hasStaffRole(botConfig, event.getMember())) {
72+
Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue();
73+
return;
74+
}
6975
User target = userMapping.getAsUser();
7076
WarnSeverity severity = WarnSeverity.valueOf(severityMapping.getAsString().trim().toUpperCase());
7177
if (target.isBot()) {
7278
Responses.warning(event, "You cannot warn bots.").queue();
7379
return;
7480
}
75-
boolean quiet = event.getOption("quiet", false, OptionMapping::getAsBoolean);
81+
boolean quiet = ModerateUserCommand.isQuiet(botConfig, event);
7682
ModerationService service = new ModerationService(notificationService, botConfig, event, warnRepository, asyncPool);
7783
service.warn(target, severity, reasonMapping.getAsString(), event.getMember(), event.getChannel(), quiet);
7884
Responses.success(event, "User Warned", "%s has been successfully warned.", target.getAsMention()).queue();

src/main/java/net/javadiscord/javabot/systems/moderation/warn/WarnExportSubcommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository;
1616
import net.javadiscord.javabot.systems.moderation.warn.model.Warn;
1717
import net.javadiscord.javabot.systems.notification.NotificationService;
18+
import net.javadiscord.javabot.util.Checks;
1819
import net.javadiscord.javabot.util.ExceptionLogger;
1920
import net.javadiscord.javabot.util.Responses;
2021

@@ -71,6 +72,10 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
7172
Responses.replyGuildOnly(event).queue();
7273
return;
7374
}
75+
if(!Checks.hasStaffRole(botConfig, event.getMember())) {
76+
Responses.replyStaffOnly(event, botConfig.get(event.getGuild())).queue();
77+
return;
78+
}
7479
User target = userMapping.getAsUser();
7580
ModerationService service = new ModerationService(notificationService, botConfig, event, warnRepository, asyncPool);
7681
List<Warn> warns = service.getAllWarns(target.getIdLong());

0 commit comments

Comments
 (0)