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);
}
});
}
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);
}
});
}
Aggregations