Search in sources :

Example 6 with EventListenerAdapter

use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.

the class GuildMember method takeRole.

@Override
public CompletableFuture<IGuildMember> takeRole(IRole... roles) {
    if (!guild.hasPermission(Permissions.MANAGE_ROLES)) {
        throw new PermissionsException("Insuccficient Permissions");
    }
    List<IRole> rls = getRoles();
    for (IRole role : roles) {
        if (!guild.isOwner() && role.getPosition() >= guild.getCurrentMember().getHighestRole().getPosition()) {
            throw new PermissionsException("Cannot take away roles higher than your's");
        }
        if (hasRole(role)) {
            rls.remove(role);
        }
    }
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    String[] ids = new String[rls.size()];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = SnowflakeUtil.asString(rls.get(i));
    }
    JSONObject payload = new JSONObject().put("roles", ids);
    CompletableFuture<Void> tcf = getLoader().rest.request(Methods.PATCH, Endpoints.guildMember(getGuild().getID(), getID()), new RESTOptions(payload), Void.class);
    IEventListener iel = new EventListenerAdapter() {

        public void GuildMemberUpdate(GuildMemberUpdateEvent e) {
            if (e.getMember().getID() == getID()) {
                future.complete(e.getMember());
                getLoader().removeEventListener(this);
            }
        }
    };
    getLoader().addEventListener(iel);
    tcf.exceptionally(ex -> {
        getLoader().removeEventListener(iel);
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) IRole(io.discloader.discloader.entity.guild.IRole) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GuildMemberUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter)

Example 7 with EventListenerAdapter

use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.

the class Main method main.

public static void main(String... args) throws IOException {
    System.setProperty("http.agent", "DiscLoader");
    String content = "";
    if (new File("options.json").exists()) {
        Object[] lines = Files.readAllLines(Paths.get("./options.json")).toArray();
        for (Object line : lines) content += line;
        options options = gson.fromJson(content, options.class);
        token = options.auth.token;
    }
    DLOptions options = parseArgs(args);
    try {
        ModRegistry.startMods().get();
    } catch (InterruptedException | ExecutionException e1) {
        e1.printStackTrace();
    }
    if (options.shards > 1) {
        ShardManager manager = new ShardManager(options);
        manager.addShardingListener(new ShardingListenerAdapter() {

            public void ShardLaunched(Shard shard) {
                LOGGER.info(String.format("Shard #%d: Launched", shard.getShardID()));
                shard.getLoader().addEventListener(new EventListenerAdapter() {

                    public void Ready(ReadyEvent e) {
                        for (Command command : CommandRegistry.commands.entries()) {
                            LOGGER.info(command.getUnlocalizedName());
                        }
                    }

                    @Override
                    public void RawPacket(RawEvent data) {
                        WebSocketFrame frame = data.getFrame();
                        if (data.isGateway() && frame.isTextFrame() && !frame.getPayloadText().contains("PRESENCE_UPDATE")) {
                        // LOGGER.fine(frame.getPayloadText());
                        } else if (data.isREST()) {
                            LOGGER.info(data.getHttpResponse().getBody());
                            LOGGER.info("" + data.getHttpResponse().getStatus());
                        }
                    }

                    @Override
                    public void GuildMessageCreate(GuildMessageCreateEvent e) {
                        if (e.getGuild().getID() != 282226852616077312l)
                            return;
                        if (e.getMessage().getContent().startsWith("logs")) {
                            String[] args = e.getMessage().getContent().split(" ");
                            if (args.length == 1) {
                            } else if (args.length == 2) {
                            }
                        }
                    }
                });
            }
        });
        manager.launchShards();
    } else {
        loader = new DiscLoader(options);
        loader.addEventListener(new EventListenerAdapter() {

            @Override
            public void MessageCreate(MessageCreateEvent e) {
                if (e.getMessage().getContent().equals("#$close")) {
                    e.getLoader().socket.ws.disconnect(1001);
                }
            }

            public void Ready(ReadyEvent e) {
                LOGGER.info(e.getLoader().user.getUsername());
                for (Command command : CommandRegistry.commands.entries()) {
                    LOGGER.info(command.getUnlocalizedName());
                }
                LOGGER.info(EntityRegistry.getGuildByID(282226852616077312l).getDefaultChannel().getName());
            // .sendMessage("testing 125");
            }

            @Override
            public void RawPacket(RawEvent data) {
                WebSocketFrame frame = data.getFrame();
                if (data.isGateway() && frame.isTextFrame() && !frame.getPayloadText().contains("PRESENCE_UPDATE")) {
                    LOGGER.info(frame.getPayloadText());
                } else if (data.isREST() && data.getHttpResponse() != null) {
                    LOGGER.info(data.getHttpResponse().getBody());
                }
            }
        });
        loader.login();
    }
}
Also used : RawEvent(io.discloader.discloader.common.event.RawEvent) ShardManager(io.discloader.discloader.common.ShardManager) ReadyEvent(io.discloader.discloader.common.event.ReadyEvent) GuildMessageCreateEvent(io.discloader.discloader.common.event.message.GuildMessageCreateEvent) MessageCreateEvent(io.discloader.discloader.common.event.message.MessageCreateEvent) DLOptions(io.discloader.discloader.common.DLOptions) Command(io.discloader.discloader.client.command.Command) DiscLoader(io.discloader.discloader.common.DiscLoader) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) WebSocketFrame(com.neovisionaries.ws.client.WebSocketFrame) ExecutionException(java.util.concurrent.ExecutionException) Shard(io.discloader.discloader.common.Shard) GuildMessageCreateEvent(io.discloader.discloader.common.event.message.GuildMessageCreateEvent) File(java.io.File) ShardingListenerAdapter(io.discloader.discloader.common.event.sharding.ShardingListenerAdapter)

Aggregations

EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)7 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)5 JSONObject (org.json.JSONObject)5 IEventListener (io.discloader.discloader.common.event.IEventListener)4 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)3 GuildEmojiUpdateEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent)2 GuildMemberUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent)2 IGuildEmoji (io.discloader.discloader.entity.guild.IGuildEmoji)2 IRole (io.discloader.discloader.entity.guild.IRole)2 EmojiJSON (io.discloader.discloader.network.json.EmojiJSON)2 WebSocketFrame (com.neovisionaries.ws.client.WebSocketFrame)1 Command (io.discloader.discloader.client.command.Command)1 DLOptions (io.discloader.discloader.common.DLOptions)1 DiscLoader (io.discloader.discloader.common.DiscLoader)1 Shard (io.discloader.discloader.common.Shard)1 ShardManager (io.discloader.discloader.common.ShardManager)1 RawEvent (io.discloader.discloader.common.event.RawEvent)1 ReadyEvent (io.discloader.discloader.common.event.ReadyEvent)1