Search in sources :

Example 1 with WebhookCreateEvent

use of sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookCreateEvent in project Discord4J by Discord4J.

the class Guild method loadWebhooks.

/**
 * Forcibly loads and caches all webhooks for the channel.
 */
public void loadWebhooks() {
    try {
        PermissionUtils.requirePermissions(this, client.getOurUser(), Permissions.MANAGE_WEBHOOKS);
    } catch (MissingPermissionsException ignored) {
        return;
    }
    RequestBuffer.request(() -> {
        try {
            List<IWebhook> oldList = getWebhooks().stream().map(IWebhook::copy).collect(Collectors.toCollection(CopyOnWriteArrayList::new));
            WebhookObject[] response = ((DiscordClientImpl) client).REQUESTS.GET.makeRequest(DiscordEndpoints.GUILDS + getStringID() + "/webhooks", WebhookObject[].class);
            if (response != null) {
                for (WebhookObject webhookObject : response) {
                    Channel channel = (Channel) getChannelByID(Long.parseUnsignedLong(webhookObject.channel_id));
                    long webhookId = Long.parseUnsignedLong(webhookObject.id);
                    if (getWebhookByID(webhookId) == null) {
                        IWebhook newWebhook = DiscordUtils.getWebhookFromJSON(channel, webhookObject);
                        client.getDispatcher().dispatch(new WebhookCreateEvent(newWebhook));
                        channel.webhooks.put(newWebhook);
                    } else {
                        IWebhook toUpdate = channel.getWebhookByID(webhookId);
                        IWebhook oldWebhook = toUpdate.copy();
                        toUpdate = DiscordUtils.getWebhookFromJSON(channel, webhookObject);
                        if (!oldWebhook.getDefaultName().equals(toUpdate.getDefaultName()) || !String.valueOf(oldWebhook.getDefaultAvatar()).equals(String.valueOf(toUpdate.getDefaultAvatar())))
                            client.getDispatcher().dispatch(new WebhookUpdateEvent(oldWebhook, toUpdate));
                        oldList.remove(oldWebhook);
                    }
                }
            }
            oldList.forEach(webhook -> {
                ((Channel) webhook.getChannel()).webhooks.remove(webhook);
                client.getDispatcher().dispatch(new WebhookDeleteEvent(webhook));
            });
        } catch (Exception e) {
            Discord4J.LOGGER.warn(LogMarkers.HANDLE, "Discord4J Internal Exception", e);
        }
    });
}
Also used : WebhookUpdateEvent(sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookUpdateEvent) WebhookDeleteEvent(sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookDeleteEvent) WebhookCreateEvent(sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookCreateEvent) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with WebhookCreateEvent

use of sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookCreateEvent in project Discord4J by Discord4J.

the class Channel method loadWebhooks.

/**
 * Forcibly loads and caches all webhooks for the channel.
 */
public void loadWebhooks() {
    try {
        PermissionUtils.requirePermissions(this, client.getOurUser(), Permissions.MANAGE_WEBHOOKS);
    } catch (MissingPermissionsException ignored) {
        return;
    }
    RequestBuffer.request(() -> {
        try {
            List<IWebhook> oldList = getWebhooks().stream().map(IWebhook::copy).collect(Collectors.toCollection(CopyOnWriteArrayList::new));
            WebhookObject[] response = ((DiscordClientImpl) client).REQUESTS.GET.makeRequest(DiscordEndpoints.CHANNELS + getStringID() + "/webhooks", WebhookObject[].class);
            if (response != null) {
                for (WebhookObject webhookObject : response) {
                    long webhookId = Long.parseUnsignedLong(webhookObject.id);
                    if (getWebhookByID(webhookId) == null) {
                        IWebhook newWebhook = DiscordUtils.getWebhookFromJSON(this, webhookObject);
                        client.getDispatcher().dispatch(new WebhookCreateEvent(newWebhook));
                        webhooks.put(newWebhook);
                    } else {
                        IWebhook toUpdate = getWebhookByID(webhookId);
                        IWebhook oldWebhook = toUpdate.copy();
                        toUpdate = DiscordUtils.getWebhookFromJSON(this, webhookObject);
                        if (!oldWebhook.getDefaultName().equals(toUpdate.getDefaultName()) || !String.valueOf(oldWebhook.getDefaultAvatar()).equals(String.valueOf(toUpdate.getDefaultAvatar())))
                            client.getDispatcher().dispatch(new WebhookUpdateEvent(oldWebhook, toUpdate));
                        oldList.remove(oldWebhook);
                    }
                }
            }
            oldList.forEach(webhook -> {
                webhooks.remove(webhook);
                client.getDispatcher().dispatch(new WebhookDeleteEvent(webhook));
            });
        } catch (Exception e) {
            Discord4J.LOGGER.warn(LogMarkers.HANDLE, "Discord4J Internal Exception", e);
        }
    });
}
Also used : WebhookUpdateEvent(sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookUpdateEvent) WebhookDeleteEvent(sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookDeleteEvent) WebhookCreateEvent(sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookCreateEvent) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 DiscordClientImpl (sx.blah.discord.api.internal.DiscordClientImpl)2 WebhookCreateEvent (sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookCreateEvent)2 WebhookDeleteEvent (sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookDeleteEvent)2 WebhookUpdateEvent (sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookUpdateEvent)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1