Search in sources :

Example 1 with MessageCreateEvent

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

the class MessageCreate method handle.

@Override
public void handle(SocketPacket packet) {
    MessageJSON data = this.gson.fromJson(gson.toJson(packet.d), MessageJSON.class);
    try {
        long channelID = SnowflakeUtil.parse(data.channel_id);
        ITextChannel channel = EntityRegistry.getTextChannelByID(channelID);
        if (channel == null)
            channel = EntityRegistry.getPrivateChannelByID(channelID);
        if (channel == null)
            return;
        IMessage message = EntityBuilder.getChannelFactory().buildMessage(channel, data);
        channel.getMessages().put(message.getID(), message);
        if (channel.isTyping(message.getAuthor())) {
            channel.getTyping().remove(message.getAuthor().getID());
        }
        MessageCreateEvent event = new MessageCreateEvent(message);
        loader.emit(DLUtil.Events.MESSAGE_CREATE, event);
        loader.emit(event);
        if (channel.getType() == ChannelTypes.TEXT) {
            GuildMessageCreateEvent event2 = new GuildMessageCreateEvent(message);
            loader.emit("GuildMessageCreate", event2);
            loader.emit(event2);
        } else if (channel.getType() == ChannelTypes.DM) {
            PrivateMessageCreateEvent event2 = new PrivateMessageCreateEvent(message);
            loader.emit(DLUtil.Events.PRIVATE_MESSAGE_CREATE, event2);
            loader.emit(event2);
        }
        CommandHandler.handleMessageCreate(event);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ITextChannel(io.discloader.discloader.entity.channel.ITextChannel) PrivateMessageCreateEvent(io.discloader.discloader.common.event.message.PrivateMessageCreateEvent) MessageCreateEvent(io.discloader.discloader.common.event.message.MessageCreateEvent) GuildMessageCreateEvent(io.discloader.discloader.common.event.message.GuildMessageCreateEvent) MessageJSON(io.discloader.discloader.network.json.MessageJSON) IMessage(io.discloader.discloader.entity.message.IMessage) PrivateMessageCreateEvent(io.discloader.discloader.common.event.message.PrivateMessageCreateEvent) GuildMessageCreateEvent(io.discloader.discloader.common.event.message.GuildMessageCreateEvent)

Example 2 with MessageCreateEvent

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

the class CommandHelp method execute.

@Override
public void execute(MessageCreateEvent e, String[] args) {
    IMessage message = e.getMessage();
    RichEmbed embed = new RichEmbed().setFooter(String.format("type `%shelp <page>` to tab through the pages", CommandHandler.prefix), e.loader.user.getAvatar().toString()).setAuthor(e.loader.user.getUsername(), "http://discloader.io", e.loader.user.getAvatar().toString()).setColor(0x08a2ff);
    Command command;
    embed.setThumbnail(getResourceLocation());
    if (args.length >= 1 && (command = CommandHandler.getCommand(args[0], message)) != null) {
        if (command != null) {
            File icon = DLUtil.MissingTexture;
            embed.setThumbnail(command.getResourceLocation());
            if (embed.getThumbnail() == null)
                embed.setThumbnail(icon);
            if (args.length > 1 && command instanceof CommandTree) {
                for (int i = 1; i < args.length; i++) {
                    if (((CommandTree) command).getSubCommands().get(args[i]) != null)
                        command = ((CommandTree) command).getSubCommands().get(args[i]);
                }
            }
            embed.setTitle(command.getUnlocalizedName()).addField("Description", this.getCommandDesc(command), true).addField("Usage", command.getUsage(), true);
            if (command instanceof CommandTree) {
                String commands = "";
                for (Command sub : ((CommandTree) command).getSubCommands().values()) {
                    String desc = sub.getDescription();
                    commands = String.format("%s**%s**: %s\n", commands, sub.getUnlocalizedName(), desc);
                }
                embed.addField("Sub Commands", commands, true);
            }
            e.getChannel().sendEmbed(embed);
            return;
        }
    } else if (args.length == 1 && args[0] != null && args[0].length() > 0) {
        String commands = "";
        int page = Integer.parseInt(args[0], 10);
        int size = CommandRegistry.commands.entries().size();
        List<Command> cmdList = Lists.newArrayList(CommandRegistry.commands.entries().toArray(new Command[size]));
        cmdList.sort((a, b) -> {
            if (a.getUnlocalizedName().compareToIgnoreCase(b.getUnlocalizedName()) < 0)
                return -1;
            if (a.getUnlocalizedName().compareToIgnoreCase(b.getUnlocalizedName()) > 0)
                return 1;
            return 0;
        });
        for (int i = 0 + (10 * (page - 1)); i < (10 * page) && i < size; i++) {
            String desc = this.getCommandDesc(cmdList.get(i));
            commands = String.format("%s**%s**: %s\n", commands, cmdList.get(i).getUnlocalizedName(), desc);
        }
        embed.addField("Commands", commands, true);
        embed.setTitle(String.format("Help. Page: %d/%d", page, (size / 10) + (size % 10 != 0 ? 1 : 0)));
    } else {
        String commands = "";
        int size = CommandRegistry.commands.entries().size();
        List<Command> cmdList = Lists.newArrayList(CommandRegistry.commands.entries().toArray(new Command[size]));
        cmdList.sort((a, b) -> {
            if (a.getUnlocalizedName().compareToIgnoreCase(b.getUnlocalizedName()) < 0)
                return -1;
            if (a.getUnlocalizedName().compareToIgnoreCase(b.getUnlocalizedName()) > 0)
                return 1;
            return 0;
        });
        for (int i = 0; i < 10 && i < size; i++) {
            String desc = cmdList.get(i).getDescription();
            commands = String.format("%s**%s**: %s\n", commands, cmdList.get(i).getUnlocalizedName(), desc);
        }
        embed.addField("Commands", commands, true);
        embed.setTitle(String.format("Help. Page: 1/%d", (size / 10) + (size % 10 != 0 ? 1 : 0)));
    }
    e.getChannel().sendEmbed(embed);
}
Also used : List(java.util.List) RichEmbed(io.discloader.discloader.core.entity.RichEmbed) Lists(com.google.common.collect.Lists) Locale(java.util.Locale) Resource(io.discloader.discloader.client.render.util.Resource) CommandRegistry(io.discloader.discloader.common.registry.CommandRegistry) MessageCreateEvent(io.discloader.discloader.common.event.message.MessageCreateEvent) DLUtil(io.discloader.discloader.util.DLUtil) File(java.io.File) LanguageRegistry(io.discloader.discloader.common.language.LanguageRegistry) IMessage(io.discloader.discloader.entity.message.IMessage) IMessage(io.discloader.discloader.entity.message.IMessage) RichEmbed(io.discloader.discloader.core.entity.RichEmbed) List(java.util.List) File(java.io.File)

Example 3 with MessageCreateEvent

use of io.discloader.discloader.common.event.message.MessageCreateEvent 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

MessageCreateEvent (io.discloader.discloader.common.event.message.MessageCreateEvent)3 GuildMessageCreateEvent (io.discloader.discloader.common.event.message.GuildMessageCreateEvent)2 IMessage (io.discloader.discloader.entity.message.IMessage)2 File (java.io.File)2 Lists (com.google.common.collect.Lists)1 WebSocketFrame (com.neovisionaries.ws.client.WebSocketFrame)1 Command (io.discloader.discloader.client.command.Command)1 Resource (io.discloader.discloader.client.render.util.Resource)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 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)1 RawEvent (io.discloader.discloader.common.event.RawEvent)1 ReadyEvent (io.discloader.discloader.common.event.ReadyEvent)1 PrivateMessageCreateEvent (io.discloader.discloader.common.event.message.PrivateMessageCreateEvent)1 ShardingListenerAdapter (io.discloader.discloader.common.event.sharding.ShardingListenerAdapter)1 LanguageRegistry (io.discloader.discloader.common.language.LanguageRegistry)1 CommandRegistry (io.discloader.discloader.common.registry.CommandRegistry)1 RichEmbed (io.discloader.discloader.core.entity.RichEmbed)1