Search in sources :

Example 1 with DiscordClientImpl

use of sx.blah.discord.api.internal.DiscordClientImpl in project Discord4J by Discord4J.

the class Reaction method getUsers.

@Override
public List<IUser> getUsers() {
    List<IUser> users = new ArrayList<>();
    String emoji = getEmoji().isUnicode() ? getEmoji().getName() : getEmoji().getName() + ":" + getEmoji().getStringID();
    String endpoint = String.format(DiscordEndpoints.REACTIONS_USER_LIST, getMessage().getChannel().getStringID(), getMessage().getStringID(), emoji);
    String after = "0";
    while (users.size() < count) {
        UserObject[] json = ((DiscordClientImpl) getClient()).REQUESTS.GET.makeRequest(endpoint + "?after=" + after + "&limit=100", UserObject[].class);
        for (UserObject obj : json) {
            users.add(getMessage().getShard().getUserByID(Long.parseUnsignedLong(obj.id)));
        }
        // Temporary measure so a refactor can be applied later.
        if (json.length == 0)
            break;
        after = json[json.length - 1].id;
    }
    return users;
}
Also used : ArrayList(java.util.ArrayList) IUser(sx.blah.discord.handle.obj.IUser) UserObject(sx.blah.discord.api.internal.json.objects.UserObject) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl)

Example 2 with DiscordClientImpl

use of sx.blah.discord.api.internal.DiscordClientImpl in project Discord4J by Discord4J.

the class NickHolder method moveToVoiceChannel.

@Override
public void moveToVoiceChannel(IVoiceChannel channel) {
    IVoiceChannel oldChannel = getVoiceStateForGuild(channel.getGuild()).getChannel();
    if (oldChannel == null)
        throw new DiscordException("User must already be in a voice channel before they can be moved to another.");
    // client must have permission to both move members and connect to the channel.
    PermissionUtils.requirePermissions(channel, client.getOurUser(), Permissions.VOICE_MOVE_MEMBERS, Permissions.VOICE_CONNECT);
    try {
        ((DiscordClientImpl) client).REQUESTS.PATCH.makeRequest(DiscordEndpoints.GUILDS + channel.getGuild().getStringID() + "/members/" + id, DiscordUtils.MAPPER_NO_NULLS.writeValueAsString(new MemberEditRequest.Builder().channel(channel.getStringID()).build()));
    } catch (JsonProcessingException e) {
        Discord4J.LOGGER.error(LogMarkers.HANDLE, "Discord4J Internal Exception", e);
    }
}
Also used : DiscordException(sx.blah.discord.util.DiscordException) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with DiscordClientImpl

use of sx.blah.discord.api.internal.DiscordClientImpl 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 4 with DiscordClientImpl

use of sx.blah.discord.api.internal.DiscordClientImpl in project Discord4J by Discord4J.

the class Webhook method edit.

/**
 * Sends a request to edit the webhook.
 *
 * @param name The default name of the webhook.
 * @param avatar The base64-encoded default avatar of the webhook.
 */
private void edit(String name, String avatar) {
    PermissionUtils.requirePermissions(channel, client.getOurUser(), Permissions.MANAGE_WEBHOOKS);
    WebhookObject response = ((DiscordClientImpl) client).REQUESTS.PATCH.makeRequest(DiscordEndpoints.WEBHOOKS + id, new WebhookEditRequest(name, avatar), WebhookObject.class);
    IWebhook oldWebhook = copy();
    IWebhook newWebhook = DiscordUtils.getWebhookFromJSON(channel, response);
    client.getDispatcher().dispatch(new WebhookUpdateEvent(oldWebhook, newWebhook));
}
Also used : WebhookUpdateEvent(sx.blah.discord.handle.impl.events.guild.channel.webhook.WebhookUpdateEvent) WebhookObject(sx.blah.discord.api.internal.json.objects.WebhookObject) DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl) WebhookEditRequest(sx.blah.discord.api.internal.json.requests.WebhookEditRequest)

Example 5 with DiscordClientImpl

use of sx.blah.discord.api.internal.DiscordClientImpl in project Discord4J by Discord4J.

the class Category method overridePermissions.

private void overridePermissions(String type, String id, EnumSet<Permissions> toAdd, EnumSet<Permissions> toRemove) {
    PermissionUtils.requirePermissions(getModifiedPermissions(getClient().getOurUser()), EnumSet.of(Permissions.MANAGE_PERMISSIONS));
    ((DiscordClientImpl) client).REQUESTS.PUT.makeRequest(DiscordEndpoints.CHANNELS + getStringID() + "/permissions/" + id, new OverwriteObject(type, null, Permissions.generatePermissionsNumber(toAdd), Permissions.generatePermissionsNumber(toRemove)));
}
Also used : DiscordClientImpl(sx.blah.discord.api.internal.DiscordClientImpl) OverwriteObject(sx.blah.discord.api.internal.json.objects.OverwriteObject)

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