@Override public IUser getInviter() { return inviterID == 0 ? null : getGuild().getUserByID(inviterID); }
private static IUser getUserFromID(String id, IGuild guild) { try { return guild.getUserByID(Long.parseUnsignedLong(id)); } catch (Exception e) { //Ignore. Probably invalid ID. return null; } }
/** * Grabs a user from a string * * @param toLookFor The String to look with * @param guild The guild * @return The user if found, null otherwise */ public static long getUser(String toLookFor, IGuild guild) { toLookFor = GeneralUtils.trim(toLookFor); final String lower = toLookFor.toLowerCase(); if (lower.matches("@!?[0-9]+") || lower.matches("[0-9]+")) { final String parse = toLookFor.replaceAll("[<@!>]", ""); IUser exists = guild.getUserByID(Long.parseLong(toLookFor.replaceAll("[<@!>]", ""))); if (exists != null) return exists.getLongID(); } List<IUser> users = new ArrayList<>(); List<IUser> us = guild.getUsers(); users.addAll(us.stream().filter(u -> u.getName().equalsIgnoreCase(lower)).collect(Collectors.toList())); users.addAll(us.stream().filter(u -> u.getName().toLowerCase().contains(lower)).collect(Collectors.toList())); users.addAll(us.stream().filter(u -> (u.getName() + "#" + u.getDiscriminator()).equalsIgnoreCase(lower)).collect(Collectors.toList())); users.addAll(us.stream().filter(u -> u.getDiscriminator().equalsIgnoreCase(lower)).collect(Collectors.toList())); users.addAll(us.stream().filter(u -> u.getDisplayName(guild).equalsIgnoreCase(lower)).collect(Collectors.toList())); users.addAll(us.stream().filter(u -> u.getDisplayName(guild).toLowerCase().contains(lower)).collect(Collectors.toList())); if (!users.isEmpty()) return users.get(0).getLongID(); return 0; }
public static IUser getIUser(String toLookFor, IMessage m, IGuild guild) { toLookFor = toLookFor.trim(); final String lower = toLookFor.toLowerCase(); IUser res = null; if (m != null && !m.getMentions().isEmpty()) res = m.getMentions().get(0); if (toLookFor.matches("<@!?[0-9]+>")) { IUser u = guild.getUserByID(Long.parseUnsignedLong(toLookFor.replaceAll("[^0-9]", ""))); if (u != null) return u; } List<IUser> users = guild.getUsers().stream() .filter(u -> u.getName().toLowerCase().contains(lower) || u.getName().equalsIgnoreCase(lower) || u.getStringID().equals(lower) || u.getDisplayName(guild).toLowerCase().contains(lower) || u.getDisplayName(guild).equalsIgnoreCase(lower)) .collect(Collectors.toList()); if (!users.isEmpty()) res = users.get(0); return res; }
@Override public IUser getUserByID(long userID) { IUser ourUser = getClient().getOurUser(); if (ourUser != null && userID == ourUser.getLongID()) return ourUser; return guildCache.findResult((guildID, guild) -> guild.getUserByID(userID)); }
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));
/** * Converts a json {@link VoiceStateObject} to a {@link IVoiceState}. * * @param guild The guild the voice state is in. * @param json The json object representing the voice state. * @return The converted voice state object. */ public static IVoiceState getVoiceStateFromJson(IGuild guild, VoiceStateObject json) { IVoiceChannel channel = json.channel_id != null ? guild.getVoiceChannelByID(Long.parseUnsignedLong(json.channel_id)) : null; return new VoiceState(guild, channel, guild.getUserByID(Long.parseUnsignedLong(json.user_id)), json.session_id, json.deaf, json.mute, json.self_deaf, json.self_mute, json.suppress); }
private void typingStart(TypingEventResponse event) { User user; Channel channel = (Channel) client.getChannelByID(Long.parseUnsignedLong(event.channel_id)); if (channel != null) { if (channel.isPrivate()) { user = (User) ((IPrivateChannel) channel).getRecipient(); } else { user = (User) channel.getGuild().getUserByID(Long.parseUnsignedLong(event.user_id)); } if (user != null) { client.dispatcher.dispatch(new TypingEvent(user, channel)); } } }
private void reactionAdd(ReactionEventResponse event) { IChannel channel = shard.getChannelByID(Long.parseUnsignedLong(event.channel_id)); if (channel == null) return; if (!PermissionUtils.hasPermissions(channel, client.ourUser, Permissions.READ_MESSAGES, Permissions.READ_MESSAGE_HISTORY)) return; // Discord sends this event no matter our permissions for some reason. boolean cached = ((Channel) channel).messages.containsKey(Long.parseUnsignedLong(event.message_id)); IMessage message = channel.fetchMessage(Long.parseUnsignedLong(event.message_id)); if (message == null) { Discord4J.LOGGER.debug("Unable to fetch the message specified by a reaction add event\nObject={}", ToStringBuilder.reflectionToString(event)); return; } IReaction reaction = event.emoji.id == null ? message.getReactionByUnicode(event.emoji.name) : message.getReactionByID(Long.parseUnsignedLong(event.emoji.id)); message.getReactions().remove(reaction); if (reaction == null) { // Only happens in the case of a cached message with a new reaction long id = event.emoji.id == null ? 0 : Long.parseUnsignedLong(event.emoji.id); reaction = new Reaction(message, 1, ReactionEmoji.of(event.emoji.name, id)); } else if (cached) { reaction = new Reaction(message, reaction.getCount() + 1, reaction.getEmoji()); } message.getReactions().add(reaction); IUser user; if (channel.isPrivate()) { user = channel.getUsersHere().get(channel.getUsersHere().get(0).getLongID() == Long.parseUnsignedLong(event.user_id) ? 0 : 1); } else { user = channel.getGuild().getUserByID(Long.parseUnsignedLong(event.user_id)); } client.dispatcher.dispatch(new ReactionAddEvent(message, reaction, user)); }
user = channel.getGuild().getUserByID(Long.parseUnsignedLong(event.user_id));