Search in sources :

Example 1 with Region

use of net.dv8tion.jda.api.Region 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;
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray) Region(net.dv8tion.jda.api.Region)

Example 2 with Region

use of net.dv8tion.jda.api.Region in project Sx4 by sx4-discord-bot.

the class RegionCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "channel", nullDefault = true) VoiceChannel channel, @Argument(value = "region", endless = true) Region region) {
    if (!Region.VOICE_CHANNEL_REGIONS.contains(region)) {
        event.replyFailure("That region is not supported for voice channels").queue();
        return;
    }
    VoiceChannel effectiveChannel = channel == null ? event.getMember().getVoiceState().getChannel() : channel;
    if (effectiveChannel == null) {
        event.replyFailure("You are not in a voice channel").queue();
        return;
    }
    if (!event.getSelfMember().hasPermission(effectiveChannel, Permission.MANAGE_CHANNEL)) {
        event.replyFailure("I do not have permission to edit the region of " + effectiveChannel.getAsMention()).queue();
        return;
    }
    if (!event.getMember().hasPermission(effectiveChannel, Permission.MANAGE_CHANNEL)) {
        event.replyFailure("You do not have permission to edit the region of " + effectiveChannel.getAsMention()).queue();
        return;
    }
    if (region == effectiveChannel.getRegion()) {
        event.replyFailure("The region is already set to that").queue();
        return;
    }
    effectiveChannel.getManager().setRegion(region).flatMap($ -> event.replySuccess("Successfully changed the voice region to **" + (region == Region.AUTOMATIC ? "Automatic" : region.getName() + " " + region.getEmoji()) + "** for " + effectiveChannel.getAsMention())).queue();
}
Also used : ModuleCategory(com.sx4.bot.category.ModuleCategory) Region(net.dv8tion.jda.api.Region) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Sx4Command(com.sx4.bot.core.Sx4Command) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Permission(net.dv8tion.jda.api.Permission) Argument(com.jockie.bot.core.argument.Argument) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel)

Example 3 with Region

use of net.dv8tion.jda.api.Region in project JDA by DV8FromTheWorld.

the class GuildImpl method retrieveRegions.

@Nonnull
@Override
public RestAction<EnumSet<Region>> retrieveRegions(boolean includeDeprecated) {
    Route.CompiledRoute route = Route.Guilds.GET_VOICE_REGIONS.compile(getId());
    return new RestActionImpl<>(getJDA(), route, (response, request) -> {
        EnumSet<Region> set = EnumSet.noneOf(Region.class);
        DataArray arr = response.getArray();
        for (int i = 0; i < arr.length(); i++) {
            DataObject obj = arr.getObject(i);
            if (!includeDeprecated && obj.getBoolean("deprecated"))
                continue;
            String id = obj.getString("id", "");
            Region region = Region.fromKey(id);
            if (region != Region.UNKNOWN)
                set.add(region);
        }
        return set;
    });
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) Region(net.dv8tion.jda.api.Region) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Nonnull(javax.annotation.Nonnull)

Aggregations

Region (net.dv8tion.jda.api.Region)3 DataArray (net.dv8tion.jda.api.utils.data.DataArray)2 Argument (com.jockie.bot.core.argument.Argument)1 ModuleCategory (com.sx4.bot.category.ModuleCategory)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)1 Nonnull (javax.annotation.Nonnull)1 Permission (net.dv8tion.jda.api.Permission)1 VoiceChannel (net.dv8tion.jda.api.entities.VoiceChannel)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1