Search in sources :

Example 61 with RESTOptions

use of io.discloader.discloader.network.rest.RESTOptions in project DiscLoader by R3alCl0ud.

the class TextChannel method setTopic.

public CompletableFuture<IGuildTextChannel> setTopic(String topic) {
    if (!this.permissionsOf(guild.getCurrentMember()).hasAny(Permissions.MANAGE_CHANNELS, Permissions.ADMINISTRATOR) && !guild.isOwner()) {
        throw new PermissionsException("Insufficient Permissions");
    }
    if (topic.length() > 1024) {
        throw new RuntimeException("topic length [" + topic.length() + "] > 1024");
    }
    CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().put("topic", topic);
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(payload), ChannelJSON.class);
    cf.thenAcceptAsync(data -> {
        IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), getGuild(), false);
        future.complete(channel);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 62 with RESTOptions

use of io.discloader.discloader.network.rest.RESTOptions in project DiscLoader by R3alCl0ud.

the class DiscLoader method login.

/**
 * Connects the current instance of the {@link DiscLoader loader} into Discord's
 * gateway servers
 *
 * @param token
 *            your API token
 * @return A CompletableFuture that completes with {@code this} if successful.
 */
public CompletableFuture<DiscLoader> login(String token) {
    if (rf != null && rf.isCompletedExceptionally())
        return rf;
    LOG.info("Attempting to login");
    rf = new CompletableFuture<>();
    Command.registerCommands();
    this.token = token;
    CompletableFuture<GatewayJSON> cf = rest.request(Methods.GET, Endpoints.gateway, new RESTOptions(), GatewayJSON.class);
    cf.thenAcceptAsync(gateway -> {
        try {
            socket.connectSocket(gateway.url + DLUtil.GatewaySuffix);
        } catch (Exception e) {
            rf.completeExceptionally(e);
        }
    });
    cf.exceptionally(e -> {
        rf.completeExceptionally(e);
        return null;
    });
    return rf;
}
Also used : RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GatewayJSON(io.discloader.discloader.network.json.GatewayJSON) GuildSyncException(io.discloader.discloader.common.exceptions.GuildSyncException) UnauthorizedException(io.discloader.discloader.common.exceptions.UnauthorizedException) AccountTypeException(io.discloader.discloader.common.exceptions.AccountTypeException)

Aggregations

RESTOptions (io.discloader.discloader.network.rest.RESTOptions)62 CompletableFuture (java.util.concurrent.CompletableFuture)61 JSONObject (org.json.JSONObject)29 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)23 ChannelJSON (io.discloader.discloader.network.json.ChannelJSON)21 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)11 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)10 IGuildChannel (io.discloader.discloader.entity.channel.IGuildChannel)10 IGuildTextChannel (io.discloader.discloader.entity.channel.IGuildTextChannel)9 AuditLog (io.discloader.discloader.core.entity.auditlog.AuditLog)8 IAuditLog (io.discloader.discloader.entity.auditlog.IAuditLog)8 IChannelCategory (io.discloader.discloader.entity.channel.IChannelCategory)8 IMessage (io.discloader.discloader.entity.message.IMessage)8 AuditLogJSON (io.discloader.discloader.network.json.AuditLogJSON)8 ArrayList (java.util.ArrayList)8 IOverwrite (io.discloader.discloader.entity.IOverwrite)7 IGuild (io.discloader.discloader.entity.guild.IGuild)7 IInvite (io.discloader.discloader.entity.invite.IInvite)7 HashMap (java.util.HashMap)7 List (java.util.List)7