use of net.dv8tion.jda.api.entities.Webhook in project Essentials by EssentialsX.
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 Essentials by drtshock.
the class JDADiscordService method updateConsoleRelay.
public void updateConsoleRelay() {
final String consoleDef = getSettings().getConsoleChannelDef();
final Matcher matcher = WebhookClientBuilder.WEBHOOK_PATTERN.matcher(consoleDef);
final long webhookId;
final String webhookToken;
if (matcher.matches()) {
webhookId = Long.parseUnsignedLong(matcher.group(1));
webhookToken = matcher.group(2);
if (commandDispatcher != null) {
jda.removeEventListener(commandDispatcher);
commandDispatcher = null;
}
} else {
final TextChannel channel = getChannel(consoleDef, false);
if (channel != null) {
if (getSettings().isConsoleCommandRelay()) {
if (commandDispatcher == null) {
commandDispatcher = new DiscordCommandDispatcher(this);
jda.addEventListener(commandDispatcher);
}
commandDispatcher.setChannelId(channel.getId());
} else if (commandDispatcher != null) {
jda.removeEventListener(commandDispatcher);
commandDispatcher = null;
}
if (channel.getId().equals(lastConsoleId)) {
return;
}
final Webhook webhook = DiscordUtil.getOrCreateWebhook(channel, DiscordUtil.CONSOLE_RELAY_NAME).join();
if (webhook == null) {
logger.info(tl("discordErrorLoggerNoPerms"));
return;
}
webhookId = webhook.getIdLong();
webhookToken = webhook.getToken();
lastConsoleId = channel.getId();
} else if (!getSettings().getConsoleChannelDef().equals("none") && !getSettings().getConsoleChannelDef().startsWith("0")) {
logger.info(tl("discordErrorLoggerInvalidChannel"));
shutdownConsoleRelay(true);
return;
} else {
// It's either not configured at all or knowingly disabled.
shutdownConsoleRelay(true);
return;
}
}
shutdownConsoleRelay(false);
consoleWebhook = DiscordUtil.getWebhookClient(webhookId, webhookToken, jda.getHttpClient());
if (injector == null) {
injector = new ConsoleInjector(this);
injector.start();
}
}
use of net.dv8tion.jda.api.entities.Webhook in project JDA by DV8FromTheWorld.
the class BaseGuildMessageChannelMixin method retrieveWebhooks.
@Nonnull
@Override
default RestAction<List<Webhook>> retrieveWebhooks() {
checkPermission(Permission.MANAGE_WEBHOOKS);
Route.CompiledRoute route = Route.Channels.GET_WEBHOOKS.compile(getId());
JDAImpl jda = (JDAImpl) getJDA();
return new RestActionImpl<>(jda, route, (response, request) -> {
DataArray array = response.getArray();
List<Webhook> webhooks = new ArrayList<>(array.length());
EntityBuilder builder = jda.getEntityBuilder();
for (int i = 0; i < array.length(); i++) {
try {
webhooks.add(builder.createWebhook(array.getObject(i)));
} catch (UncheckedIOException | NullPointerException e) {
JDAImpl.LOG.error("Error while creating websocket from json", e);
}
}
return Collections.unmodifiableList(webhooks);
});
}
use of net.dv8tion.jda.api.entities.Webhook in project Essentials by EssentialsX.
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 EssentialsX.
the class JDADiscordService method updateConsoleRelay.
public void updateConsoleRelay() {
final String consoleDef = getSettings().getConsoleChannelDef();
final Matcher matcher = WebhookClientBuilder.WEBHOOK_PATTERN.matcher(consoleDef);
final long webhookId;
final String webhookToken;
if (matcher.matches()) {
webhookId = Long.parseUnsignedLong(matcher.group(1));
webhookToken = matcher.group(2);
if (commandDispatcher != null) {
jda.removeEventListener(commandDispatcher);
commandDispatcher = null;
}
} else {
final TextChannel channel = getChannel(consoleDef, false);
if (channel != null) {
if (getSettings().isConsoleCommandRelay()) {
if (commandDispatcher == null) {
commandDispatcher = new DiscordCommandDispatcher(this);
jda.addEventListener(commandDispatcher);
}
commandDispatcher.setChannelId(channel.getId());
} else if (commandDispatcher != null) {
jda.removeEventListener(commandDispatcher);
commandDispatcher = null;
}
if (channel.getId().equals(lastConsoleId)) {
return;
}
final Webhook webhook = DiscordUtil.getOrCreateWebhook(channel, DiscordUtil.CONSOLE_RELAY_NAME).join();
if (webhook == null) {
logger.info(tl("discordErrorLoggerNoPerms"));
return;
}
webhookId = webhook.getIdLong();
webhookToken = webhook.getToken();
lastConsoleId = channel.getId();
} else if (!getSettings().getConsoleChannelDef().equals("none") && !getSettings().getConsoleChannelDef().startsWith("0")) {
logger.info(tl("discordErrorLoggerInvalidChannel"));
shutdownConsoleRelay(true);
return;
} else {
// It's either not configured at all or knowingly disabled.
shutdownConsoleRelay(true);
return;
}
}
shutdownConsoleRelay(false);
consoleWebhook = DiscordUtil.getWebhookClient(webhookId, webhookToken, jda.getHttpClient());
if (injector == null) {
injector = new ConsoleInjector(this);
injector.start();
}
}
Aggregations