Search in sources :

Example 1 with AuditableRestActionImpl

use of net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl in project pokeraidbot by magnusmickelsson.

the class InstallEmotesCommand method createEmote.

// Code taken from JDA's GuildController since they have a limitation that bot accounts can't upload emotes.
private void createEmote(String iconName, CommandEvent commandEvent, Icon icon, Role... roles) {
    JSONObject body = new JSONObject();
    body.put("name", iconName);
    body.put("image", icon.getEncoding());
    if (// making sure none of the provided roles are null before mapping them to the snowflake id
    roles.length > 0) {
        body.put("roles", Stream.of(roles).filter(Objects::nonNull).map(ISnowflake::getId).collect(Collectors.toSet()));
    }
    StringWriter writer = new StringWriter();
    body.write(writer);
    DataObject dataObject = DataObject.fromJson(writer.toString());
    GuildImpl guild = (GuildImpl) commandEvent.getGuild();
    JDA jda = commandEvent.getJDA();
    Route.CompiledRoute route = Route.Emotes.CREATE_EMOTE.compile(guild.getId());
    AuditableRestAction<Emote> action = new AuditableRestActionImpl<Emote>(jda, route, dataObject) {

        @Override
        public void handleResponse(Response response, Request<Emote> request) {
            if (response.isOk()) {
                DataObject obj = response.getObject();
                final long id = obj.getLong("id");
                String name = obj.getString("name");
                EmoteImpl emote = new EmoteImpl(id, guild).setName(name);
                // managed is false by default, should always be false for emotes created by client accounts.
                DataArray rolesArr = obj.getArray("roles");
                Set<Role> roleSet = emote.getRoleSet();
                for (int i = 0; i < rolesArr.length(); i++) {
                    roleSet.add(guild.getRoleById(rolesArr.getString(i)));
                }
                // put emote into cache
                guild.createEmote(name, icon, roles);
                request.onSuccess(emote);
            } else {
                request.onFailure(response);
                throw new RuntimeException("Couldn't install emojis. " + "Make sure that pokeraidbot has access to manage emojis.");
            }
        }
    };
    action.queue();
}
Also used : JDA(net.dv8tion.jda.api.JDA) Emote(net.dv8tion.jda.api.entities.Emote) Request(net.dv8tion.jda.api.requests.Request) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Response(net.dv8tion.jda.api.requests.Response) Role(net.dv8tion.jda.api.entities.Role) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) EmoteImpl(net.dv8tion.jda.internal.entities.EmoteImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) JSONObject(org.json.JSONObject) StringWriter(java.io.StringWriter) Route(net.dv8tion.jda.internal.requests.Route) ISnowflake(net.dv8tion.jda.api.entities.ISnowflake)

Example 2 with AuditableRestActionImpl

use of net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl in project JDA by DV8FromTheWorld.

the class ReceivedMessage method suppressEmbeds.

@Nonnull
@Override
public AuditableRestAction<Void> suppressEmbeds(boolean suppressed) {
    if (isEphemeral())
        throw new IllegalStateException("Cannot suppress embeds on ephemeral messages.");
    if (!getJDA().getSelfUser().equals(getAuthor())) {
        if (isFromType(ChannelType.PRIVATE))
            throw new PermissionException("Cannot suppress embeds of others in a PrivateChannel.");
        GuildMessageChannel gChan = getGuildChannel();
        if (!getGuild().getSelfMember().hasPermission(gChan, Permission.MESSAGE_MANAGE))
            throw new InsufficientPermissionException(gChan, Permission.MESSAGE_MANAGE);
    }
    JDAImpl jda = (JDAImpl) getJDA();
    Route.CompiledRoute route = Route.Messages.EDIT_MESSAGE.compile(getChannel().getId(), getId());
    int newFlags = flags;
    int suppressionValue = MessageFlag.EMBEDS_SUPPRESSED.getValue();
    if (suppressed)
        newFlags |= suppressionValue;
    else
        newFlags &= ~suppressionValue;
    return new AuditableRestActionImpl<>(jda, route, DataObject.empty().put("flags", newFlags));
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Example 3 with AuditableRestActionImpl

use of net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl in project JDA by DV8FromTheWorld.

the class RoleImpl method delete.

@Nonnull
@Override
public AuditableRestAction<Void> delete() {
    Guild guild = getGuild();
    if (!guild.getSelfMember().hasPermission(Permission.MANAGE_ROLES))
        throw new InsufficientPermissionException(guild, Permission.MANAGE_ROLES);
    if (!PermissionUtil.canInteract(guild.getSelfMember(), this))
        throw new HierarchyException("Can't delete role >= highest self-role");
    if (managed)
        throw new UnsupportedOperationException("Cannot delete a Role that is managed. ");
    Route.CompiledRoute route = Route.Roles.DELETE_ROLE.compile(guild.getId(), getId());
    return new AuditableRestActionImpl<>(getJDA(), route);
}
Also used : HierarchyException(net.dv8tion.jda.api.exceptions.HierarchyException) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Example 4 with AuditableRestActionImpl

use of net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl in project JDA by DV8FromTheWorld.

the class Guild method addRoleToMember.

/**
 * Atomically assigns the provided {@link net.dv8tion.jda.api.entities.Role Role} to the specified member by their user id.
 * <br><b>This can be used together with other role modification methods as it does not require an updated cache!</b>
 *
 * <p>If multiple roles should be added/removed (efficiently) in one request
 * you may use {@link #modifyMemberRoles(Member, Collection, Collection) modifyMemberRoles(Member, Collection, Collection)} or similar methods.
 *
 * <p>If the specified role is already present in the member's set of roles this does nothing.
 *
 * <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} caused by
 * the returned {@link net.dv8tion.jda.api.requests.RestAction RestAction} include the following:
 * <ul>
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
 *     <br>The Members Roles could not be modified due to a permission discrepancy</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_MEMBER UNKNOWN_MEMBER}
 *     <br>The target Member was removed from the Guild before finishing the task</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_ROLE UNKNOWN_ROLE}
 *     <br>If the specified Role does not exist</li>
 * </ul>
 *
 * @param  userId
 *         The id of the target member who will receive the new role
 * @param  role
 *         The role which should be assigned atomically
 *
 * @throws java.lang.IllegalArgumentException
 *         <ul>
 *             <li>If the specified role is not from the current Guild</li>
 *             <li>If the role is {@code null}</li>
 *         </ul>
 * @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
 *         If the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MANAGE_ROLES Permission.MANAGE_ROLES}
 * @throws net.dv8tion.jda.api.exceptions.HierarchyException
 *         If the provided roles are higher in the Guild's hierarchy
 *         and thus cannot be modified by the currently logged in account
 *
 * @return {@link net.dv8tion.jda.api.requests.restaction.AuditableRestAction AuditableRestAction}
 */
@Nonnull
@CheckReturnValue
default AuditableRestAction<Void> addRoleToMember(long userId, @Nonnull Role role) {
    Checks.notNull(role, "Role");
    Checks.check(role.getGuild().equals(this), "Role must be from the same guild! Trying to use role from %s in %s", role.getGuild().toString(), toString());
    Member member = getMemberById(userId);
    if (member != null)
        return addRoleToMember(member, role);
    if (!getSelfMember().hasPermission(Permission.MANAGE_ROLES))
        throw new InsufficientPermissionException(this, Permission.MANAGE_ROLES);
    if (!getSelfMember().canInteract(role))
        throw new HierarchyException("Can't modify a role with higher or equal highest role than yourself! Role: " + role.toString());
    Route.CompiledRoute route = Route.Guilds.ADD_MEMBER_ROLE.compile(getId(), Long.toUnsignedString(userId), role.getId());
    return new AuditableRestActionImpl<>(getJDA(), route);
}
Also used : HierarchyException(net.dv8tion.jda.api.exceptions.HierarchyException) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Route(net.dv8tion.jda.internal.requests.Route) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Example 5 with AuditableRestActionImpl

use of net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl in project JDA by DV8FromTheWorld.

the class Guild method removeRoleFromMember.

/**
 * Atomically removes the provided {@link net.dv8tion.jda.api.entities.Role Role} from the specified member by their user id.
 * <br><b>This can be used together with other role modification methods as it does not require an updated cache!</b>
 *
 * <p>If multiple roles should be added/removed (efficiently) in one request
 * you may use {@link #modifyMemberRoles(Member, Collection, Collection) modifyMemberRoles(Member, Collection, Collection)} or similar methods.
 *
 * <p>If the specified role is not present in the member's set of roles this does nothing.
 *
 * <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} caused by
 * the returned {@link net.dv8tion.jda.api.requests.RestAction RestAction} include the following:
 * <ul>
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
 *     <br>The Members Roles could not be modified due to a permission discrepancy</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_MEMBER UNKNOWN_MEMBER}
 *     <br>The target Member was removed from the Guild before finishing the task</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_ROLE UNKNOWN_ROLE}
 *     <br>If the specified Role does not exist</li>
 * </ul>
 *
 * @param  userId
 *         The id of the target member who will lose the specified role
 * @param  role
 *         The role which should be removed atomically
 *
 * @throws java.lang.IllegalArgumentException
 *         <ul>
 *             <li>If the specified role is not from the current Guild</li>
 *             <li>The role is {@code null}</li>
 *         </ul>
 * @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
 *         If the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MANAGE_ROLES Permission.MANAGE_ROLES}
 * @throws net.dv8tion.jda.api.exceptions.HierarchyException
 *         If the provided roles are higher in the Guild's hierarchy
 *         and thus cannot be modified by the currently logged in account
 *
 * @return {@link net.dv8tion.jda.api.requests.restaction.AuditableRestAction AuditableRestAction}
 */
@Nonnull
@CheckReturnValue
default AuditableRestAction<Void> removeRoleFromMember(long userId, @Nonnull Role role) {
    Checks.notNull(role, "Role");
    Checks.check(role.getGuild().equals(this), "Role must be from the same guild! Trying to use role from %s in %s", role.getGuild().toString(), toString());
    Member member = getMemberById(userId);
    if (member != null)
        return removeRoleFromMember(member, role);
    if (!getSelfMember().hasPermission(Permission.MANAGE_ROLES))
        throw new InsufficientPermissionException(this, Permission.MANAGE_ROLES);
    if (!getSelfMember().canInteract(role))
        throw new HierarchyException("Can't modify a role with higher or equal highest role than yourself! Role: " + role.toString());
    Route.CompiledRoute route = Route.Guilds.REMOVE_MEMBER_ROLE.compile(getId(), Long.toUnsignedString(userId), role.getId());
    return new AuditableRestActionImpl<>(getJDA(), route);
}
Also used : HierarchyException(net.dv8tion.jda.api.exceptions.HierarchyException) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Route(net.dv8tion.jda.internal.requests.Route) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Aggregations

Route (net.dv8tion.jda.internal.requests.Route)5 AuditableRestActionImpl (net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl)5 Nonnull (javax.annotation.Nonnull)4 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)4 HierarchyException (net.dv8tion.jda.api.exceptions.HierarchyException)3 CheckReturnValue (javax.annotation.CheckReturnValue)2 StringWriter (java.io.StringWriter)1 JDA (net.dv8tion.jda.api.JDA)1 Emote (net.dv8tion.jda.api.entities.Emote)1 ISnowflake (net.dv8tion.jda.api.entities.ISnowflake)1 Role (net.dv8tion.jda.api.entities.Role)1 PermissionException (net.dv8tion.jda.api.exceptions.PermissionException)1 Request (net.dv8tion.jda.api.requests.Request)1 Response (net.dv8tion.jda.api.requests.Response)1 DataArray (net.dv8tion.jda.api.utils.data.DataArray)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 JDAImpl (net.dv8tion.jda.internal.JDAImpl)1 EmoteImpl (net.dv8tion.jda.internal.entities.EmoteImpl)1 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)1 JSONObject (org.json.JSONObject)1