Search in sources :

Example 6 with DiscordClientImpl

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

Example 7 with DiscordClientImpl

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);
}
Also used : EmojiObject(sx.blah.discord.api.internal.json.objects.EmojiObject) EmojiEditRequest(sx.blah.discord.api.internal.json.requests.EmojiEditRequest) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl) IEmoji(sx.blah.discord.handle.obj.IEmoji)

Example 8 with DiscordClientImpl

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);
}
Also used : EmojiObject(sx.blah.discord.api.internal.json.objects.EmojiObject) EmojiEditRequest(sx.blah.discord.api.internal.json.requests.EmojiEditRequest) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl) IEmoji(sx.blah.discord.handle.obj.IEmoji)

Example 9 with DiscordClientImpl

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()));
}
Also used : AuditLogEntryObject(sx.blah.discord.api.internal.json.objects.audit.AuditLogEntryObject) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl) AuditLog(sx.blah.discord.handle.audit.AuditLog) AuditLogObject(sx.blah.discord.api.internal.json.objects.audit.AuditLogObject)

Example 10 with DiscordClientImpl

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;
}
Also used : EventDispatcher(sx.blah.discord.api.events.EventDispatcher) PresenceUpdateRequest(sx.blah.discord.api.internal.json.requests.PresenceUpdateRequest) GatewayBotResponse(sx.blah.discord.api.internal.json.responses.GatewayBotResponse) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DiscordException(sx.blah.discord.util.DiscordException) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl)

Aggregations

DiscordClientImpl (sx.blah.discord.api.internal.DiscordClientImpl)11 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 WebhookUpdateEvent (sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookUpdateEvent)3 EmojiObject (sx.blah.discord.api.internal.json.objects.EmojiObject)2 EmojiEditRequest (sx.blah.discord.api.internal.json.requests.EmojiEditRequest)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 IEmoji (sx.blah.discord.handle.obj.IEmoji)2 DiscordException (sx.blah.discord.util.DiscordException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 EventDispatcher (sx.blah.discord.api.events.EventDispatcher)1 OverwriteObject (sx.blah.discord.api.internal.json.objects.OverwriteObject)1 UserObject (sx.blah.discord.api.internal.json.objects.UserObject)1 WebhookObject (sx.blah.discord.api.internal.json.objects.WebhookObject)1 AuditLogEntryObject (sx.blah.discord.api.internal.json.objects.audit.AuditLogEntryObject)1 AuditLogObject (sx.blah.discord.api.internal.json.objects.audit.AuditLogObject)1 MessageRequest (sx.blah.discord.api.internal.json.requests.MessageRequest)1