Search in sources :

Example 11 with MessageJSON

use of io.discloader.discloader.network.json.MessageJSON 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)
            channel = EntityRegistry.getGroupChannelByID(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(event);
        if (channel.getType() == ChannelTypes.TEXT) {
            loader.emit(event = new GuildMessageCreateEvent(message));
        } else if (channel.getType() == ChannelTypes.DM) {
            loader.emit(event = new PrivateMessageCreateEvent(message));
        } else if (channel.getType() == ChannelTypes.GROUP) {
            loader.emit(event = new GroupMessageCreateEvent(message));
        }
        CommandHandler.handleMessageCreate(event);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ITextChannel(io.discloader.discloader.entity.channel.ITextChannel) GroupMessageCreateEvent(io.discloader.discloader.common.event.message.GroupMessageCreateEvent) GroupMessageCreateEvent(io.discloader.discloader.common.event.message.GroupMessageCreateEvent) 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 12 with MessageJSON

use of io.discloader.discloader.network.json.MessageJSON in project DiscLoader by R3alCl0ud.

the class MessageUpdate method handle.

@Override
public void handle(SocketPacket packet) {
    MessageJSON data = gson.fromJson(gson.toJson(packet.d), MessageJSON.class);
    ITextChannel channel = EntityRegistry.getTextChannelByID(data.channel_id);
    if (channel == null)
        channel = EntityRegistry.getPrivateChannelByID(data.channel_id);
    if (channel == null)
        channel = EntityRegistry.getGroupChannelByID(data.channel_id);
    if (channel == null)
        return;
    IMessage oldMessage = channel.getMessage(data.id), message = EntityBuilder.getChannelFactory().buildMessage(channel, data);
    MessageUpdateEvent event = new MessageUpdateEvent(message, oldMessage);
    loader.emit(event);
    if (message.getGuild() != null) {
        loader.emit(new GuildMessageUpdateEvent(message, oldMessage));
    } else if (channel.getType() == ChannelTypes.DM) {
        loader.emit(new PrivateMessageUpdateEvent(message, oldMessage));
    } else if (channel.getType() == ChannelTypes.GROUP) {
        loader.emit(new GroupMessageUpdateEvent(message, oldMessage));
    }
}
Also used : ITextChannel(io.discloader.discloader.entity.channel.ITextChannel) MessageJSON(io.discloader.discloader.network.json.MessageJSON) IMessage(io.discloader.discloader.entity.message.IMessage) PrivateMessageUpdateEvent(io.discloader.discloader.common.event.message.PrivateMessageUpdateEvent) MessageUpdateEvent(io.discloader.discloader.common.event.message.MessageUpdateEvent) PrivateMessageUpdateEvent(io.discloader.discloader.common.event.message.PrivateMessageUpdateEvent) GuildMessageUpdateEvent(io.discloader.discloader.common.event.message.GuildMessageUpdateEvent) GroupMessageUpdateEvent(io.discloader.discloader.common.event.message.GroupMessageUpdateEvent) GroupMessageUpdateEvent(io.discloader.discloader.common.event.message.GroupMessageUpdateEvent) GuildMessageUpdateEvent(io.discloader.discloader.common.event.message.GuildMessageUpdateEvent)

Example 13 with MessageJSON

use of io.discloader.discloader.network.json.MessageJSON in project DiscLoader by R3alCl0ud.

the class PinnedMessages method complete.

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

Example 14 with MessageJSON

use of io.discloader.discloader.network.json.MessageJSON in project DiscLoader by R3alCl0ud.

the class FetchMessages method complete.

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

Example 15 with MessageJSON

use of io.discloader.discloader.network.json.MessageJSON in project DiscLoader by R3alCl0ud.

the class TextChannel method sendMessage.

@Override
public CompletableFuture<IMessage> sendMessage(String content, RichEmbed embed, File file) {
    Attachment attachment = file == null ? null : new Attachment(file.getName());
    SendableMessage sendable = new SendableMessage(content, false, embed, attachment, file);
    CompletableFuture<IMessage> future = new CompletableFuture<>();
    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) Attachment(io.discloader.discloader.entity.sendable.Attachment)

Aggregations

IMessage (io.discloader.discloader.entity.message.IMessage)21 MessageJSON (io.discloader.discloader.network.json.MessageJSON)21 SendableMessage (io.discloader.discloader.entity.sendable.SendableMessage)15 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)15 CompletableFuture (java.util.concurrent.CompletableFuture)15 Attachment (io.discloader.discloader.entity.sendable.Attachment)9 File (java.io.File)7 Message (io.discloader.discloader.core.entity.message.Message)3 ITextChannel (io.discloader.discloader.entity.channel.ITextChannel)3 HashMap (java.util.HashMap)2 GroupMessageCreateEvent (io.discloader.discloader.common.event.message.GroupMessageCreateEvent)1 GroupMessageDeleteEvent (io.discloader.discloader.common.event.message.GroupMessageDeleteEvent)1 GroupMessageUpdateEvent (io.discloader.discloader.common.event.message.GroupMessageUpdateEvent)1 GuildMessageCreateEvent (io.discloader.discloader.common.event.message.GuildMessageCreateEvent)1 GuildMessageDeleteEvent (io.discloader.discloader.common.event.message.GuildMessageDeleteEvent)1 GuildMessageUpdateEvent (io.discloader.discloader.common.event.message.GuildMessageUpdateEvent)1 MessageCreateEvent (io.discloader.discloader.common.event.message.MessageCreateEvent)1 MessageDeleteEvent (io.discloader.discloader.common.event.message.MessageDeleteEvent)1 MessageUpdateEvent (io.discloader.discloader.common.event.message.MessageUpdateEvent)1 PrivateMessageCreateEvent (io.discloader.discloader.common.event.message.PrivateMessageCreateEvent)1