Search in sources :

Example 11 with Route

use of net.dv8tion.jda.internal.requests.Route 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 12 with Route

use of net.dv8tion.jda.internal.requests.Route 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 13 with Route

use of net.dv8tion.jda.internal.requests.Route 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 14 with Route

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

the class StageChannelImpl method requestToSpeak.

@Nonnull
@Override
public RestAction<Void> requestToSpeak() {
    Guild guild = getGuild();
    Route.CompiledRoute route = Route.Guilds.UPDATE_VOICE_STATE.compile(guild.getId(), "@me");
    DataObject body = DataObject.empty().put("channel_id", getId());
    // Stage moderators can bypass the request queue by just unsuppressing
    if (guild.getSelfMember().hasPermission(this, Permission.VOICE_MUTE_OTHERS))
        body.putNull("request_to_speak_timestamp").put("suppress", false);
    else
        body.put("request_to_speak_timestamp", OffsetDateTime.now().toString());
    if (!this.equals(guild.getSelfMember().getVoiceState().getChannel()))
        throw new IllegalStateException("Cannot request to speak without being connected to the stage channel!");
    return new RestActionImpl<>(getJDA(), route, body);
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Example 15 with Route

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

the class StageChannelImpl method cancelRequestToSpeak.

@Nonnull
@Override
public RestAction<Void> cancelRequestToSpeak() {
    Guild guild = getGuild();
    Route.CompiledRoute route = Route.Guilds.UPDATE_VOICE_STATE.compile(guild.getId(), "@me");
    DataObject body = DataObject.empty().putNull("request_to_speak_timestamp").put("suppress", true).put("channel_id", getId());
    if (!this.equals(guild.getSelfMember().getVoiceState().getChannel()))
        throw new IllegalStateException("Cannot cancel request to speak without being connected to the stage channel!");
    return new RestActionImpl<>(getJDA(), route, body);
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Aggregations

Route (net.dv8tion.jda.internal.requests.Route)31 Nonnull (javax.annotation.Nonnull)24 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)24 DataObject (net.dv8tion.jda.api.utils.data.DataObject)12 JDAImpl (net.dv8tion.jda.internal.JDAImpl)12 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)10 AuditableRestActionImpl (net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl)10 CheckReturnValue (javax.annotation.CheckReturnValue)9 DataArray (net.dv8tion.jda.api.utils.data.DataArray)8 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)5 JDA (net.dv8tion.jda.api.JDA)3 HierarchyException (net.dv8tion.jda.api.exceptions.HierarchyException)3 MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 DeferredRestAction (net.dv8tion.jda.internal.requests.DeferredRestAction)2 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)1 WebhookEmbedBuilder (club.minnced.discord.webhook.send.WebhookEmbedBuilder)1 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1