use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.
the class TypingStartHandler method handleInternally.
@Override
protected Long handleInternally(DataObject content) {
GuildImpl guild = null;
if (!content.isNull("guild_id")) {
long guildId = content.getUnsignedLong("guild_id");
guild = (GuildImpl) getJDA().getGuildById(guildId);
if (getJDA().getGuildSetupController().isLocked(guildId))
return guildId;
else if (guild == null)
// Don't cache typing events
return null;
}
final long channelId = content.getLong("channel_id");
// TODO-v5-unified-channel-cache
MessageChannel channel = getJDA().getTextChannelsView().get(channelId);
if (channel == null)
channel = getJDA().getNewsChannelView().get(channelId);
if (channel == null)
channel = getJDA().getPrivateChannelsView().get(channelId);
if (channel == null)
// We don't have the channel cached yet. We chose not to cache this event
return null;
// because that happen very often and could easily fill up the EventCache if
// we, for some reason, never get the channel. Especially in an active channel.
final long userId = content.getLong("user_id");
User user;
MemberImpl member = null;
if (channel instanceof PrivateChannel)
user = ((PrivateChannel) channel).getUser();
else
user = getJDA().getUsersView().get(userId);
if (!content.isNull("member")) {
// Try to load member for the typing event
EntityBuilder entityBuilder = getJDA().getEntityBuilder();
member = entityBuilder.createMember(guild, content.getObject("member"));
entityBuilder.updateMemberCache(member);
user = member.getUser();
}
if (user == null)
// Just like in the comment above, if for some reason we don't have the user
return null;
// then we will just throw the event away.
OffsetDateTime timestamp = Instant.ofEpochSecond(content.getInt("timestamp")).atOffset(ZoneOffset.UTC);
getJDA().handleEvent(new UserTypingEvent(getJDA(), responseNumber, user, channel, timestamp, member));
return null;
}
use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.
the class CommandInteractionPayloadImpl method parseResolved.
private void parseResolved(JDAImpl jda, DataObject resolveJson) {
EntityBuilder entityBuilder = jda.getEntityBuilder();
resolveJson.optObject("users").ifPresent(users -> users.keys().forEach(userId -> {
DataObject userJson = users.getObject(userId);
UserImpl userArg = entityBuilder.createUser(userJson);
resolved.put(userArg.getIdLong(), userArg);
}));
resolveJson.optObject("attachments").ifPresent(attachments -> attachments.keys().forEach(id -> {
DataObject json = attachments.getObject(id);
Message.Attachment file = entityBuilder.createMessageAttachment(json);
resolved.put(file.getIdLong(), file);
}));
if (// Technically these can function in DMs too ...
guild != null) {
resolveJson.optObject("members").ifPresent(members -> members.keys().forEach(memberId -> {
DataObject userJson = resolveJson.getObject("users").getObject(memberId);
DataObject memberJson = members.getObject(memberId);
memberJson.put("user", userJson);
MemberImpl optionMember = entityBuilder.createMember((GuildImpl) guild, memberJson);
entityBuilder.updateMemberCache(optionMember);
// This basically upgrades user to member
resolved.put(optionMember.getIdLong(), optionMember);
}));
resolveJson.optObject("roles").ifPresent(roles -> roles.keys().stream().map(guild::getRoleById).filter(Objects::nonNull).forEach(role -> resolved.put(role.getIdLong(), role)));
resolveJson.optObject("channels").ifPresent(channels -> {
channels.keys().forEach(id -> {
ISnowflake channelObj = jda.getGuildChannelById(id);
if (channelObj != null)
resolved.put(channelObj.getIdLong(), channelObj);
});
});
}
}
use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.
the class ThreadListSyncHandler method handleInternally.
@Override
protected Long handleInternally(DataObject content) {
long guildId = content.getLong("guild_id");
if (api.getGuildSetupController().isLocked(guildId))
return guildId;
EntityBuilder entityBuilder = api.getEntityBuilder();
DataArray threadsArrayJson = content.getArray("threads");
for (int i = 0; i < threadsArrayJson.length(); i++) {
DataObject threadJson = threadsArrayJson.getObject(i);
ThreadChannel thread = entityBuilder.createThreadChannel(threadJson, guildId);
api.handleEvent(new ThreadRevealedEvent(api, responseNumber, thread));
}
return null;
}
use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.
the class ThreadMembersUpdateHandler method handleAddedThreadMembers.
private void handleAddedThreadMembers(ThreadChannelImpl thread, DataArray addedMembersJson) {
EntityBuilder entityBuilder = api.getEntityBuilder();
CacheView.SimpleCacheView<ThreadMember> view = thread.getThreadMemberView();
List<ThreadMember> addedThreadMembers = new ArrayList<>();
for (int i = 0; i < addedMembersJson.length(); i++) {
DataObject threadMemberJson = addedMembersJson.getObject(i);
ThreadMember threadMember = entityBuilder.createThreadMember((GuildImpl) thread.getGuild(), thread, threadMemberJson);
addedThreadMembers.add(threadMember);
}
// want to cache ThreadMembers if the Members they're based on aren't being cached.
try (UnlockHook lock = view.writeLock()) {
for (ThreadMember threadMember : addedThreadMembers) {
view.getMap().put(threadMember.getIdLong(), threadMember);
}
}
// Emit the events from outside the writeLock
for (ThreadMember threadMember : addedThreadMembers) {
api.handleEvent(new ThreadMemberJoinEvent(api, responseNumber, thread, threadMember));
}
}
use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.
the class BaseGuildMessageChannelMixin method retrieveWebhooks.
@Nonnull
@Override
default RestAction<List<Webhook>> retrieveWebhooks() {
checkPermission(Permission.MANAGE_WEBHOOKS);
Route.CompiledRoute route = Route.Channels.GET_WEBHOOKS.compile(getId());
JDAImpl jda = (JDAImpl) getJDA();
return new RestActionImpl<>(jda, route, (response, request) -> {
DataArray array = response.getArray();
List<Webhook> webhooks = new ArrayList<>(array.length());
EntityBuilder builder = jda.getEntityBuilder();
for (int i = 0; i < array.length(); i++) {
try {
webhooks.add(builder.createWebhook(array.getObject(i)));
} catch (UncheckedIOException | NullPointerException e) {
JDAImpl.LOG.error("Error while creating websocket from json", e);
}
}
return Collections.unmodifiableList(webhooks);
});
}
Aggregations