net.dv8tion.jda.internal.entities.CategoryImpl 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;
import gnu.trove.map.TLongObjectMap;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.managers.channel.concrete.CategoryManager;
import net.dv8tion.jda.api.requests.restaction.ChannelAction;
import net.dv8tion.jda.api.requests.restaction.order.CategoryOrderAction;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.internal.entities.mixin.channel.attribute.IPermissionContainerMixin;
import net.dv8tion.jda.internal.entities.mixin.channel.attribute.IPositionableChannelMixin;
import net.dv8tion.jda.internal.managers.channel.concrete.CategoryManagerImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.PermissionUtil;
import javax.annotation.Nonnull;
public class CategoryImpl extends AbstractGuildChannelImpl implements
Category,
IPositionableChannelMixin,
IPermissionContainerMixin
{
private final TLongObjectMap overrides = MiscUtil.newLongMap();
private int position;
public CategoryImpl(long id, GuildImpl guild)
{
super(id, guild);
}
@Nonnull
@Override
public ChannelType getType()
{
return ChannelType.CATEGORY;
}
@Override
public int getPositionRaw()
{
return position;
}
@Nonnull
@Override
public ChannelAction createTextChannel(@Nonnull String name)
{
ChannelAction action = getGuild().createTextChannel(name, this);
return trySync(action);
}
@Nonnull
@Override
public ChannelAction createVoiceChannel(@Nonnull String name)
{
ChannelAction action = getGuild().createVoiceChannel(name, this);
return trySync(action);
}
@Nonnull
@Override
public ChannelAction createStageChannel(@Nonnull String name)
{
ChannelAction action = getGuild().createStageChannel(name, this);
return trySync(action);
}
@Nonnull
@Override
public CategoryOrderAction modifyTextChannelPositions()
{
return getGuild().modifyTextChannelPositions(this);
}
@Nonnull
@Override
public CategoryOrderAction modifyVoiceChannelPositions()
{
return getGuild().modifyVoiceChannelPositions(this);
}
@Nonnull
@Override
public ChannelAction createCopy(@Nonnull Guild guild)
{
Checks.notNull(guild, "Guild");
ChannelAction action = guild.createCategory(name);
if (guild.equals(getGuild()))
{
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 ChannelAction createCopy()
{
return createCopy(getGuild());
}
@Nonnull
@Override
public CategoryManager getManager()
{
return new CategoryManagerImpl(this);
}
@Override
public TLongObjectMap getPermissionOverrideMap()
{
return overrides;
}
@Override
public CategoryImpl setPosition(int position)
{
this.position = position;
return this;
}
@Override
public String toString()
{
return "GC:" + getName() + '(' + id + ')';
}
private ChannelAction trySync(ChannelAction action)
{
Member selfMember = getGuild().getSelfMember();
if (!selfMember.canSync(this))
{
long botPerms = PermissionUtil.getEffectivePermission(this, selfMember);
for (PermissionOverride override : getPermissionOverrides())
{
long perms = override.getDeniedRaw() | override.getAllowedRaw();
if ((perms & ~botPerms) != 0)
return action;
}
}
return action.syncPermissionOverrides();
}
}