44import com .mongodb .client .FindIterable ;
55import com .mongodb .client .MongoDatabase ;
66import com .mongodb .client .model .Filters ;
7- import net .javadiscord .javabot2 .Bot ;
87import net .javadiscord .javabot2 .config .guild .ModerationConfig ;
98import net .javadiscord .javabot2 .systems .moderation .model .WarnSeverity ;
109import org .bson .Document ;
1716import java .time .Instant ;
1817import java .time .OffsetDateTime ;
1918import java .util .Map ;
19+ import java .util .concurrent .CompletableFuture ;
2020
2121/**
2222 * This service provides methods for performing moderation actions, like banning
@@ -48,8 +48,9 @@ public ModerationService(DiscordApi api, MongoDatabase db, ModerationConfig conf
4848 * @param reason The reason for this warning.
4949 * @param warnedBy The user who issued the warning.
5050 * @param channel The channel in which the warning was issued.
51+ * @return A future that completes when all warn operations are complete.
5152 */
52- public void warn (User user , WarnSeverity severity , String reason , User warnedBy , ServerTextChannel channel ) {
53+ public CompletableFuture < Void > warn (User user , WarnSeverity severity , String reason , User warnedBy , ServerTextChannel channel ) {
5354 var warns = db .getCollection ("warn" );
5455 Instant now = Instant .now ();
5556 warns .insertOne (new Document (Map .of (
@@ -64,13 +65,12 @@ public void warn(User user, WarnSeverity severity, String reason, User warnedBy,
6465 totalSeverity += WarnSeverity .getWeightOrDefault (warnDoc .getString ("severity" ));
6566 }
6667 var warnEmbed = buildWarnEmbed (user , severity , reason , warnedBy , now , totalSeverity );
67- Bot .asyncPool .submit (() -> {
68- user .openPrivateChannel ().thenAcceptAsync (privateChannel -> privateChannel .sendMessage (warnEmbed ));
69- channel .sendMessage (warnEmbed );
70- });
68+ var future = channel .sendMessage (warnEmbed )
69+ .thenComposeAsync (unused -> user .openPrivateChannel ().thenAcceptAsync (privateChannel -> privateChannel .sendMessage (warnEmbed )));
7170 if (totalSeverity > config .getMaxWarnSeverity ()) {
72- ban (user , "Too many warnings." , warnedBy , channel );
71+ return future . thenComposeAsync ( unused -> ban (user , "Too many warnings." , warnedBy , channel ) );
7372 }
73+ return future ;
7474 }
7575
7676 /**
@@ -79,15 +79,19 @@ public void warn(User user, WarnSeverity severity, String reason, User warnedBy,
7979 * @param reason The reason for banning the user.
8080 * @param bannedBy The user who is responsible for banning this user.
8181 * @param channel The channel in which the ban was issued.
82+ * @return A future that completes once all ban operations are done.
8283 */
83- public void ban (User user , String reason , User bannedBy , ServerTextChannel channel ) {
84+ public CompletableFuture < Void > ban (User user , String reason , User bannedBy , ServerTextChannel channel ) {
8485 var banEmbed = buildBanEmbed (user , reason , bannedBy );
85- Bot . asyncPool . submit (() -> {
86- channel .sendMessage ( banEmbed );
87- user .openPrivateChannel ()
86+ if ( channel . getServer (). canBanUser ( bannedBy , user )) {
87+ return channel .getServer (). banUser ( user , BAN_DELETE_DAYS , reason )
88+ . thenComposeAsync ( unused -> user .openPrivateChannel () )
8889 .thenComposeAsync (privateChannel -> privateChannel .sendMessage (banEmbed ))
89- .thenRunAsync (() -> channel .getServer ().banUser (user , BAN_DELETE_DAYS , reason ));
90- });
90+ .thenComposeAsync (unused -> channel .sendMessage (banEmbed ))
91+ .thenApply (msg -> null );
92+ } else {
93+ return CompletableFuture .failedFuture (new PermissionException ("You don't have permission to ban this user." ));
94+ }
9195 }
9296
9397 private FindIterable <Document > findActiveWarns (User user ) {
0 commit comments