use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.
the class ReadyHandler method handleReady.
public void handleReady(DataObject content) {
EntityBuilder builder = getJDA().getEntityBuilder();
DataArray privateChannels = content.getArray("private_channels");
for (int i = 0; i < privateChannels.length(); i++) {
DataObject chan = privateChannels.getObject(i);
ChannelType type = ChannelType.fromId(chan.getInt("type"));
// noinspection SwitchStatementWithTooFewBranches
switch(type) {
case PRIVATE:
builder.createPrivateChannel(chan);
break;
default:
WebSocketClient.LOG.warn("Received a Channel in the private_channels array in READY of an unknown type! Type: {}", type);
}
}
}
use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.
the class ChannelUpdateHandler method handleInternally.
@Override
protected Long handleInternally(DataObject content) {
final ChannelType type = ChannelType.fromId(content.getInt("type"));
if (type == ChannelType.GROUP) {
WebSocketClient.LOG.warn("Ignoring CHANNEL_UPDATE for a group which we don't support");
return null;
}
if (!content.isNull("guild_id")) {
long guildId = content.getUnsignedLong("guild_id");
if (getJDA().getGuildSetupController().isLocked(guildId))
return guildId;
}
final long channelId = content.getLong("id");
final long parentId = content.isNull("parent_id") ? 0 : content.getLong("parent_id");
final int position = content.getInt("position");
final String name = content.getString("name");
final boolean nsfw = content.getBoolean("nsfw");
final int slowmode = content.getInt("rate_limit_per_user", 0);
final DataArray permOverwrites = content.getArray("permission_overwrites");
// We assume the CHANNEL_UPDATE was for a GuildChannel because PrivateChannels don't emit CHANNEL_UPDATE for 1:1 DMs, only Groups.
GuildChannel channel = getJDA().getGuildChannelById(channelId);
if (channel == null) {
getJDA().getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
EventCache.LOG.debug("CHANNEL_UPDATE attempted to update a channel that does not exist. JSON: {}", content);
return null;
}
// Detect if we changed the channel type at all and reconstruct the channel entity if needed
channel = handleChannelTypeChange(channel, content, type);
switch(type) {
case TEXT:
{
final String topic = content.getString("topic", null);
TextChannelImpl textChannel = (TextChannelImpl) channel;
// If any properties changed, update the values and fire the proper events.
final long oldParentId = textChannel.getParentCategoryIdLong();
final String oldName = textChannel.getName();
final String oldTopic = textChannel.getTopic();
final int oldPosition = textChannel.getPositionRaw();
final boolean oldNsfw = textChannel.isNSFW();
final int oldSlowmode = textChannel.getSlowmode();
if (!Objects.equals(oldName, name)) {
textChannel.setName(name);
getJDA().handleEvent(new ChannelUpdateNameEvent(getJDA(), responseNumber, textChannel, oldName, name));
}
if (oldParentId != parentId) {
final Category oldParent = textChannel.getParentCategory();
textChannel.setParentCategory(parentId);
getJDA().handleEvent(new ChannelUpdateParentEvent(getJDA(), responseNumber, textChannel, oldParent, textChannel.getParentCategory()));
}
if (!Objects.equals(oldTopic, topic)) {
textChannel.setTopic(topic);
getJDA().handleEvent(new ChannelUpdateTopicEvent(getJDA(), responseNumber, textChannel, oldTopic, topic));
}
if (oldPosition != position) {
textChannel.setPosition(position);
getJDA().handleEvent(new ChannelUpdatePositionEvent(getJDA(), responseNumber, textChannel, oldPosition, position));
}
if (oldNsfw != nsfw) {
textChannel.setNSFW(nsfw);
getJDA().handleEvent(new ChannelUpdateNSFWEvent(getJDA(), responseNumber, textChannel, oldNsfw, nsfw));
}
if (oldSlowmode != slowmode) {
textChannel.setSlowmode(slowmode);
getJDA().handleEvent(new ChannelUpdateSlowmodeEvent(getJDA(), responseNumber, textChannel, oldSlowmode, slowmode));
}
break;
}
case NEWS:
{
final String topic = content.getString("topic", null);
NewsChannelImpl newsChannel = (NewsChannelImpl) channel;
// If any properties changed, update the values and fire the proper events.
final long oldParentId = newsChannel.getParentCategoryIdLong();
final String oldName = newsChannel.getName();
final String oldTopic = newsChannel.getTopic();
final int oldPosition = newsChannel.getPositionRaw();
final boolean oldNsfw = newsChannel.isNSFW();
if (!Objects.equals(oldName, name)) {
newsChannel.setName(name);
getJDA().handleEvent(new ChannelUpdateNameEvent(getJDA(), responseNumber, newsChannel, oldName, name));
}
if (oldParentId != parentId) {
final Category oldParent = newsChannel.getParentCategory();
newsChannel.setParentCategory(parentId);
getJDA().handleEvent(new ChannelUpdateParentEvent(getJDA(), responseNumber, newsChannel, oldParent, newsChannel.getParentCategory()));
}
if (!Objects.equals(oldTopic, topic)) {
newsChannel.setTopic(topic);
getJDA().handleEvent(new ChannelUpdateTopicEvent(getJDA(), responseNumber, newsChannel, oldTopic, topic));
}
if (oldPosition != position) {
newsChannel.setPosition(position);
getJDA().handleEvent(new ChannelUpdatePositionEvent(getJDA(), responseNumber, newsChannel, oldPosition, position));
}
if (oldNsfw != nsfw) {
newsChannel.setNSFW(nsfw);
getJDA().handleEvent(new ChannelUpdateNSFWEvent(getJDA(), responseNumber, newsChannel, oldNsfw, nsfw));
}
break;
}
case VOICE:
{
final int userLimit = content.getInt("user_limit");
final int bitrate = content.getInt("bitrate");
final String regionRaw = content.getString("rtc_region", null);
VoiceChannelImpl voiceChannel = (VoiceChannelImpl) channel;
// If any properties changed, update the values and fire the proper events.
final long oldParentId = voiceChannel.getParentCategoryIdLong();
final String oldName = voiceChannel.getName();
final String oldRegionRaw = voiceChannel.getRegionRaw();
final int oldPosition = voiceChannel.getPositionRaw();
final int oldLimit = voiceChannel.getUserLimit();
final int oldBitrate = voiceChannel.getBitrate();
if (!Objects.equals(oldName, name)) {
voiceChannel.setName(name);
getJDA().handleEvent(new ChannelUpdateNameEvent(getJDA(), responseNumber, voiceChannel, oldName, name));
}
if (!Objects.equals(oldRegionRaw, regionRaw)) {
final Region oldRegion = Region.fromKey(oldRegionRaw);
voiceChannel.setRegion(regionRaw);
getJDA().handleEvent(new ChannelUpdateRegionEvent(getJDA(), responseNumber, voiceChannel, oldRegion, voiceChannel.getRegion()));
}
if (oldParentId != parentId) {
final Category oldParent = voiceChannel.getParentCategory();
voiceChannel.setParentCategory(parentId);
getJDA().handleEvent(new ChannelUpdateParentEvent(getJDA(), responseNumber, voiceChannel, oldParent, voiceChannel.getParentCategory()));
}
if (oldPosition != position) {
voiceChannel.setPosition(position);
getJDA().handleEvent(new ChannelUpdatePositionEvent(getJDA(), responseNumber, voiceChannel, oldPosition, position));
}
if (oldLimit != userLimit) {
voiceChannel.setUserLimit(userLimit);
getJDA().handleEvent(new ChannelUpdateUserLimitEvent(getJDA(), responseNumber, voiceChannel, oldLimit, userLimit));
}
if (oldBitrate != bitrate) {
voiceChannel.setBitrate(bitrate);
getJDA().handleEvent(new ChannelUpdateBitrateEvent(getJDA(), responseNumber, voiceChannel, oldBitrate, bitrate));
}
break;
}
case STAGE:
{
final int bitrate = content.getInt("bitrate");
final String regionRaw = content.getString("rtc_region", null);
StageChannelImpl stageChannel = (StageChannelImpl) channel;
// If any properties changed, update the values and fire the proper events.
final long oldParentId = stageChannel.getParentCategoryIdLong();
final String oldName = stageChannel.getName();
final String oldRegionRaw = stageChannel.getRegionRaw();
final int oldPosition = stageChannel.getPositionRaw();
final int oldBitrate = stageChannel.getBitrate();
if (!Objects.equals(oldName, name)) {
stageChannel.setName(name);
getJDA().handleEvent(new ChannelUpdateNameEvent(getJDA(), responseNumber, stageChannel, oldName, name));
}
if (!Objects.equals(oldRegionRaw, regionRaw)) {
final Region oldRegion = Region.fromKey(oldRegionRaw);
stageChannel.setRegion(regionRaw);
getJDA().handleEvent(new ChannelUpdateRegionEvent(getJDA(), responseNumber, stageChannel, oldRegion, stageChannel.getRegion()));
}
if (oldParentId != parentId) {
final Category oldParent = stageChannel.getParentCategory();
stageChannel.setParentCategory(parentId);
getJDA().handleEvent(new ChannelUpdateParentEvent(getJDA(), responseNumber, stageChannel, oldParent, stageChannel.getParentCategory()));
}
if (oldPosition != position) {
stageChannel.setPosition(position);
getJDA().handleEvent(new ChannelUpdatePositionEvent(getJDA(), responseNumber, stageChannel, oldPosition, position));
}
if (oldBitrate != bitrate) {
stageChannel.setBitrate(bitrate);
getJDA().handleEvent(new ChannelUpdateBitrateEvent(getJDA(), responseNumber, stageChannel, oldBitrate, bitrate));
}
break;
}
case CATEGORY:
{
CategoryImpl category = (CategoryImpl) channel;
final String oldName = category.getName();
final int oldPosition = category.getPositionRaw();
if (!Objects.equals(oldName, name)) {
category.setName(name);
getJDA().handleEvent(new ChannelUpdateNameEvent(getJDA(), responseNumber, category, oldName, name));
}
if (!Objects.equals(oldPosition, position)) {
category.setPosition(position);
getJDA().handleEvent(new ChannelUpdatePositionEvent(getJDA(), responseNumber, category, oldPosition, position));
}
break;
}
default:
WebSocketClient.LOG.debug("CHANNEL_UPDATE provided an unrecognized channel type JSON: {}", content);
}
applyPermissions((IPermissionContainerMixin<?>) channel, permOverwrites);
boolean hasAccessToChannel = channel.getGuild().getSelfMember().hasPermission((IPermissionContainer) channel, Permission.VIEW_CHANNEL);
if (channel.getType().isMessage() && !hasAccessToChannel) {
handleHideChildThreads((IThreadContainer) channel);
}
return null;
}
use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.
the class GuildImpl method retrieveBanList.
@Nonnull
@Override
public RestActionImpl<List<Ban>> retrieveBanList() {
if (!getSelfMember().hasPermission(Permission.BAN_MEMBERS))
throw new InsufficientPermissionException(this, Permission.BAN_MEMBERS);
Route.CompiledRoute route = Route.Guilds.GET_BANS.compile(getId());
return new RestActionImpl<>(getJDA(), route, (response, request) -> {
EntityBuilder builder = api.getEntityBuilder();
List<Ban> bans = new LinkedList<>();
DataArray bannedArr = response.getArray();
for (int i = 0; i < bannedArr.length(); i++) {
final DataObject object = bannedArr.getObject(i);
DataObject user = object.getObject("user");
bans.add(new Ban(builder.createUser(user), object.getString("reason", null)));
}
return Collections.unmodifiableList(bans);
});
}
use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.
the class GuildImpl method retrieveInvites.
@Nonnull
@Override
public RestAction<List<Invite>> retrieveInvites() {
if (!this.getSelfMember().hasPermission(Permission.MANAGE_SERVER))
throw new InsufficientPermissionException(this, Permission.MANAGE_SERVER);
final Route.CompiledRoute route = Route.Invites.GET_GUILD_INVITES.compile(getId());
return new RestActionImpl<>(getJDA(), route, (response, request) -> {
EntityBuilder entityBuilder = api.getEntityBuilder();
DataArray array = response.getArray();
List<Invite> invites = new ArrayList<>(array.length());
for (int i = 0; i < array.length(); i++) invites.add(entityBuilder.createInvite(array.getObject(i)));
return Collections.unmodifiableList(invites);
});
}
use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.
the class GuildEmojisUpdateHandler method handleInternally.
@Override
protected Long handleInternally(DataObject content) {
if (!getJDA().isCacheFlagSet(CacheFlag.EMOTE))
return null;
final long guildId = content.getLong("guild_id");
if (getJDA().getGuildSetupController().isLocked(guildId))
return guildId;
GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
if (guild == null) {
getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
return null;
}
DataArray array = content.getArray("emojis");
List<Emote> oldEmotes, newEmotes;
SnowflakeCacheViewImpl<Emote> emoteView = guild.getEmotesView();
try (UnlockHook hook = emoteView.writeLock()) {
TLongObjectMap<Emote> emoteMap = emoteView.getMap();
// snapshot of emote cache
oldEmotes = new ArrayList<>(emoteMap.valueCollection());
newEmotes = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
DataObject current = array.getObject(i);
final long emoteId = current.getLong("id");
EmoteImpl emote = (EmoteImpl) emoteMap.get(emoteId);
EmoteImpl oldEmote = null;
if (emote == null) {
emote = new EmoteImpl(emoteId, guild);
newEmotes.add(emote);
} else {
// emote is in our cache which is why we don't want to remove it in cleanup later
oldEmotes.remove(emote);
oldEmote = emote.clone();
}
emote.setName(current.getString("name")).setAnimated(current.getBoolean("animated")).setManaged(current.getBoolean("managed"));
// update roles
DataArray roles = current.getArray("roles");
Set<Role> newRoles = emote.getRoleSet();
// snapshot of cached roles
Set<Role> oldRoles = new HashSet<>(newRoles);
for (int j = 0; j < roles.length(); j++) {
Role role = guild.getRoleById(roles.getString(j));
if (role != null) {
newRoles.add(role);
oldRoles.remove(role);
}
}
// cleanup old cached roles that were not found in the JSONArray
for (Role r : oldRoles) {
// newRoles directly writes to the set contained in the emote
newRoles.remove(r);
}
// finally, update the emote
emoteMap.put(emote.getIdLong(), emote);
// check for updated fields and fire events
handleReplace(oldEmote, emote);
}
for (Emote e : oldEmotes) emoteMap.remove(e.getIdLong());
}
// cleanup old emotes that don't exist anymore
for (Emote e : oldEmotes) {
getJDA().handleEvent(new EmoteRemovedEvent(getJDA(), responseNumber, e));
}
for (Emote e : newEmotes) {
getJDA().handleEvent(new EmoteAddedEvent(getJDA(), responseNumber, e));
}
return null;
}
Aggregations