use of net.dv8tion.jda.api.entities.Webhook in project Essentials by drtshock.
the class DiscordUtil method getOrCreateWebhook.
/**
* Gets or creates a webhook with the given name in the given channel.
*
* @param channel The channel to search for/create webhooks in.
* @param webhookName The name of the webhook to search for/create.
*
* @return A future which completes with the webhook by the given name in the given channel, or null
* if the bot lacks the proper permissions.
*/
public static CompletableFuture<Webhook> getOrCreateWebhook(final TextChannel channel, final String webhookName) {
if (!channel.getGuild().getSelfMember().hasPermission(channel, Permission.MANAGE_WEBHOOKS)) {
return CompletableFuture.completedFuture(null);
}
final CompletableFuture<Webhook> future = new CompletableFuture<>();
channel.retrieveWebhooks().queue(webhooks -> {
for (final Webhook webhook : webhooks) {
if (webhook.getName().equals(webhookName) && webhook.getToken() != null) {
ACTIVE_WEBHOOKS.addIfAbsent(webhook.getId());
future.complete(webhook);
return;
}
}
createWebhook(channel, webhookName).thenAccept(future::complete);
});
return future;
}
use of net.dv8tion.jda.api.entities.Webhook in project Essentials by drtshock.
the class JDADiscordService method updateTypesRelay.
public void updateTypesRelay() {
if (!getSettings().isShowAvatar() && !getSettings().isShowName() && !getSettings().isShowDisplayName()) {
for (WebhookClient webhook : channelIdToWebhook.values()) {
webhook.close();
}
typeToChannelId.clear();
channelIdToWebhook.clear();
return;
}
for (MessageType type : MessageType.DefaultTypes.values()) {
if (!type.isPlayer()) {
continue;
}
final TextChannel channel = getChannel(type.getKey(), true);
if (channel.getId().equals(typeToChannelId.get(type))) {
continue;
}
final Webhook webhook = DiscordUtil.getOrCreateWebhook(channel, DiscordUtil.ADVANCED_RELAY_NAME).join();
if (webhook == null) {
final WebhookClient current = channelIdToWebhook.get(channel.getId());
if (current != null) {
current.close();
}
channelIdToWebhook.remove(channel.getId());
continue;
}
typeToChannelId.put(type, channel.getId());
channelIdToWebhook.put(channel.getId(), DiscordUtil.getWebhookClient(webhook.getIdLong(), webhook.getToken(), jda.getHttpClient()));
}
}
use of net.dv8tion.jda.api.entities.Webhook in project JDA by DV8FromTheWorld.
the class WebhookActionImpl method handleSuccess.
@Override
protected void handleSuccess(Response response, Request<Webhook> request) {
DataObject json = response.getObject();
Webhook webhook = api.getEntityBuilder().createWebhook(json);
request.onSuccess(webhook);
}
use of net.dv8tion.jda.api.entities.Webhook in project c10ver by Gartham.
the class GarmonUtils method queueWithFeasibleWebhook.
public static void queueWithFeasibleWebhook(TextChannel channel, Consumer<Webhook> consumer) {
channel.retrieveWebhooks().queue(t -> {
for (Webhook wb : t) if (wb.getOwner().getId().equals(channel.getJDA().getSelfUser().getId())) {
consumer.accept(wb);
return;
}
byte[] b = new byte[5];
// 1/2^40 collision chance.
new Random().nextBytes(b);
channel.createWebhook(StringTools.toHexString(b)).queue(t1 -> consumer.accept(t1));
});
}
use of net.dv8tion.jda.api.entities.Webhook in project c10ver by Gartham.
the class GarmonUtils method getFeasibleWebhook.
/**
* Tries to provide a feasible webhook for use. This method iterates over all
* the webhooks it retrieves from the specified channel. If any one of them is
* created by the bot user (more specifically, if its owner is the bot user), it
* is returned. Otherwise, an attempt is made to create a new webhook. This
* function will throw exceptions if it does not have the appropriate
* permissions or if an error occurs during the retrieval or creation of a
* webhook.
*
* @param channel The channel that the webhook will belong to.
* @return The {@link Webhook} that was found or newly created.
*/
public static Webhook getFeasibleWebhook(TextChannel channel) {
for (Webhook wb : channel.retrieveWebhooks().complete()) if (wb.getOwner().getId().equals(channel.getJDA().getSelfUser().getId()))
return wb;
byte[] b = new byte[5];
// 1/2^40 collision chance.
new Random().nextBytes(b);
return channel.createWebhook(StringTools.toHexString(b)).complete();
}
Aggregations