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();
}
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));
}
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);
}
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);
}
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);
}
Aggregations