net.dv8tion.jda.internal.entities.channel.concrete.VoiceChannelImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JDA Show documentation
Show all versions of JDA Show documentation
Java wrapper for the popular chat & VOIP service: Discord https://discord.com
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.internal.entities.channel.concrete;
import gnu.trove.map.TLongObjectMap;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.Region;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.PermissionOverride;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.Category;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.managers.channel.concrete.VoiceChannelManager;
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import net.dv8tion.jda.api.requests.restaction.ChannelAction;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.entities.channel.middleman.AbstractStandardGuildChannelImpl;
import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IAgeRestrictedChannelMixin;
import net.dv8tion.jda.internal.entities.channel.mixin.attribute.ISlowmodeChannelMixin;
import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IWebhookContainerMixin;
import net.dv8tion.jda.internal.entities.channel.mixin.middleman.AudioChannelMixin;
import net.dv8tion.jda.internal.entities.channel.mixin.middleman.GuildMessageChannelMixin;
import net.dv8tion.jda.internal.managers.channel.concrete.VoiceChannelManagerImpl;
import net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class VoiceChannelImpl extends AbstractStandardGuildChannelImpl implements
VoiceChannel,
GuildMessageChannelMixin,
AudioChannelMixin,
IWebhookContainerMixin,
IAgeRestrictedChannelMixin,
ISlowmodeChannelMixin
{
private final TLongObjectMap connectedMembers = MiscUtil.newLongMap();
private String region;
private String status = "";
private long latestMessageId;
private int bitrate;
private int userLimit;
private int slowmode;
private boolean nsfw;
public VoiceChannelImpl(long id, GuildImpl guild)
{
super(id, guild);
}
@Nonnull
@Override
public ChannelType getType()
{
return ChannelType.VOICE;
}
@Override
public int getBitrate()
{
return bitrate;
}
@Nullable
@Override
public String getRegionRaw()
{
return region;
}
@Override
public int getUserLimit()
{
return userLimit;
}
@Override
public boolean isNSFW()
{
return nsfw;
}
@Override
public int getSlowmode()
{
return slowmode;
}
@Override
public boolean canTalk(@Nonnull Member member)
{
Checks.notNull(member, "Member");
return member.hasPermission(this, Permission.MESSAGE_SEND);
}
@Override
public long getLatestMessageIdLong()
{
return latestMessageId;
}
@Nonnull
@Override
public List getMembers()
{
return Collections.unmodifiableList(new ArrayList<>(connectedMembers.valueCollection()));
}
@Nonnull
@Override
public ChannelAction createCopy(@Nonnull Guild guild)
{
Checks.notNull(guild, "Guild");
ChannelAction action = guild.createVoiceChannel(name)
.setBitrate(bitrate)
.setUserlimit(userLimit);
if (region != null)
{
action.setRegion(Region.fromKey(region));
}
if (guild.equals(getGuild()))
{
Category parent = getParentCategory();
if (parent != null)
action.setParent(parent);
for (PermissionOverride o : overrides.valueCollection())
{
if (o.isMemberOverride())
action.addMemberPermissionOverride(o.getIdLong(), o.getAllowedRaw(), o.getDeniedRaw());
else
action.addRolePermissionOverride(o.getIdLong(), o.getAllowedRaw(), o.getDeniedRaw());
}
}
return action;
}
@Nonnull
@Override
public VoiceChannelManager getManager()
{
return new VoiceChannelManagerImpl(this);
}
@Nonnull
@Override
public String getStatus()
{
return status;
}
@Nonnull
@Override
public AuditableRestAction modifyStatus(@Nonnull String status)
{
Checks.notLonger(status, MAX_STATUS_LENGTH, "Voice Status");
checkCanAccessChannel();
if (this.equals(getGuild().getSelfMember().getVoiceState().getChannel()))
checkPermission(Permission.VOICE_SET_STATUS);
else
checkCanManage();
Route.CompiledRoute route = Route.Channels.SET_STATUS.compile(getId());
DataObject body = DataObject.empty().put("status", status.isEmpty() ? null : status);
return new AuditableRestActionImpl<>(api, route, body);
}
@Override
public TLongObjectMap getConnectedMembersMap()
{
return connectedMembers;
}
@Override
public VoiceChannelImpl setBitrate(int bitrate)
{
this.bitrate = bitrate;
return this;
}
@Override
public VoiceChannelImpl setRegion(String region)
{
this.region = region;
return this;
}
@Override
public VoiceChannelImpl setUserLimit(int userLimit)
{
this.userLimit = userLimit;
return this;
}
@Override
public VoiceChannelImpl setNSFW(boolean nsfw)
{
this.nsfw = nsfw;
return this;
}
@Override
public VoiceChannelImpl setSlowmode(int slowmode)
{
this.slowmode = slowmode;
return this;
}
@Override
public VoiceChannelImpl setLatestMessageIdLong(long latestMessageId)
{
this.latestMessageId = latestMessageId;
return this;
}
public VoiceChannelImpl setStatus(String status)
{
this.status = status;
return this;
}
// -- Abstract Hooks --
@Override
protected void onPositionChange()
{
getGuild().getVoiceChannelsView().clearCachedLists();
}
}