Search in sources :

Example 1 with ImplUser

use of de.btobastian.javacord.entities.impl.ImplUser in project Javacord by BtoBastian.

the class ImplDiscordAPI method getOrCreateUser.

/**
 * Gets or creates a user based on the given data.
 *
 * @param data The JSONObject containing the data.
 * @return The user.
 */
public User getOrCreateUser(JSONObject data) {
    String id = data.getString("id");
    User user = users.get(id);
    if (user == null) {
        if (!data.has("username")) {
            return null;
        }
        user = new ImplUser(data, this);
    }
    return user;
}
Also used : ImplUser(de.btobastian.javacord.entities.impl.ImplUser) ImplUser(de.btobastian.javacord.entities.impl.ImplUser)

Example 2 with ImplUser

use of de.btobastian.javacord.entities.impl.ImplUser in project Javacord by BtoBastian.

the class ChannelCreateHandler method handle.

@Override
public void handle(JSONObject packet) {
    int type = packet.getInt("type");
    switch(type) {
        case 0:
            handleServerTextChannel(packet, api.getServerById(packet.getString("guild_id")));
            break;
        case 1:
            User recipient = api.getOrCreateUser(packet.getJSONArray("recipients").getJSONObject(0));
            ((ImplUser) recipient).setUserChannelId(packet.getString("id"));
            break;
        case 2:
            handleServerVoiceChannel(packet, api.getServerById(packet.getString("guild_id")));
            break;
        case 3:
            // TODO DM groups
            break;
        case 4:
            break;
        default:
            break;
    }
}
Also used : ImplUser(de.btobastian.javacord.entities.impl.ImplUser) User(de.btobastian.javacord.entities.User) ImplUser(de.btobastian.javacord.entities.impl.ImplUser)

Example 3 with ImplUser

use of de.btobastian.javacord.entities.impl.ImplUser in project Javacord by BtoBastian.

the class PresenceUpdateHandler method handle.

@Override
public void handle(JSONObject packet) {
    final User user = api.getOrCreateUser(packet.getJSONObject("user"));
    if (user == null) {
        return;
    }
    Server server = null;
    if (packet.has("guild_id")) {
        server = api.getServerById(packet.getString("guild_id"));
    }
    if (server != null) {
        // add user to server
        ((ImplServer) server).addMember(user);
    }
    if (server != null && packet.has("roles")) {
        JSONArray roleIds = packet.getJSONArray("roles");
        for (int i = 0; i < roleIds.length(); i++) {
            // add user to the role
            ((ImplRole) server.getRoleById(roleIds.getString(i))).addUserNoUpdate(user);
        }
    }
    // check status
    if (packet.has("status")) {
        UserStatus status = UserStatus.fromString(packet.getString("status"));
        final UserStatus oldStatus = user.getStatus();
        ((ImplUser) user).setStatus(status);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<UserChangeStatusListener> listeners = api.getListeners(UserChangeStatusListener.class);
                synchronized (listeners) {
                    for (UserChangeStatusListener listener : listeners) {
                        try {
                            listener.onUserChangeStatus(api, user, oldStatus);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in UserChangeStatusListener!", t);
                        }
                    }
                }
            }
        });
    }
    // check username
    if (packet.getJSONObject("user").has("username")) {
        String name = packet.getJSONObject("user").getString("username");
        if (!user.getName().equals(name)) {
            final String oldName = user.getName();
            ((ImplUser) user).setName(name);
            listenerExecutorService.submit(new Runnable() {

                @Override
                public void run() {
                    List<UserChangeNameListener> listeners = api.getListeners(UserChangeNameListener.class);
                    synchronized (listeners) {
                        for (UserChangeNameListener listener : listeners) {
                            try {
                                listener.onUserChangeName(api, user, oldName);
                            } catch (Throwable t) {
                                logger.warn("Uncaught exception in UserChangeNameListener!", t);
                            }
                        }
                    }
                }
            });
        }
    }
    // check game
    if (packet.has("game")) {
        String game;
        if (!packet.isNull("game") && packet.getJSONObject("game").has("name") && !packet.getJSONObject("game").isNull("name")) {
            game = packet.getJSONObject("game").get("name").toString();
        } else {
            game = null;
        }
        String oldGame = user.getGame();
        if ((game == null && oldGame != null) || (game != null && oldGame == null) || (game != null && !game.equals(oldGame))) {
            ((ImplUser) user).setGame(game);
            List<UserChangeGameListener> listeners = api.getListeners(UserChangeGameListener.class);
            synchronized (listeners) {
                for (UserChangeGameListener listener : listeners) {
                    try {
                        listener.onUserChangeGame(api, user, oldGame);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in UserChangeGameListener!", t);
                    }
                }
            }
        }
    }
}
Also used : ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) ImplUser(de.btobastian.javacord.entities.impl.ImplUser) User(de.btobastian.javacord.entities.User) ImplServer(de.btobastian.javacord.entities.impl.ImplServer) UserChangeStatusListener(de.btobastian.javacord.listener.user.UserChangeStatusListener) Server(de.btobastian.javacord.entities.Server) ImplServer(de.btobastian.javacord.entities.impl.ImplServer) JSONArray(org.json.JSONArray) UserChangeGameListener(de.btobastian.javacord.listener.user.UserChangeGameListener) UserStatus(de.btobastian.javacord.entities.UserStatus) List(java.util.List) ImplUser(de.btobastian.javacord.entities.impl.ImplUser) UserChangeNameListener(de.btobastian.javacord.listener.user.UserChangeNameListener)

Example 4 with ImplUser

use of de.btobastian.javacord.entities.impl.ImplUser in project Javacord by BtoBastian.

the class VoiceStateUpdateHandler method handle.

@Override
public void handle(JSONObject packet) {
    ImplUser user = null;
    try {
        user = (ImplUser) api.getUserById(packet.getString("user_id")).get();
    } catch (JSONException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    String channelId = null;
    try {
        channelId = packet.getString("channel_id");
    } catch (JSONException ignored) {
    }
    final User userPassed = user;
    if (channelId != null) {
        if (user.getVoiceChannel() != null) {
            if (channelId.equals(user.getVoiceChannel().getId())) {
                // Probably a mute/unmute event; Ignore for now
                return;
            }
            ((ImplVoiceChannel) user.getVoiceChannel()).removeConnectedUser(user);
        }
        final ImplVoiceChannel channel = (ImplVoiceChannel) api.getVoiceChannelById(channelId);
        if (channel == null) {
            return;
        }
        channel.addConnectedUser(user);
        user.setVoiceChannel(channel);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<UserJoinVoiceChannelListener> listeners = api.getListeners(UserJoinVoiceChannelListener.class);
                synchronized (listeners) {
                    for (UserJoinVoiceChannelListener listener : listeners) {
                        try {
                            listener.onUserJoinVoiceChannel(api, userPassed, channel);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in UserJoinVoiceChannelListener!", t);
                        }
                    }
                }
            }
        });
    } else {
        if (user.getVoiceChannel() != null) {
            ((ImplVoiceChannel) user.getVoiceChannel()).removeConnectedUser(user);
        }
        user.setVoiceChannel(null);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<UserLeaveVoiceChannelListener> listeners = api.getListeners(UserLeaveVoiceChannelListener.class);
                synchronized (listeners) {
                    for (UserLeaveVoiceChannelListener listener : listeners) {
                        try {
                            listener.onUserLeaveVoiceChannel(api, userPassed);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in UserLeaveVoiceChannelListener!", t);
                        }
                    }
                }
            }
        });
    }
}
Also used : UserLeaveVoiceChannelListener(de.btobastian.javacord.listener.voice.UserLeaveVoiceChannelListener) ImplUser(de.btobastian.javacord.entities.impl.ImplUser) User(de.btobastian.javacord.entities.User) JSONException(org.json.JSONException) ImplVoiceChannel(de.btobastian.javacord.entities.impl.ImplVoiceChannel) List(java.util.List) UserJoinVoiceChannelListener(de.btobastian.javacord.listener.voice.UserJoinVoiceChannelListener) ExecutionException(java.util.concurrent.ExecutionException) ImplUser(de.btobastian.javacord.entities.impl.ImplUser)

Example 5 with ImplUser

use of de.btobastian.javacord.entities.impl.ImplUser in project Javacord by BtoBastian.

the class ReadyHandler method handle.

@Override
public void handle(JSONObject packet) {
    // guild = server
    JSONArray guilds = packet.getJSONArray("guilds");
    for (int i = 0; i < guilds.length(); i++) {
        JSONObject guild = guilds.getJSONObject(i);
        if (guild.has("unavailable") && guild.getBoolean("unavailable")) {
            // add guild to the list of unavailable servers
            api.getUnavailableServers().add(guild.getString("id"));
            continue;
        }
        new ImplServer(guild, api);
    }
    JSONArray privateChannels = packet.getJSONArray("private_channels");
    for (int i = 0; i < privateChannels.length(); i++) {
        JSONObject privateChannel = privateChannels.getJSONObject(i);
        String id = privateChannel.getString("id");
        if (privateChannel.has("recipient")) {
            User user = api.getOrCreateUser(privateChannel.getJSONObject("recipient"));
            if (user != null) {
                ((ImplUser) user).setUserChannelId(id);
            }
        }
    }
    api.setYourself(api.getOrCreateUser(packet.getJSONObject("user")));
}
Also used : ImplServer(de.btobastian.javacord.entities.impl.ImplServer) ImplUser(de.btobastian.javacord.entities.impl.ImplUser) User(de.btobastian.javacord.entities.User) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ImplUser(de.btobastian.javacord.entities.impl.ImplUser)

Aggregations

ImplUser (de.btobastian.javacord.entities.impl.ImplUser)5 User (de.btobastian.javacord.entities.User)4 ImplServer (de.btobastian.javacord.entities.impl.ImplServer)2 List (java.util.List)2 JSONArray (org.json.JSONArray)2 Server (de.btobastian.javacord.entities.Server)1 UserStatus (de.btobastian.javacord.entities.UserStatus)1 ImplVoiceChannel (de.btobastian.javacord.entities.impl.ImplVoiceChannel)1 ImplRole (de.btobastian.javacord.entities.permissions.impl.ImplRole)1 UserChangeGameListener (de.btobastian.javacord.listener.user.UserChangeGameListener)1 UserChangeNameListener (de.btobastian.javacord.listener.user.UserChangeNameListener)1 UserChangeStatusListener (de.btobastian.javacord.listener.user.UserChangeStatusListener)1 UserJoinVoiceChannelListener (de.btobastian.javacord.listener.voice.UserJoinVoiceChannelListener)1 UserLeaveVoiceChannelListener (de.btobastian.javacord.listener.voice.UserLeaveVoiceChannelListener)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1