use of sx.blah.discord.api.internal.DiscordClientImpl 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);
}
});
}
use of sx.blah.discord.api.internal.DiscordClientImpl in project Discord4J by Discord4J.
the class EmojiImpl method changeName.
@Override
public void changeName(String name) {
PermissionUtils.requirePermissions(getGuild(), getClient().getOurUser(), Permissions.MANAGE_EMOJIS);
EmojiObject response = ((DiscordClientImpl) getClient()).REQUESTS.PATCH.makeRequest(DiscordEndpoints.GUILDS + getGuild().getStringID() + "/emojis/" + getStringID(), new EmojiEditRequest(name, getRoles().toArray(new IRole[getRoles().size()])), EmojiObject.class);
IEmoji emoji = DiscordUtils.getEmojiFromJSON(getGuild(), response);
}
use of sx.blah.discord.api.internal.DiscordClientImpl in project Discord4J by Discord4J.
the class EmojiImpl method changeRoles.
@Override
public void changeRoles(IRole[] roles) {
PermissionUtils.requirePermissions(getGuild(), getClient().getOurUser(), Permissions.MANAGE_EMOJIS);
EmojiObject response = ((DiscordClientImpl) getClient()).REQUESTS.PATCH.makeRequest(DiscordEndpoints.GUILDS + getGuild().getStringID() + "/emojis/" + getStringID(), new EmojiEditRequest(getName(), roles), EmojiObject.class);
IEmoji emoji = DiscordUtils.getEmojiFromJSON(getGuild(), response);
}
use of sx.blah.discord.api.internal.DiscordClientImpl in project Discord4J by Discord4J.
the class Guild method getAuditLog.
private AuditLog getAuditLog(IUser user, ActionType actionType, long before) {
PermissionUtils.requirePermissions(this, client.getOurUser(), Permissions.VIEW_AUDIT_LOG);
List<AuditLog> retrieved = new ArrayList<>();
AuditLogEntryObject[] chunk;
do {
String query = "?limit=100&before=" + before;
if (user != null)
query += "&user_id=" + Long.toUnsignedString(user.getLongID());
if (actionType != null)
query += "&action_type=" + actionType.getRaw();
AuditLogObject auditLog = ((DiscordClientImpl) client).REQUESTS.GET.makeRequest(DiscordEndpoints.GUILDS + getStringID() + "/audit-logs" + query, AuditLogObject.class);
chunk = auditLog.audit_log_entries;
if (chunk.length == 0)
break;
retrieved.add(DiscordUtils.getAuditLogFromJSON(this, auditLog));
before = Long.parseLong(auditLog.audit_log_entries[auditLog.audit_log_entries.length - 1].id);
} while (chunk.length == 100);
return new AuditLog(retrieved.stream().map(AuditLog::getEntries).flatMap(Collection::stream).collect(LongMapCollector.toLongMap()));
}
use of sx.blah.discord.api.internal.DiscordClientImpl in project Discord4J by Discord4J.
the class ClientBuilder method build.
/**
* Creates a {@link IDiscordClient} with the configuration specified by this builder.
*
* @return The new client with the configuration specified by this builder.
*/
public IDiscordClient build() {
if (botToken == null)
throw new DiscordException("No login info present!");
if (withRecommendedShardCount && shard != null)
throw new DiscordException("Cannot use recommend shard count options with a specific shard!");
if (withRecommendedShardCount) {
GatewayBotResponse response = Requests.GENERAL_REQUESTS.GET.makeRequest(DiscordEndpoints.GATEWAY + "/bot", GatewayBotResponse.class, new BasicNameValuePair("Authorization", "Bot " + botToken), new BasicNameValuePair("Content-Type", "application/json"));
shardCount = response.shards;
}
final IDiscordClient client = new DiscordClientImpl(botToken, shard != null ? -1 : shardCount, isDaemon, maxMissedPings, maxReconnectAttempts, retryCount, maxCacheCount, provider, shard, backpressureHandler, minimumPoolSize, maximumPoolSize, overflowCapacity, eventThreadTimeout, eventThreadTimeoutUnit, new PresenceUpdateRequest(status, activity, text, streamUrl));
// Registers events as soon as client is initialized
final EventDispatcher dispatcher = client.getDispatcher();
iListeners.forEach(dispatcher::registerListener);
listeners.forEach(dispatcher::registerListener);
listenerClasses.forEach(dispatcher::registerListener);
return client;
}
Aggregations