forked from EssentialsX/Essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Discord module webhook closing incorrectly
- Loading branch information
Showing
5 changed files
with
71 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
54 changes: 54 additions & 0 deletions
54
EssentialsDiscord/src/main/java/net/essentialsx/discord/util/WrappedWebhookClient.java
This file contains 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,54 @@ | ||
package net.essentialsx.discord.util; | ||
|
||
import club.minnced.discord.webhook.WebhookClient; | ||
import club.minnced.discord.webhook.WebhookClientBuilder; | ||
import club.minnced.discord.webhook.receive.ReadonlyMessage; | ||
import club.minnced.discord.webhook.send.AllowedMentions; | ||
import club.minnced.discord.webhook.send.WebhookMessage; | ||
import club.minnced.discord.webhook.util.ThreadPools; | ||
import net.essentialsx.discord.EssentialsDiscord; | ||
import okhttp3.OkHttpClient; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class WrappedWebhookClient { | ||
private final static Logger logger = EssentialsDiscord.getWrappedLogger(); | ||
private final WebhookClient webhookClient; | ||
private final ScheduledExecutorService executorService; | ||
|
||
public WrappedWebhookClient(final long id, final String token, final OkHttpClient client) { | ||
webhookClient = new WebhookClientBuilder(id, token) | ||
.setWait(false) | ||
.setAllowedMentions(AllowedMentions.none()) | ||
.setHttpClient(client) | ||
.setExecutorService(executorService = ThreadPools.getDefaultPool(id, null, true)) | ||
.build(); | ||
} | ||
|
||
public CompletableFuture<ReadonlyMessage> send(WebhookMessage message) { | ||
return webhookClient.send(message); | ||
} | ||
|
||
public boolean isShutdown() { | ||
return webhookClient.isShutdown(); | ||
} | ||
|
||
public void close() { | ||
// This call should close the executor service as well | ||
webhookClient.close(); | ||
if (executorService.isTerminated()) { | ||
return; | ||
} | ||
try { | ||
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) { | ||
throw new InterruptedException("ExecutorService did not terminate in time."); | ||
} | ||
} catch (InterruptedException e) { | ||
logger.log(Level.WARNING, "Webhook (ID: " + webhookClient.getId() + ") took longer than expected to shutdown, this may have caused some problems.", e); | ||
} | ||
} | ||
} |