3737
3838import java .util .List ;
3939import java .util .Objects ;
40+ import java .util .concurrent .CompletableFuture ;
4041import java .util .concurrent .CopyOnWriteArrayList ;
42+ import java .util .concurrent .ExecutionException ;
4143import java .util .concurrent .Executors ;
4244import java .util .concurrent .ScheduledExecutorService ;
4345import java .util .function .Consumer ;
@@ -50,7 +52,7 @@ public class WebhookManagerImpl implements WebhookManager {
5052
5153 static {
5254 Runtime .getRuntime ().addShutdownHook (new Thread (() ->
53- MANAGERS .forEach (WebhookManagerImpl ::close ), "WebhookClosure " ));
55+ MANAGERS .forEach (WebhookManagerImpl ::close ), "WebhookClosing " ));
5456 }
5557
5658 private static ScheduledExecutorService getExecutor () {
@@ -67,7 +69,7 @@ private static ScheduledExecutorService getExecutor() {
6769 @ Nullable
6870 private final Consumer <Webhook > creationListener ;
6971
70- public WebhookManagerImpl (final Predicate <String > predicate , final String webhookName , final AllowedMentions allowedMentions , final Consumer <Webhook > creationListener ) {
72+ public WebhookManagerImpl (final Predicate <String > predicate , final String webhookName , final AllowedMentions allowedMentions , @ javax . annotation . Nullable final Consumer <Webhook > creationListener ) {
7173 this .predicate = predicate ;
7274 this .webhookName = webhookName ;
7375 this .allowedMentions = allowedMentions ;
@@ -97,20 +99,28 @@ public void sendAndCrosspost(final IWebhookContainer channel, final WebhookMessa
9799 }
98100
99101 private Webhook getOrCreateWebhook (IWebhookContainer channel ) {
100- final var alreadyExisted = Objects .requireNonNull (channel ).retrieveWebhooks ()
101- .complete ( )
102+ final var alreadyExisted = unwrap ( Objects .requireNonNull (channel ).retrieveWebhooks ()
103+ .submit ( false ) )
102104 .stream ()
103105 .filter (w -> predicate .test (w .getName ()))
104106 .findAny ();
105107 return alreadyExisted .orElseGet (() -> {
106- final var webhook = channel .createWebhook (webhookName ).complete ( );
108+ final var webhook = unwrap ( channel .createWebhook (webhookName ).submit ( false ) );
107109 if (creationListener != null ) {
108110 creationListener .accept (webhook );
109111 }
110112 return webhook ;
111113 });
112114 }
113115
116+ private static <T > T unwrap (CompletableFuture <T > completableFuture ) {
117+ try {
118+ return completableFuture .get ();
119+ } catch (InterruptedException | ExecutionException e ) {
120+ throw new RuntimeException (e );
121+ }
122+ }
123+
114124 public void close () {
115125 webhooks .forEach ((id , client ) -> client .close ());
116126 }
0 commit comments