Search in sources :

Example 26 with IMessage

use of io.discloader.discloader.entity.message.IMessage in project DiscLoader by R3alCl0ud.

the class ReactionAdd method handle.

public void handle(SocketPacket packet) {
    ReactionJSON data = gson.fromJson(gson.toJson(packet.d), ReactionJSON.class);
    IUser user = EntityRegistry.getUserByID(data.user_id);
    ITextChannel channel = EntityRegistry.getTextChannelByID(data.channel_id);
    if (channel == null)
        channel = EntityRegistry.getPrivateChannelByID(data.channel_id);
    if (channel == null)
        return;
    IMessage message = channel.getMessage(data.message_id);
    if (message == null || user == null)
        return;
    IReaction reaction = new Reaction(data, message);
    message.getReactions().add(reaction);
    loader.emit(new MessageReactionAddEvent(message, reaction, user));
}
Also used : ITextChannel(io.discloader.discloader.entity.channel.ITextChannel) MessageReactionAddEvent(io.discloader.discloader.common.event.message.MessageReactionAddEvent) IMessage(io.discloader.discloader.entity.message.IMessage) IUser(io.discloader.discloader.entity.user.IUser) IReaction(io.discloader.discloader.entity.message.IReaction) ReactionJSON(io.discloader.discloader.network.json.ReactionJSON) Reaction(io.discloader.discloader.core.entity.message.Reaction) IReaction(io.discloader.discloader.entity.message.IReaction)

Example 27 with IMessage

use of io.discloader.discloader.entity.message.IMessage in project DiscLoader by R3alCl0ud.

the class CommandHandler method handleMessageCreate.

public static void handleMessageCreate(MessageCreateEvent e) {
    try {
        IMessage message = e.getMessage();
        if (!handleCommands || e.getLoader().getSelfUser() == null || message.getAuthor() == null || message.getAuthor().isBot() || ((!e.getLoader().getSelfUser().isBot() && selfBot) && message.getAuthor().getID() != e.getLoader().getSelfUser().getID()) || message.getContent().length() < prefix.length()) {
            return;
        }
        String[] Args = e.args;
        String label = Args[0];
        String rest = "";
        if (label.length() < message.getContent().length())
            rest = message.getContent().substring(label.length() + 1);
        int argc = Args.length > 1 ? Args.length - 1 : 0;
        if (label.length() < prefix.length() || !label.substring(0, prefix.length()).equals(prefix)) {
            return;
        }
        label = label.substring(prefix.length());
        Command command = getCommand(label, message);
        if (command != null) {
            if (message.getChannel() instanceof IGuildTextChannel && !command.shouldExecute(message.getMember(), (IGuildTextChannel) message.getChannel()))
                return;
            String[] args = new String[argc];
            Matcher argM = command.getArgsPattern().matcher(rest);
            int n = 0;
            while (argM.find() && n < args.length) {
                for (int i = 0; i < argM.groupCount() && i < args.length; i++) {
                    try {
                        args[n] = argM.group(i);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                n++;
            }
            Thread handler = new Thread(command.getUnlocalizedName() + " - Message Handler") {

                @Override
                public void run() {
                    try {
                        command.execute(e, args);
                    } catch (Exception ex) {
                        DLLogger.getLogger(CommandHandler.class).severe("Error Occurred While attempting to execute the command \"" + command.getUnlocalizedName() + "\".");
                        ex.printStackTrace();
                    }
                }
            };
            handler.setPriority(Thread.MAX_PRIORITY - 2);
            handler.start();
            return;
        }
    } catch (Exception ex) {
        System.err.println("Error occurred whilst attempting to handle commands.");
        ex.printStackTrace();
    }
}
Also used : Matcher(java.util.regex.Matcher) IMessage(io.discloader.discloader.entity.message.IMessage) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel)

Example 28 with IMessage

use of io.discloader.discloader.entity.message.IMessage in project DiscLoader by R3alCl0ud.

the class PrivateChannel method sendMessage.

@Override
public CompletableFuture<IMessage> sendMessage(String content, RichEmbed embed, Attachment attachment, boolean tts) {
    CompletableFuture<IMessage> future = new CompletableFuture<>();
    File file = attachment == null ? null : new File(attachment.filename);
    SendableMessage sendable = new SendableMessage(content, tts, embed, attachment, file);
    CompletableFuture<MessageJSON> mcf = loader.rest.request(Methods.POST, Endpoints.messages(getID()), new RESTOptions(sendable), MessageJSON.class);
    mcf.thenAcceptAsync(e -> {
        future.complete(new Message<>(this, e));
    });
    mcf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) MessageJSON(io.discloader.discloader.network.json.MessageJSON) SendableMessage(io.discloader.discloader.entity.sendable.SendableMessage) IMessage(io.discloader.discloader.entity.message.IMessage) File(java.io.File)

Example 29 with IMessage

use of io.discloader.discloader.entity.message.IMessage in project DiscLoader by R3alCl0ud.

the class Main method testTextChannelThings.

public static CompletableFuture<Void> testTextChannelThings(IGuild guild, IChannelCategory category) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    AvoidRateLimits();
    CompletableFuture<IGuildTextChannel> f5 = category.createTextChannel("text-channel");
    f5.exceptionally(ex -> {
        logger.severe("Test Failed");
        ex.printStackTrace();
        System.exit(5);
        return null;
    });
    f5.thenAcceptAsync(textchannel -> {
        logger.config("Text Channel Created");
        AvoidRateLimits();
        CompletableFuture<IMessage> f6 = textchannel.sendMessage("content", new RichEmbed("embed").addField().setTimestamp(), new File("README.md"));
        f6.exceptionally(ex -> {
            logger.severe("Test Failed");
            ex.printStackTrace();
            System.exit(6);
            return null;
        });
        f6.thenAcceptAsync(m1 -> {
            logger.config("Message Created");
            AvoidRateLimits();
            CompletableFuture<IMessage> f7 = m1.edit("editted content");
            f7.exceptionally(ex -> {
                logger.severe("Test Failed");
                ex.printStackTrace();
                System.exit(7);
                return null;
            });
            f7.thenAcceptAsync(m2 -> {
                logger.config("Message Edited");
                AvoidRateLimits();
                CompletableFuture<IMessage> f8 = m2.pin();
                f8.exceptionally(ex -> {
                    logger.severe("Test Failed");
                    ex.printStackTrace();
                    System.exit(8);
                    return null;
                });
                f8.thenAcceptAsync(m3 -> {
                    logger.config("Message Pinned");
                    AvoidRateLimits();
                    CompletableFuture<IMessage> f9 = m3.unpin();
                    f9.exceptionally(ex -> {
                        logger.severe("Test Failed");
                        ex.printStackTrace();
                        System.exit(9);
                        return null;
                    });
                    f9.thenAcceptAsync(m4 -> {
                        logger.config("Message Unpinned");
                        AvoidRateLimits();
                        CompletableFuture<Void> f10 = m4.addReaction("🍠");
                        f10.exceptionally(ex -> {
                            logger.severe("Test Failed");
                            ex.printStackTrace();
                            System.exit(10);
                            return null;
                        });
                        f10.thenAcceptAsync(n -> {
                            logger.config("Reaction added");
                            AvoidRateLimits();
                            CompletableFuture<IMessage> f11 = m4.deleteAllReactions();
                            f11.exceptionally(ex -> {
                                logger.severe("Test Failed");
                                ex.printStackTrace();
                                System.exit(11);
                                return null;
                            });
                            f11.thenAcceptAsync(m5 -> {
                                logger.config("Reaction removed");
                                AvoidRateLimits();
                                future.complete(null);
                            });
                        });
                    });
                });
            });
        });
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) IMessage(io.discloader.discloader.entity.message.IMessage) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel) RichEmbed(io.discloader.discloader.core.entity.message.embed.RichEmbed) File(java.io.File)

Example 30 with IMessage

use of io.discloader.discloader.entity.message.IMessage in project DiscLoader by R3alCl0ud.

the class FetchMessage method complete.

@Override
public void complete(String s, Throwable ex) {
    if (ex != null) {
        future.completeExceptionally(ex);
        return;
    }
    MessageJSON data = DLUtil.gson.fromJson(s, MessageJSON.class);
    IMessage message = new Message<>(channel, data);
    channel.getMessages().put(message.getID(), message);
    future.complete(message);
}
Also used : IMessage(io.discloader.discloader.entity.message.IMessage) Message(io.discloader.discloader.core.entity.message.Message) MessageJSON(io.discloader.discloader.network.json.MessageJSON) IMessage(io.discloader.discloader.entity.message.IMessage)

Aggregations

IMessage (io.discloader.discloader.entity.message.IMessage)34 MessageJSON (io.discloader.discloader.network.json.MessageJSON)21 CompletableFuture (java.util.concurrent.CompletableFuture)20 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)17 SendableMessage (io.discloader.discloader.entity.sendable.SendableMessage)15 File (java.io.File)10 Attachment (io.discloader.discloader.entity.sendable.Attachment)9 ITextChannel (io.discloader.discloader.entity.channel.ITextChannel)6 RichEmbed (io.discloader.discloader.core.entity.message.embed.RichEmbed)4 Message (io.discloader.discloader.core.entity.message.Message)3 IGuildTextChannel (io.discloader.discloader.entity.channel.IGuildTextChannel)3 MessageCreateEvent (io.discloader.discloader.common.event.message.MessageCreateEvent)2 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)2 Reaction (io.discloader.discloader.core.entity.message.Reaction)2 IGuildChannel (io.discloader.discloader.entity.channel.IGuildChannel)2 IPrivateChannel (io.discloader.discloader.entity.channel.IPrivateChannel)2 IReaction (io.discloader.discloader.entity.message.IReaction)2 IUser (io.discloader.discloader.entity.user.IUser)2 ReactionJSON (io.discloader.discloader.network.json.ReactionJSON)2 HashMap (java.util.HashMap)2