Search in sources :

Example 6 with DiscordApiImpl

use of org.javacord.core.DiscordApiImpl in project Javacord by BtoBastian.

the class SlashCommandUpdaterDelegateImpl method updateGlobal.

@Override
public CompletableFuture<SlashCommand> updateGlobal(DiscordApi api) {
    ObjectNode body = JsonNodeFactory.instance.objectNode();
    prepareBody(body);
    return new RestRequest<SlashCommand>(api, RestMethod.PATCH, RestEndpoint.APPLICATION_COMMANDS).setUrlParameters(String.valueOf(api.getClientId()), String.valueOf(commandId)).setBody(body).execute(result -> new SlashCommandImpl((DiscordApiImpl) api, result.getJsonBody()));
}
Also used : SlashCommand(org.javacord.api.interaction.SlashCommand) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DiscordApiImpl(org.javacord.core.DiscordApiImpl)

Example 7 with DiscordApiImpl

use of org.javacord.core.DiscordApiImpl in project Javacord by BtoBastian.

the class UserContextMenuUpdaterDelegateImpl method updateGlobal.

@Override
public CompletableFuture<UserContextMenu> updateGlobal(DiscordApi api) {
    ObjectNode body = JsonNodeFactory.instance.objectNode();
    prepareBody(body);
    return new RestRequest<UserContextMenu>(api, RestMethod.PATCH, RestEndpoint.APPLICATION_COMMANDS).setUrlParameters(String.valueOf(api.getClientId()), String.valueOf(commandId)).setBody(body).execute(result -> new UserContextMenuImpl((DiscordApiImpl) api, result.getJsonBody()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DiscordApiImpl(org.javacord.core.DiscordApiImpl) UserContextMenu(org.javacord.api.interaction.UserContextMenu)

Example 8 with DiscordApiImpl

use of org.javacord.core.DiscordApiImpl in project Javacord by BtoBastian.

the class UserContextMenuUpdaterDelegateImpl method updateForServer.

@Override
public CompletableFuture<UserContextMenu> updateForServer(Server server) {
    ObjectNode body = JsonNodeFactory.instance.objectNode();
    prepareBody(body);
    return new RestRequest<UserContextMenu>(server.getApi(), RestMethod.PATCH, RestEndpoint.SERVER_APPLICATION_COMMANDS).setUrlParameters(String.valueOf(server.getApi().getClientId()), server.getIdAsString(), String.valueOf(commandId)).setBody(body).execute(result -> new UserContextMenuImpl((DiscordApiImpl) server.getApi(), result.getJsonBody()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DiscordApiImpl(org.javacord.core.DiscordApiImpl) UserContextMenu(org.javacord.api.interaction.UserContextMenu)

Example 9 with DiscordApiImpl

use of org.javacord.core.DiscordApiImpl in project Javacord by BtoBastian.

the class AudioUdpSocket method startSending.

/**
 * Starts polling frames from the audio connection and sending them through the socket.
 */
public void startSending() {
    if (shouldSend) {
        return;
    }
    shouldSend = true;
    DiscordApiImpl api = (DiscordApiImpl) connection.getChannel().getApi();
    api.getThreadPool().getSingleThreadExecutorService(threadName).submit(() -> {
        try {
            long nextFrameTimestamp = System.nanoTime();
            boolean dontSleep = true;
            boolean speaking = false;
            long framesOfSilenceToPlay = 5;
            while (shouldSend) {
                // Get the current audio source. If none is available, it will block the thread
                AudioSource source = connection.getCurrentAudioSourceBlocking();
                if (source == null) {
                    logger.error("Got null audio source without being interrupted ({})", connection);
                    return;
                }
                if (source.hasFinished()) {
                    connection.removeAudioSource();
                    dontSleep = true;
                    // Dispatch AudioSourceFinishedEvent AFTER removing the source.
                    // Otherwise, AudioSourceFinishedEvent#getNextSource() won't work
                    api.getEventDispatcher().dispatchAudioSourceFinishedEvent((ServerImpl) connection.getServer(), connection, ((AudioSourceBase) source).getDelegate(), new AudioSourceFinishedEventImpl(source, connection));
                    continue;
                }
                AudioPacket packet = null;
                byte[] frame = source.hasNextFrame() ? source.getNextFrame() : null;
                // If the source is muted, replace the frame with a muted frame
                if (source.isMuted()) {
                    frame = null;
                }
                if (frame != null || framesOfSilenceToPlay > 0) {
                    if (!speaking && frame != null) {
                        speaking = true;
                        connection.setSpeaking(true);
                    }
                    packet = new AudioPacket(frame, ssrc, sequence, ((int) sequence) * 960);
                    // We can stop sending frames of silence after 5 frames
                    if (frame == null) {
                        framesOfSilenceToPlay--;
                        if (framesOfSilenceToPlay == 0) {
                            speaking = false;
                            connection.setSpeaking(false);
                        }
                    } else {
                        framesOfSilenceToPlay = 5;
                    }
                }
                nextFrameTimestamp = nextFrameTimestamp + 20_000_000;
                sequence++;
                if (packet != null) {
                    packet.encrypt(secretKey);
                }
                try {
                    if (dontSleep) {
                        nextFrameTimestamp = System.nanoTime() + 20_000_000;
                        dontSleep = false;
                    } else {
                        Thread.sleep(Math.max(0, nextFrameTimestamp - System.nanoTime()) / 1_000_000);
                    }
                    if (packet != null) {
                        socket.send(packet.asUdpPacket(address));
                    }
                } catch (IOException e) {
                    logger.error("Failed to send audio packet for {}", connection);
                }
            }
        } catch (InterruptedException e) {
            if (shouldSend) {
                logger.debug("Got interrupted unexpectedly while waiting for next audio source packet");
            }
        }
    });
}
Also used : AudioSource(org.javacord.api.audio.AudioSource) DiscordApiImpl(org.javacord.core.DiscordApiImpl) AudioSourceFinishedEventImpl(org.javacord.core.event.audio.AudioSourceFinishedEventImpl) IOException(java.io.IOException)

Example 10 with DiscordApiImpl

use of org.javacord.core.DiscordApiImpl in project Javacord by BtoBastian.

the class ApplicationCommandPermissionsUpdaterDelegateImpl method update.

@Override
public CompletableFuture<ServerApplicationCommandPermissions> update(long commandId) {
    ObjectNode body = JsonNodeFactory.instance.objectNode();
    ArrayNode array = body.putArray("permissions");
    for (ApplicationCommandPermissions permission : permissions) {
        array.add(((ApplicationCommandPermissionsImpl) permission).toJsonNode());
    }
    return new RestRequest<ServerApplicationCommandPermissions>(server.getApi(), RestMethod.PUT, RestEndpoint.APPLICATION_COMMAND_PERMISSIONS).setUrlParameters(String.valueOf(server.getApi().getClientId()), server.getIdAsString(), String.valueOf(commandId)).setBody(body).execute(result -> new ServerApplicationCommandPermissionsImpl((DiscordApiImpl) server.getApi(), result.getJsonBody()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DiscordApiImpl(org.javacord.core.DiscordApiImpl) ApplicationCommandPermissions(org.javacord.api.interaction.ApplicationCommandPermissions) ServerApplicationCommandPermissions(org.javacord.api.interaction.ServerApplicationCommandPermissions) ServerApplicationCommandPermissions(org.javacord.api.interaction.ServerApplicationCommandPermissions) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

DiscordApiImpl (org.javacord.core.DiscordApiImpl)13 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ArrayList (java.util.ArrayList)4 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)3 BufferedImage (java.awt.image.BufferedImage)3 File (java.io.File)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Icon (org.javacord.api.entity.Icon)3 FileContainer (org.javacord.core.util.FileContainer)3 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)3 RestMethod (org.javacord.core.util.rest.RestMethod)3 RestRequest (org.javacord.core.util.rest.RestRequest)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Arrays (java.util.Arrays)2