Search in sources :

Example 1 with VoiceDescriptionResponse

use of sx.blah.discord.api.internal.json.responses.voice.VoiceDescriptionResponse in project Discord4J by Discord4J.

the class DiscordVoiceWS method onWebSocketText.

@Override
public void onWebSocketText(String message) {
    try {
        JsonNode json = DiscordUtils.MAPPER.readTree(message);
        VoiceOps op = VoiceOps.get(json.get("op").asInt());
        JsonNode d = json.has("d") && !json.get("d").isNull() ? json.get("d") : null;
        switch(op) {
            case READY:
                try {
                    VoiceReadyResponse ready = DiscordUtils.MAPPER.treeToValue(d, VoiceReadyResponse.class);
                    voiceSocket.setup(endpoint, ready.port, ready.ssrc);
                    beginHeartbeat(ready.heartbeat_interval);
                } catch (IOException e) {
                    Discord4J.LOGGER.error(LogMarkers.VOICE_WEBSOCKET, "Encountered error handling voice ready payload: ", e);
                }
                break;
            case SESSION_DESCRIPTION:
                VoiceDescriptionResponse description = DiscordUtils.MAPPER.treeToValue(d, VoiceDescriptionResponse.class);
                voiceSocket.setSecret(description.secret_key);
                voiceSocket.begin();
                break;
            case SPEAKING:
                VoiceSpeakingResponse response = DiscordUtils.MAPPER.treeToValue(d, VoiceSpeakingResponse.class);
                IUser user = getGuild().getUserByID(Long.parseUnsignedLong(response.user_id));
                users.put(response.ssrc, user);
                guild.getClient().getDispatcher().dispatch(new UserSpeakingEvent(user.getVoiceStateForGuild(guild).getChannel(), user, response.ssrc, response.speaking));
                break;
            case UNKNOWN:
                Discord4J.LOGGER.debug(LogMarkers.VOICE_WEBSOCKET, "Received unknown voice opcode, {}", message);
                break;
        }
    } catch (IOException e) {
        Discord4J.LOGGER.error(LogMarkers.WEBSOCKET, "JSON Parsing exception!", e);
    }
}
Also used : UserSpeakingEvent(sx.blah.discord.handle.impl.events.guild.voice.user.UserSpeakingEvent) VoiceSpeakingResponse(sx.blah.discord.api.internal.json.responses.voice.VoiceSpeakingResponse) IUser(sx.blah.discord.handle.obj.IUser) JsonNode(com.fasterxml.jackson.databind.JsonNode) VoiceReadyResponse(sx.blah.discord.api.internal.json.responses.voice.VoiceReadyResponse) IOException(java.io.IOException) VoiceDescriptionResponse(sx.blah.discord.api.internal.json.responses.voice.VoiceDescriptionResponse)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 VoiceDescriptionResponse (sx.blah.discord.api.internal.json.responses.voice.VoiceDescriptionResponse)1 VoiceReadyResponse (sx.blah.discord.api.internal.json.responses.voice.VoiceReadyResponse)1 VoiceSpeakingResponse (sx.blah.discord.api.internal.json.responses.voice.VoiceSpeakingResponse)1 UserSpeakingEvent (sx.blah.discord.handle.impl.events.guild.voice.user.UserSpeakingEvent)1 IUser (sx.blah.discord.handle.obj.IUser)1